C++ 错误 MSB6006:“CL.exe”以代码 2 退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13948990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error MSB6006: "CL.exe" exited with code 2
提问by tiavec88
I'm writing with visual c++ and when I compile this error occures:
我正在用 Visual C++ 编写,当我编译时出现此错误:
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(147,5): error MSB6006: "CL.exe" terminato con il codice 2.
Does anyone know why?
有谁知道为什么?
Thanks in advance!
提前致谢!
回答by Michel Keijzers
I got the same error when I forgot the return statement in the following method:
当我忘记以下方法中的 return 语句时,我遇到了同样的错误:
char SpiRAM::write_byte(int address, char data_byte)
{
assert(address >= 0);
assert(address < SRAM_SIZE);
_sram[address] = data_byte;
return data_byte;
}
回答by Sahbi
You can actually see the proper error messages instead of Microsoft's arbitrary error codes. But since the error list is always forcibly made visible when there are errors, it isn't quite so obvious. Next to the tab Error List
is another tab Output
, which shows the raw error output. I'm not sure if that tab exists in all versions since I'm using 2019, but there might be something very similar in older versions anyways. Differently named perhaps, or an entirely separate window instead of grouped with Error List
.
您实际上可以看到正确的错误消息,而不是 Microsoft 的任意错误代码。但是由于错误列表总是在出现错误时强制可见,所以它不是那么明显。选项卡旁边Error List
是另一个选项卡Output
,显示原始错误输出。自从我使用 2019 以来,我不确定该选项卡是否在所有版本中都存在,但无论如何在旧版本中可能会有非常相似的东西。可能名称不同,或者是一个完全独立的窗口而不是与Error List
.
In the case of another answerer herethat exact tab would've shown: error C4700: uninitialized local variable 'm' used
对于此处的另一个回答者,该确切选项卡将显示:error C4700: uninitialized local variable 'm' used
Which would have saved him from having to dig through all his code. =]
这将使他不必深入研究他的所有代码。=]
And if you forget a return value for a function that requires it, you'll see: error C4716: 'foo': must return a value
如果您忘记了需要它的函数的返回值,您会看到: error C4716: 'foo': must return a value
回答by Ali ?hsan Elmas
This happened me for a variety of different reasons:
这发生在我身上的原因有很多:
1) I forgot to add a return statement to a non-void function.
1)我忘记向非空函数添加 return 语句。
2) I tried to use an uninitialized pointer.
2)我尝试使用未初始化的指针。
3) I wrote a loop like for(int i=i;...) instead of for(int i=0;...)
3)我写了一个循环,比如 for(int i=i;...) 而不是 for(int i=0;...)
You can check your code for these and it may help.
您可以检查这些代码,它可能会有所帮助。
回答by arch
I get this bug in v110 (Visual studio 2012)-Compiler with the following code, which contains a wrong for-based loop.
我在 v110 (Visual Studio 2012)-Compiler 中遇到了这个错误,代码如下,其中包含一个错误的基于 for 的循环。
class A
{
int b;
};
int main(int argc, char* argv[])
{
A inst;
for (auto &i : inst)
{
}
return 0;
}
PS: v140 (Visual Studio 2015) shows the correct error:
PS:v140 (Visual Studio 2015) 显示正确的错误:
error C3312: no callable 'begin' function found for type 'A'
error C3312: no callable 'end' function found for type 'A'
回答by joseph o'malley
This error occured to me with C++ code with visual studio 2019 because I did not initialize my for-loop
correctly.
我在使用 Visual Studio 2019 的 C++ 代码时出现了这个错误,因为我没有for-loop
正确初始化 。
I did:
我做了:
for (int m; m < bytewidths + 1; m++) {}
for (int m; m < bytewidths + 1; m++) {}
rather than
而不是
for (int m=0; m < bytewidths + 1; m++) {}
for (int m=0; m < bytewidths + 1; m++) {}
I think that the way to fix this problem is to solve your recent code manually
我认为解决这个问题的方法是手动解决你最近的代码
回答by Kaleb Coberly
I got this error because I had misspelled a filename when I save it.*Incidentally, this save was after having inserted a few for loops, which sent me on a wild goose chase because those can be a source of this error as well.
我收到此错误是因为我在保存文件名时拼错了文件名。*顺便说一句,此保存是在插入了一些 for 循环之后,这让我大吃一惊,因为这些也可能是此错误的根源。
Sahbi's tip to look at the Output tab instead of just the general Error Code tab was very useful to me! I used the View menu to find and display the Output tab. It read the following:
Sahbi 的提示是查看输出选项卡,而不仅仅是一般的错误代码选项卡,这对我非常有用!我使用“查看”菜单查找并显示“输出”选项卡。它读到以下内容:
1>------ Build started: Project: C867 Performative Assessment, Configuration: Debug x64 ------ 1>Security_Student.cpp 1>c1xx: fatal error C1083: Cannot open source file: 'Security_Student.cpp': No such file or directory1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(429,5): error MSB6006: "CL.exe" exited with code 2. 1>Done building project "C867 Performative Assessment.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1>------ 构建开始:项目:C867 表演评估,配置:调试 x64 ------ 1>Security_Student.cpp 1>c1xx:致命错误 C1083:无法打开源文件:'Security_Student.cpp' : 没有这样的文件或目录1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(429,5): error MSB6006: "CL. exe”退出,代码为 2。 1>完成构建项目“C867 Performative Assessment.vcxproj”——失败。========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
I had saved the file as 'Securtiy_Student.cpp'.*
我已将文件保存为“Securtiy_Student.cpp”。*
I'd comment and upvote, but I don't have the rep yet.
我会评论和投票,但我还没有代表。
*This wouldn't have been a problem if the correctly spelled file had not also been deleted, because the compiler would have just ignored the misspelled file and used the previous version that was spelled correctly. However, I'm also getting a weird bug with Visual Studio where it will occasionally decide that one of my files is new when I try to save it and must "Save as..." But, then when I accept that action, it says the file already exists, but if I try to replace it with the save, it says you can't replace it because it's being used. So, the solution has been to paste the file into a text file, decline to save the file in the Visual Studio solution, erase the solution file, create a new file, paste the text into it, and save into the solution again. Anyway, I will post this problem elsewhere, but I wanted to give context for a sneaky way one could get the error in this thread from a misspelled filename.
*如果拼写正确的文件没有也被删除,这不会成为问题,因为编译器会忽略拼写错误的文件并使用拼写正确的先前版本。但是,我在 Visual Studio 中也遇到了一个奇怪的错误,当我尝试保存它时,它偶尔会决定我的一个文件是新的,并且必须“另存为...”但是,当我接受该操作时,它说文件已经存在,但如果我尝试用保存替换它,它说你不能替换它,因为它正在被使用。因此,解决方案是将文件粘贴到文本文件中,拒绝将文件保存在 Visual Studio 解决方案中,擦除解决方案文件,创建一个新文件,将文本粘贴到其中,然后再次保存到解决方案中。无论如何,我会在其他地方发布这个问题,
回答by Daniel Mehlber
Could also be an actually deleted header or source file, still listed in your project. Just check for such files.
也可能是实际删除的头文件或源文件,仍列在您的项目中。只需检查此类文件。
I deleted a Header and Source file using the system explorer. VS apparently doesn't recognize the absence of deleted files and tries to compile them, till you reload your project.
我使用系统资源管理器删除了一个头文件和源文件。VS 显然无法识别已删除文件的缺失并尝试编译它们,直到您重新加载项目。
Reloading the projectworked for me.
重新加载项目对我有用。
回答by Reed Hedges
Just wanted to add another case where this happens with Visual C++ 2019 (16.1.4):
只是想添加另一种情况,Visual C++ 2019 (16.1.4) 会发生这种情况:
...
...
char *s;
for(int i = 0; i < n; ++i)
{
if(i == 4) // we know n is going to be >= 4 but Visual C++ doesn't
s = getStringPointerFromSomewhere();
}
puts(s);
Visual C++ will print a message in the Output: potentially uninitialized local pointer variable 's' used
, but crash instead of reporting it as a normal error or warning (arguably this should be a warning, since it's perfectly legal C code).
Visual C++ 将在 Output: 中打印一条消息potentially uninitialized local pointer variable 's' used
,但会崩溃而不是将其报告为正常错误或警告(可以说这应该是警告,因为它是完全合法的 C 代码)。
The solution for this one is to just assign s to NULL after declared:
这个的解决方案是在声明后将 s 分配给 NULL:
char *s = NULL;
(So... just use the probably good habit of initializing pointers and other variables when declared I guess.)
(所以......我猜在声明时使用初始化指针和其他变量的可能好习惯。)
回答by Umair
You should check your your source files; it's probable that the compiler can't find the source file maybe you typed the wrong name. eg writing #include <isotream>
instead of #include <iostream>
will cause the problem.
你应该检查你的源文件;很可能编译器找不到源文件,也许你输入了错误的名称。例如写作#include <isotream>
而不是#include <iostream>
会导致问题。