c++templatescrashmsvc12cl

error MSB6006: "CL.exe" exited with code 1 after adding template function or class


Asking this question preemtively because there is not much to be found about this error code. It is rather trivial to solve, but cost me a lot of time to diagnose because no proper error message is given.

So what happened?

The problem were syntax errors in my template functions. Who could have guessed that.

However, it was hard to find out, because these did not get reported. See my answer for how to determine which functions are defective.

The templated functions were in a .cpp included in the header, however, defining them completely in the header did not make any difference.

// foo.h

template <typename T>
void foo();

...

#include "foo.cpp"



// foo.cpp

template <typename T>
void foo() 
{
...
}

Solution

  • First you need to compile all .cpp files that include the templated functions seperately (select one in the project explorer, right click and "compile").

    For me, the first hint was that some of them compiled, while for others cl.exe crashed.

    The next step was to create a bogus.cpp file with just one function, where one by one I added calls to every templated function I created. After adding one: recompile. This went well until I got to the defective one, now the bogus.cppalso crashed cl.exe. Jackpot.

    The last job was fixing the syntax error, which is annoying without error messages, but once this is done, bogus.cpp will compile again. Return to adding more function calls there until you have everything covered.

    Hope I could help.