bobeff
bobeff

Reputation: 3749

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.

Upvotes: 4

Views: 3025

Answers (4)

JOE
JOE

Reputation: 373

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)

Upvotes: 0

IUnknown
IUnknown

Reputation: 659

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.

Upvotes: 0

bobeff
bobeff

Reputation: 3749

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.

Upvotes: 3

k.v.
k.v.

Reputation: 1223

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.

Upvotes: 1

Related Questions