0

I don't have any experience with CMake, so sorry ahead if there's something I'm doing completely wrong here.

The directories I have on my project

CMakeLists.txt:

INCLUDE_DIRECTORIES(BEFORE
                    ${PROJECT_SOURCE_DIR}/src
                    ${CMAKE_INSTALL_PREFIX}/include
                    ${CMAKE_INSTALL_PREFIX}/include/sensor
                    ${CMAKE_INSTALL_PREFIX}/include/valijson
                    )

FIND_LIBRARY(FMT_LIB NAMES fmt fmtd HINTS "${CMAKE_INSTALL_PREFIX}/lib")
FIND_LIBRARY(SENSOR_LIB sensor HINTS "${CMAKE_INSTALL_PREFIX}/lib")

ADD_EXECUTABLE(${PROJECT_NAME}
        src/check_dummy.h
        src/check_dummy.cpp
        )

INCLUDE("${SENSORINFRA_PREFIX}/cmake/sensor_main.cmake")
INCLUDE("${SENSORINFRA_PREFIX}/cmake/sensor_schemata.cmake")


TARGET_LINK_LIBRARIES(${PROJECT_NAME}
                      ${FMT_LIB}
                      ${SENSOR_LIB}
                      )

INSTALL(TARGETS ${PROJECT_NAME}
        DESTINATION sensors
        )

IF (BUILD_TESTS)
        MESSAGE(STATUS "Building tests")
        ENABLE_TESTING()
        ADD_SUBDIRECTORY(test)
        ADD_TEST(NAME ${PROJECT_NAME}_tests COMMAND test/${PROJECT_NAME}_tests)
ENDIF (BUILD_TESTS)

until this part everything worked, now I tried adding a test to it using googletest.

I added an inner directory named 'test', and this is it's CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 3.15)
PROJECT(checkdummytests_tests LANGUAGES CXX)

SET(CMAKE_CXX_STANDARD 11)
ADD_COMPILE_DEFINITIONS(CHECKDUMMY_SENSOR_TEST SENSOR_TEST)

SET(GOOGLETEST_PATH "../../BUILD/external_sources/src/googletest")

FIND_LIBRARY(SENSOR_LIB sensor HINTS "${CMAKE_INSTALL_PREFIX}/lib")

INCLUDE_DIRECTORIES(BEFORE
        ${CMAKE_INSTALL_PREFIX}/include/sensor
        ${FMT_PATH}/include/
        ${LOGGER_PATH}/src/
        ${PROJECT_SOURCE_DIR}/../src/
        )

ADD_SUBDIRECTORY("${GOOGLETEST_PATH}/" [EXCLUDE_FROM_ALL])

ADD_EXECUTABLE(${PROJECT_NAME}
        gtest_main.cpp
        ../src/check_dummy.h
        **../src/check_dummy.cpp**
         src/check_dummy_test_mock.h
        src/ut_first_test.cpp
        )

TARGET_LINK_LIBRARIES(${PROJECT_NAME}
        gtest
        gmock
        ${SENSOR_LIB}
        )

Notice the bolded line (../src/check_dummy.cpp), this is the main cpp file of my project, which I obviously need to use for the test. Unfortanley, I can't manage to add it to the exe. It works without it (if I comment this line out) and may be compiled (also the test I have for now that only does ASSERT_EQ(true, true) passes).

But when I uncomment that line and add that cpp file, I get this error:

check_dummy.obj : error LNK2019: unresolved external symbol "public: virtual class rapidjson::GenericDocument<struct rapidjson::UTF8<char>,class rapidjson::MemoryPoolAllocator<class rapidjson::CrtAllocator>,class rapidjson::CrtAllocator> const & __cdecl x::sensors::CheckDummySensor::getQueriesSchema(void)const " (?getQueriesSchema@CheckDummySensor@sensors@x@@UEBAAEBV?$GenericDocument@U?$UTF8@D@rapidjson@@V?$MemoryPoolAllocator@VCrtAllocator@rapidjson@@@2@VCrtAllocator@2@@rapidjson@@XZ) referenced in function "public: __cdecl x::sensors::CheckDummySensor::CheckDummySensor(void)" (??0CheckDummySensor@sensors@x@@QEAA@XZ) [C:\si\sensors\checkdummytests\cmake-build-relwithdebinfo\test\checkdummytests_tests.vcxproj]
check_dummy.obj : error LNK2019: unresolved external symbol "public: virtual class rapidjson::GenericDocument<struct rapidjson::UTF8<char>,class rapidjson::MemoryPoolAllocator<class rapidjson::CrtAllocator>,class rapidjson::CrtAllocator> const & __cdecl x::sensors::CheckDummySensor::getSessionSchema(void)const " (?getSessionSchema@CheckDummySensor@sensors@x@@UEBAAEBV?$GenericDocument@U?$UTF8@D@rapidjson@@V?$MemoryPoolAllocator@VCrtAllocator@rapidjson@@@2@VCrtAllocator@2@@rapidjson@@XZ) referenced in function "public: __cdecl x::sensors::CheckDummySensor::CheckDummySensor(void)" (??0CheckDummySensor@sensors@x@@QEAA@XZ) [C:\si\sensors\checkdummytests\cmake-build-relwithdebinfo\test\checkdummytests_tests.vcxproj]

Notice that I may compile the same file with the first CMakeList.txt, it only doesn't work for some reason in the one inside 'test' directory.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Dolev
  • 1
  • 2
    `unresolved exernal symbol ... rapidjson::GenericDocument` sounds like you need to link with rapidjson. `now I tried adding a test to it using googletest.` sure, could be that gtest requires rapidjson. – KamilCuk Dec 05 '21 at 13:13
  • It's not the gtetst, it's 2 functions which I use. The thing is that I also use that in the CMake in the outer directory it works. Only when compiling the same file from the CMakeList.txt inside 'test' directory, this error occurs – Dolev Dec 05 '21 at 13:53
  • The source file `check_dummy.cpp` uses functions from rapidjson, so you need to link with that library **both** your main executable and the test one. If you `main` executable is successfully linked, then some line of your code actually links it with `rapidjson`. I don't think that `fmt` library internally uses rapidjson, so it is probably script `${SENSORINFRA_PREFIX}/cmake/sensor_main.cmake` or `${SENSORINFRA_PREFIX}/cmake/sensor_schemata.cmake` which performs the linkage. Since you don't show content of these scripts, we cannot be sure about what they do. – Tsyvarev Dec 05 '21 at 14:52

0 Answers0