4

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.

bobeff
  • 3,543
  • 3
  • 34
  • 62
  • You are missing some Catch library(s) obviously. If you can't find them in the linker section of your VS project; then try looking in the headers of Catch for `#pragma comment( lib, "name-of-catch-library" )` which will cause the named library to be automatically picked up by the linker. – Richard Critten Jun 21 '17 at 13:30
  • 2
    @Richard Critten As I said the catch library is header only and there is no **libs** for it. – bobeff Jun 21 '17 at 13:32
  • 2
    Did you use "official" instruction for CMake integration? https://github.com/philsquared/Catch/blob/master/docs/build-systems.md (see CMake section) – k.v. Jun 21 '17 at 13:32
  • 2
    Could you please give a [mcve] including some `CMakeLists.txt` code? – Florian Jun 21 '17 at 14:44

4 Answers4

3

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.

bobeff
  • 3,543
  • 3
  • 34
  • 62
1

It's a little bit hard to deduce a concrete problem from the context you provided, but here is an official Catch instruction for CMake integration.

In my experience using it with Visual Studio - integration went smoothly.

k.v.
  • 1,193
  • 8
  • 11
  • +1 Thank you for pointing me to the official **Catch** documentation about **CMake** integration, but I didn't find there anything useful about my problem. Defining **Catch** as interface library and adding it as dependency in **target_link_libraries** is pointless because **Catch** is header only library and no separate build and linking are required. Nevertheless I tried it and this didn't change anything. – bobeff Jun 21 '17 at 13:54
  • @bobeff Did you try to create a catch unit testing project in your new CMake environment *from scratch* using exact instructions? Maybe it will help you to find the exact problem, because currently there is not enough information. And doing it from scratch may help you to find a step where it breaks. – k.v. Jun 22 '17 at 06:24
0

When I try to use Catch with the Precompiled Header option enabled in my test project, I end up with linker errors LNK2019.

I still use stdafx.h in my project and disable the Precompiled header option in order for the project to build.

Right-click project -> Configuration Properties -> C/C++->Precompiled Headers -> Precompiled Header -> Not Using Precompiled Headers.

IUnknown
  • 559
  • 6
  • 12
0

I have the same error and I solved it by linking the target to Catch2WithMain

target_link_libraries( ${PROJECT_TEST} Catch2 Catch2WithMain)
set_property(TARGET ${PROJECT_TEST} PROPERTY CXX_STANDARD 14)
set_property(TARGET ${PROJECT_TEST} PROPERTY CXX_EXTENSIONS OFF)
JOE
  • 353
  • 2
  • 11