在Visual Studio C ++中编译时,错误MSB6006“ CL.exe”以代码2退出

时间:2019-09-13 08:50:03

标签: c++ visual-studio

我从事c ++控制台项目。该项目在msmpisdk平台中具有。当我在Visual Studio 2019中进行编译时,发生以下错误:

  

“严重性代码描述项目文件行抑制状态...错误MSB6006“ CL.exe”退出,代码为2。...C:\ Program Files(x86)\ Microsoft VisualStudio \ 2019 \ Enterprise \ MSBuild \ Microsoft \ VC \ v160- \ Microsoft.CppCommon.targets 429。“

我检查了我的代码,看来还可以,还检查了项目的ref lib,看来还可以。

我在网上搜索了。

  1. 所有函数都返回一个值。
  2. 所有变量在使用前都会设置一个值。
  3. 我重新启动了Visual Studio和计算机。
  4. 我创建了一个新项目并添加了代码,但是发生了相同的错误。

但是发生了相同的错误,我的代码无法编译。

1 个答案:

答案 0 :(得分:0)

最后我找到了问题。

  • 我的变量之一,已在算法中初始化。
  • 但是编译器无法检测到之前初始化的变量并引发错误。
  • 请参阅下面的代码,以查看此错误的产生方式。

#include "iostream"
class myclass1
{
    public: int _AMethod() { return 55; }
};

int main()
{
  myclass1* myVariable;
  int x = 0;
  if (x == 0)
  {
    myVariable = (myclass1*)malloc(sizeof(myclass1) * 5);
    //myVariable = init();
  }

  if (x == 0)
  {
    myVariable->_AMethod();
  }
}

#include "iostream" class myclass1 { public: int _AMethod() { return 55; } }; int main() { myclass1* myVariable; int x = 0; if (x == 0) { myVariable = (myclass1*)malloc(sizeof(myclass1) * 5); //myVariable = init(); } if (x == 0) { myVariable->_AMethod(); } }

现在在编译时发生此错误: 错误MSB6006“ CL.exe”退出,代码为2。

  • 算法中的所有内容都可以。
  • 但是编译器会引发错误..

此错误可以很容易地通过定义init来解决,如下所示:

#include "iostream"
class myclass1
{
    public: int _AMethod() { return 55; }
};

int main()
{
  myclass1* myVariable=(myclass1*)malloc(sizeof(myclass1) * 5);
  int x = 0;
  if (x == 0)
  {
    //myVariable = (myclass1*)malloc(sizeof(myclass1) * 5);
    //myVariable = init();
  }

  if (x == 0)
  {
    myVariable->_AMethod();
  }
}

相关问题