0

我对 CMake 没有任何经验,如果我在这里做错了什么,请先抱歉。

我的项目中的目录

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)

直到这部分一切正常,现在我尝试使用 googletest 添加一个测试。

我添加了一个名为“test”的内部目录,这就是 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}
        )

注意粗体线 (../src/check_dummy.cpp),这是我项目的主要 cpp 文件,我显然需要将其用于测试。不幸的是,我无法将其添加到 exe 中。没有它它可以工作(如果我注释掉这一行)并且可以编译(也是我现在只有 ASSERT_EQ(true, true) 通过的测试)。

但是当我取消注释该行并添加该 cpp 文件时,我收到此错误:

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]

请注意,我可能会用第一个编译相同的文件CMakeList.txt,它只是在“测试”目录中的某个文件中由于某种原因不起作用。

4

0 回答 0