简体   繁体   中英

error MSB6006: “CL.exe” exited with code 2

I'm writing with visual c++ and when I compile this error occures:

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!

I got the same error when I forgot the return statement in the following method:

char SpiRAM::write_byte(int address, char data_byte)
{
    assert(address >= 0);
    assert(address < SRAM_SIZE);

    _sram[address] = data_byte;

    return data_byte;
}

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 .

In the case of another answerer here that exact tab would've shown: 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

This happened me for a variety of different reasons:

1) I forgot to add a return statement to a non-void function.

2) I tried to use an uninitialized pointer.

3) I wrote a loop like for(int i=i;...) instead of for(int i=0;...)

You can check your code for these and it may help.

I get this bug in v110 (Visual studio 2012)-Compiler with the following code, which contains a wrong for-based loop.

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:

error C3312: no callable 'begin' function found for type 'A'
error C3312: no callable 'end' function found for type 'A'

This error occured to me with C++ code with visual studio 2019 because I did not initialize my for-loop correctly.

I did:

for (int m; m < bytewidths + 1; m++) {}

rather than

for (int m=0; m < bytewidths + 1; m++) {}

I think that the way to fix this problem is to solve your recent code manually

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.

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:

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 directory 1>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 ==========

I had saved the file as '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.

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.

Reloading the project worked for me.

Just wanted to add another case where this happens with 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).

The solution for this one is to just assign s to NULL after declared:

char *s = NULL;

(So... just use the probably good habit of initializing pointers and other variables when declared I guess.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM