【问题标题】:error C2220: warning treated as error - no 'object' file generated错误 C2220:警告视为错误 - 未生成“对象”文件
【发布时间】:2013-08-16 01:05:51
【问题描述】:

我有以下课程

class Cdata12Mnt
{
public:
    char IOBname[ID1_IOB_PIOTSUP-ID1_IOB_TOP][BOADNAM_MAX + 4];
    char ExIOBname[ID1_MAX_INF-ID1_EXIOB_U1TOP][BOADNAM_MAX + 4];
    char cflpath[256];
    char basetext[256];
    UINT database[ID1_MAX_INF];
    int State;

public:
    char SelectPath[256];

public:
    int GetIOBName(int slt,char *Name);
    Cdata12Mnt(char *SelectPath);
    virtual ~Cdata12Mnt();
    int     GetValue(int id);
    int     GetState() { return State; }
};

我的功能如下

Cdata12Mnt::Cdata12Mnt(char *SelectPath)
{
    SCTReg  reg;
    char    buf[256], *cpnt, *npnt, *bpnt1, *bpnt2;
    char    *startcode[] = {"CNTL_CODE ","SEGMENT "};
    char    *stopcode    = {"END_CNTL_CODE "};
    FILE    *fp;
    int     ii, infl;

    State = 0;

    for (ii = 0; ii < (ID1_IOB_PIOTSUP - ID1_IOB_TOP); ii++) {
        strcpy(IOBname[ii], "");
    }

    for (ii = 0; ii < (ID1_MAX_INF-ID1_EXIOB_U1TOP); ii++) {
        **strcpy(ExIOBname[ii], "");**
    }

    sprintf(cflpath, "%s\\%s", SelectPath, CDATAFL);

    if ((fp = fopen(cflpath,"r"))!=NULL) {
        for (ii = 0, infl = 0; fgets(buf, 256, fp) != NULL;) {
            if (infl == 0 && strncmp(buf, startcode[0], strlen(startcode[0])) == 0) {
                if ((cpnt = strchr(&buf[strlen(startcode[0])],*startcode[1])) != NULL) {
                    if (strncmp(cpnt,startcode[1], strlen(startcode[1])) == 0) {
                        infl = 1;
                        continue;
                    }
                }
            }

            if (infl == 0) {
                continue;
            }

            if (strncmp(buf,stopcode,strlen(stopcode))==0) {
                if (ii == ID1_EXIOB_U1TOP) {
                    for (int nDataNumber = ii; nDataNumber < ID1_MAX_INF; nDataNumber++) {
                        database[nDataNumber] = 0;
                    }
                }

                infl = 0;
                continue;
            }

            if (strncmp(&buf[14], " DD ", 4) == 0) {
                if ((cpnt=strchr(buf, ';')) != NULL) {
                    *cpnt = '\0';
                }

                if (ii >= ID1_IOB_TOP && ii < ID1_IOB_PIOTSUP) {
                    if ((bpnt1 = strchr(cpnt + 1,'(')) != NULL && (bpnt2=strchr(cpnt + 1,')'))!=NULL && bpnt1 < bpnt2) {
                        *bpnt2 = '\0';
                        *(bpnt1 + BOADNAM_MAX + 1) = '\0';
                        strcpy(IOBname[ii-ID1_IOB_TOP], bpnt1 + 1);
                    }
                }

                if (ii >= ID1_EXIOB_U1TOP && ii < ID1_MAX_INF) {
                    if ((bpnt1 = strchr(cpnt + 1, '(')) != NULL && (bpnt2=strchr(cpnt+1,')'))!=NULL && bpnt1 < bpnt2) {
                            *bpnt2='\0';
                            *(bpnt1+BOADNAM_MAX+1)='\0';
                            strcpy(ExIOBname[ii-ID1_EXIOB_U1TOP], bpnt1 + 1);
                    }
                }

                for (cpnt = &buf[18]; cpnt != NULL;) {
                    if ((npnt=strchr(cpnt, ',')) != NULL)
                        *npnt='\0';
                }

                if (strchr(cpnt,'H')!=NULL) {
                    sscanf(cpnt,"%XH",&database[ii]);
                } else {
                    database[ii]=atoi(cpnt);
                }

                ii++;
                cpnt = npnt;

                if (cpnt != NULL) {
                    cpnt++;
                }
            }
        }
    }

    fclose(fp);
} else {
    State=-1;
}

当我在 Visual Studio 2008 中编译此函数时,它在 strcpy(IOBname[ii],""); 处出现错误,如下所示

错误 C2220:警告视为错误 - 未生成“对象”文件

如何解决这个错误?

【问题讨论】:

  • 您的项目设置包含一个标志,告诉编译器将警告视为错误。关闭该标志,您将只看到原始警告(无论是什么)。
  • 顺便问一下,为什么 C++ 项目中有旧的 C 风格字符串?使用std::string,从长远来看效果会更好。此外,不要使用旧的 C 样式数组,而是使用 std::array(或 std::vector)。
  • 修复你的警告,它会构建

标签: c++ object compiler-errors project compiler-warnings


【解决方案1】:

错误表示警告被视为错误,因此您的问题是警告消息!然后没有创建目标文件因为有一个错误。所以你需要检查你的警告并修复它们。

如果您不知道如何找到它们:打开Error List (View > Error List) 并点击Warning

【讨论】:

  • 任何不是纯VisualStudio的解决方案?我在另一个 IDE 下遇到了同样的问题。
【解决方案2】:

转到project properties -&gt; configurations properties -&gt; C/C++ -&gt; treats warning as error -&gt; No (/WX-)

【讨论】:

  • 所以你的解决方案是:“盲目地忽略所有警告”?
  • @mojtaba setoodeh,忽略警告不是解决办法,而是绕过。
  • 不是一个有效的解决方案!
  • 这取决于你想要做什么。如果您正在使用共享代码库并且您不关心导致此错误的模块,那么此解决方案应该可以帮助您继续工作,以便您可以专注于您正在做的事情。
  • 你为什么关心开发模式。只需按照建议禁用并专注于工作。稍后再次启用它,在签入前修复警告。
【解决方案3】:

附带说明,您可以使用#pragma 启用/禁用个别警告。你可以看看文档here

来自文档:

// pragma_warning.cpp
// compile with: /W1
#pragma warning(disable:4700)
void Test() {
   int x;
   int y = x;   // no C4700 here
   #pragma warning(default:4700)   // C4700 enabled after Test ends
}

int main() {
   int x;
   int y = x;   // C4700
}

【讨论】:

    【解决方案4】:

    此错误消息非常令人困惑。我刚刚修复了项目中的其他“警告”,而我真的只有一个(简单的一个):

    警告 C4101:“i”:未引用的局部变量

    在我评论了这个未使用的i并编译它之后,另一个错误消失了。

    【讨论】:

    • 我确认,我今天遇到了类似的问题。在某处出现“无法访问的代码”警告以及令人困惑的“未生成'目标文件'”。修复无法访问的警告也消除了第二个警告。有点奇怪。
    【解决方案5】:

    此警告是关于不安全地使用 strcpy。请改用IOBname[ii]='\0';

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2012-01-13
      • 2022-08-03
      • 2010-10-09
      • 1970-01-01
      相关资源
      最近更新 更多