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

Catch unit test library linking error


I am trying to use CATCH unit test suite linked below. https://github.com/philsquared/Catch

However, I cannot succeed to make it right. The main.cpp and test.cpp are as follows.

//main.cpp
#define CATCH_CONFIG_MAIN 
#include "catch.hpp"

//test.cpp
#include "catch.hpp"

TEST_CASE("TESTTest", "") {
    CHECK(1 != 2 );
}

When this two files are located in the same folder, I could get a desired result. However, I moved the test.cpp to the subdirectory named test. It does not work anymore as expected but generates linking errors.

My cmake setting is described below.

project(catchTest)
cmake_minimum_required(VERSION 2.8)

file(GLOB_RECURSE INCS "./*.cpp")
add_executable(${PROJECT_NAME} main.cpp ${INCS})

include_directories(.)

file(GLOB_RECURSE INCS "./*.cpp") was added to include every cpp source files located in the subdirectories. and include_directories(.) was included to let them know the definition of catch.hpp.

I am pretty shure I've done something wrong but I don't know how to fix it. Please advise me to solve this problem.

It was run on Windows, compiled using mingw gcc-4.9.1 and generated by cmake ninja generator.

EDIT : I added the first few lines of error messages.

FAILED: cmd.exe /c cd . && C:\MinGW\bin\g++.exe CMakeFiles/catchTest.dir/main.cpp.obj CMakeFiles/catchTest.dir/main.cpp.obj CMakeFiles/catchTest.dir/test/testTest.cpp.obj -o catchTest.exe -Wl,--out-implib,libcatchTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd . CMakeFiles/catchTest.dir/main.cpp.obj:main.cpp:(.text+0x0): multiple definition of `Catch::getResultCapture()'

CMakeFiles/catchTest.dir/main.cpp.obj:main.cpp:(.text+0x0): first defined here


Solution

  • Your CMakeLists.txt is wrong, it includes main twice. If you change the add_executable-statement like this it works for me:

    add_executable(${PROJECT_NAME} main.cpp test.cpp )
    

    Hope that helps.

    Kim