0

According to the guide of integrating catch2 into cmake, I added the following to my CMakeLists.txt:

Include(FetchContent)

FetchContent_Declare(
    Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG        v3.0.0-preview3
)

FetchContent_MakeAvailable(Catch2)
    
add_executable(tests tests/test.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

which worked perfectly. However, when I am trying to write tests for my own files, I get a bunch of error LNK2019: unresolved external symbol error, such as:

[build] test.obj : error LNK2019: unresolved external symbol "public: __cdecl EventListener::EventListener(void)" (??0EventListener@@QEAA@XZ) referenced in function "public: static class EventListener & __cdecl EventListener::Instance(void)" (?Instance@EventListener@@SAAEAV1@XZ) [D:\projects\final-project-cyborgwizards\build\tests.vcxproj]
[build] test.obj : error LNK2019: unresolved external symbol "public: void __cdecl EventListener::BroadcastEvent(class Event)" (?BroadcastEvent@EventListener@@QEAAXVEvent@@@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____0(void)" (?____C_A_T_C_H____T_E_S_T____0@@YAXXZ) [D:\projects\final-project-cyborgwizards\build\tests.vcxproj]

According to the Microsoft guide, the problem might be that my own cpp files are not compiled. In /build/my-project-name.dir/Debug, all of my cpp files have a corresponding .obj file inside. However in /build/test.dir/Debug, there is only a .obj file test.obj. I know that I should write the files needed for compiling & linking in add_executable(), but writing something like add_executable(tests tests/test.cpp src/*.cpp) doesn't work. How can I add all cpp files?

Power_tile
  • 382
  • 4
  • 12
  • The problem of collecting all source files in the directory is not specific to testing. You could use `file(GLOB)` for that purpose. – Tsyvarev Nov 20 '21 at 21:59

0 Answers0