Catch2 v3 linking errors on Windows
Hi,
I have a project with a bunch of unit-test made with Catch2, on Linux I have no problems, it compiles, link and run flawlessly.
On windows I have several linking errors due to Catch2.
I've installed catch 2 following the git\cmake integration guide:
Catch2/docs/cmake-integration.md at devel · catchorg/Catch2 · GitHub
And in fact I can see Catch2 install under “C:\Program Files (x86)\Catch2”
here's the part of my project cmake file where I link against Catch2:
# Catch2 v3 is the Unit Test framework of choice for this project
find_package(Catch2 3 REQUIRED)
# CPP_TEST_FILES lists the unit-test files.
file(GLOB_RECURSE CPP_TEST_FILES ${PROJECT_SOURCE_DIR}/test/*.cpp)
# TEST_EXECUTABLE_NAME contains the target unit-test name
set(TEST_EXECUTABLE_NAME "${CORE_LIBRARY_NAME}-tests")
# Create the target executable for the unit tests
add_executable(${TEST_EXECUTABLE_NAME} ${CPP_TEST_FILES})
# Link the for real core library and the Catch2 default main
target_link_libraries(${TEST_EXECUTABLE_NAME} PRIVATE ${CORE_LIBRARY_NAME})
target_link_libraries(${TEST_EXECUTABLE_NAME} PRIVATE Catch2::Catch2WithMain)
# Set the include paths for the unit tests
target_include_directories(${TEST_EXECUTABLE_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/test/extern)
target_include_directories(${TEST_EXECUTABLE_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
Cmake find the package no problem, but the project fails with a TON of linking errors some of them just to illustrate my point:
o@Catch@@V?$allocator@USectionInfo@Catch@@@std@@@std@@QEAA?AV?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@USectionInfo@Catch@@@std@@@std@@@2@XZ'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Program Files (x86)/Catch2/lib/Catch2d.lib(Catch2.dir/Debug/catch_reporter_console.obj):(.text$mn+0x140d): undefined reference to `??E?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@USectionInfo@Catch@@@std@@@std@@@std@@QEAAAEAV01@XZ'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Program Files (x86)/Catch2/lib/Catch2d.lib(Catch2.dir/Debug/catch_reporter_console.obj):(.text$mn+0x141f): undefined reference to `??9?$_Vector_const_iterator@V?$_Vector_val@U?$_Simple_types@USectionInfo@Catch@@@std@@@std@@@std@@QEBA_NAEBV01@@Z'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Program Files (x86)/Catch2/lib/Catch2d.lib(Catch2.dir/Debug/catch_reporter_console.obj):(.text$mn+0x1430): undefined reference to `??C?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@USectionInfo@Catch@@@std@@@std@@@std@@QEBAPEAUSectionInfo@Catch@@XZ'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Program Files (x86)/Catch2/lib/Catch2d.lib(Catch2.dir/Debug/catch_reporter_console.obj):(.text$mn+0x1455): undefined reference to `??1?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@USectionInfo@Catch@@@std@@@std@@@std@@QEAA@XZ'For completeness, others pointing directly at the code using Catch2 macros:
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Workspace/projects/cpp/for-real-engine/core/test/math/math-hash-functions-test.cpp:7: undefined reference to `Catch::Section::Section(Catch::SourceLineInfo const&, Catch::StringRef, char const*)'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Workspace/projects/cpp/for-real-engine/core/test/math/math-hash-functions-test.cpp:7: undefined reference to `Catch::Section::operator bool() const'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Workspace/projects/cpp/for-real-engine/core/test/math/math-hash-functions-test.cpp:9: undefined reference to `Catch::AssertionHandler::AssertionHandler(Catch::StringRef, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)'
C:\Program Files\JetBrains\CLion 2025.1.3\bin\mingw\bin/ld.exe: C:/Workspace/projects/cpp/for-real-engine/core/test/math/math-hash-functions-test.cpp:9: undefined reference to `Catch::AssertionHandler::handleExpr(Catch::ITransientExpression const&)'
Can anyone help me with this problem? I repeat, it compiles, link and run under linux.
请先登录再写评论。
I fixed the problem, command-line cmake was building catch2 using MSVC compiler meanwhile the clion project was using GCC, thus the errors while linking, I've compiled everything using the same compiler and the errors went away.
thanks anyway, cheers