Search code examples
c++unit-testingbuildcmakecatch-unit-test

Unresolved external symbol with Catch library


I'm migrating some projects to use CMake build system. Now I'm adding project with some unit tests using the Catch library. It is header only library. The old Visual Studio project builds fine, but the new CMake project gives unresolved external symbol linker error. I have defined CATCH_CONFIG_MAIN in one of my source files. There are added all cpp files from other projects which are needed for the tests and all libraries on which other tested projects depend are linked. Despite this I have unresolved external symbol error only with generated from CMake project:

ChipCountTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Catch::SourceLineInfo::SourceLineInfo(char const *,unsigned int)" (??0SourceLineInfo@Catch@@QAE@PBDI@Z) referenced in function "void __cdecl `anonymous namespace'::`dynamic initializer for 'autoRegistrar1''(void)" (??__EautoRegistrar1@?A0xb4291ec5@@YAXXZ)
1>FlyingChipRewardCalculatorUT.obj : error LNK2001: unresolved external symbol "public: __thiscall Catch::SourceLineInfo::SourceLineInfo(char const *,unsigned int)" (??0SourceLineInfo@Catch@@QAE@PBDI@Z)

Obviously I'm missing to add some configuration from vcxproj to CMakeLists.txt but I'm currently can't figure it out.


Solution

  • In one of my files I have:

    #define CATCH_CONFIG_MAIN
    #include <catch.hpp>
    

    but I also using CMake macro for adding precompiled header to the project:

    add_precompiled_header (${TARGET_NAME}
      ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.h
      ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.cpp)
    

    This macro forcefully includes precompiled header in all files, but in it I have #include <catch.hpp> without #define CATCH_CONFIG_MAIN which is needed by all files except one.

    I added option to the macro to pass list of files in which not to be included precompiled header and this resolves the issue.