我有一个包含两个文件夹的C ++ Project文件夹。
包含多个子文件夹和CmakeList.txt文件的C ++ src文件夹。 src文件夹包含main.cpp文件。
**Cmakelist.txt src folder**
project (TestProject)
# Include all global directories and the current directory
include_directories(${GLOBAL_INCLUDES} ".")
link_directories(${GLOBAL_LIB_DIRS})
file(GLOB_RECURSE GLOBAL_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp")
add_executable(${PROJECT_NAME} ${GLOBAL_SOURCES})
target_link_libraries(${PROJECT_NAME} ${GLOBAL_LIBS})
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d")
install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
一个C ++构建标准外部文件夹ExtLib,包含多个子文件夹和一个用于编译和CmakeList.txt文件的build.sh脚本。 src文件夹中的.cpp文件链接(包括标题(.h)文件)到此ExtLib文件夹。
Project文件夹包含一个main build.sh文件,该文件编译项目并创建包含调试和发布配置的构建文件夹。它还包含一个CmakeList.txt文件,我想在执行build.sh文件时会执行该文件。我需要在这个项目上执行的任务是创建一个新的测试文件夹并添加一个文件(RunAllTests.cpp),该文件对src文件夹.cpp文件中编写的代码执行单元测试(使用catch单元测试)。
我面临的问题:
第一个问题是当我在我的tests文件夹中创建一个源测试文件,并尝试从src文件夹中包含头文件时,编译器抛出一个致命的错误,没有找到这样的(.h)文件如果我在测试文件夹源文件中包含一个文件,其中包含来自ExtLib文件夹的头文件 - 那么我在测试文件夹文件中的ExtLib文件夹中通过src文件夹文件链接文件时出错。我的所有Cmakelist文件都写得正确吗?
**Cmakelist.txt tests folder**
cmake_minimum_required(VERSION 3.5)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
include_directories(../src)
include_directories(../ExtLib)
link_directories(${GLOBAL_LIB_DIRS})
add_library(AConfigTest ./aconfigtest/AConfigTest.cpp)
add_library(AClientTest ./aclienttest/AClientTest.cpp)
add_library(AServerTest ./aservertest/AServerTest.cpp)
file(GLOB_RECURSE GLOBAL_SOURCES
"${PROJECT_SOURCE_DIR}/src/AConfig/*.cpp"
"${PROJECT_SOURCE_DIR}/src/AClient/*.cpp"
"${PROJECT_SOURCE_DIR}/src/AServer/*.cpp"
"${PROJECT_SOURCE_DIR}/src/ErrorLogger.cpp"
)
add_executable(tests RunAllTests.cpp ${GLOBAL_SOURCES})
set_target_properties(tests PROPERTIES DEBUG_POSTFIX "d")
target_link_libraries(tests ${GLOBAL_LIBS}
AConfigTest
AClientTest
AServerTest
)
第二个问题是,如果我能够成功地包含来自其他src或ExtLib文件夹的文件,我怎样才能运行测试源文件RunAllTests.cpp来执行我的测试用例?我的所有Cmakelist文件都写得正确吗?
自从3周以来我一直坚持这个问题,我迫切需要一个解决方案。以下是编译器错误:
编译错误:
make[2]: Entering directory '/home/Caspian/TestProject/build/linux/debug'
[ 56%] Building CXX object tests/CMakeFiles/tests.dir/RunAllTests.cpp.o
cd /home/Caspian/TestProject/build/linux/debug/tests && /usr/bin/c++ -I/home/Caspian/TestProject/tests/../src -I/home/Caspian/TestProject/tests/../ExtLib -g -o CMakeFiles/tests.dir/RunAllTests.cpp.o -c /home/Caspian/TestProject/tests/RunAllTests.cpp
In file included from /home/Caspian/TestProject/tests/RunAllTests.cpp:14:0:
/home/Caspian/TestProject/tests/../src/AConfig/Config.h:16:29: fatal error: xxplatformlayer.h: No such file or directory
compilation terminated.
tests/CMakeFiles/tests.dir/build.make:62: recipe for target 'tests/CMakeFiles/tests.dir/RunAllTests.cpp.o' failed
make[2]: *** [tests/CMakeFiles/tests.dir/RunAllTests.cpp.o] Error 1
make[2]: Leaving directory '/home/Caspian/TestProject/build/linux/debug'
CMakeFiles/Makefile2:277: recipe for target 'tests/CMakeFiles/tests.dir/all' failed
make[1]: *** [tests/CMakeFiles/tests.dir/all] Error 2
make[1]: Leaving directory '/home/Caspian/TestProject/build/linux/debug'
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
make failed.
项目层次结构如下所示:
/TestProject
│ build.sh
│ CMakeList.txt
/ExtLib
│ build.sh
│ CMakeList.txt
│ debug
│ release
│ A/include/..(multiple subfolders and .cpp files)
/src
│ build.sh
│ CMakeList.txt
│ AServer(multiple subfolders and .cpp files)
│ AClient(multiple subfolders and .cpp files)
│ AConfigt(multiple subfolders and .cpp files)
/tests
│ CMakeList.txt
│ RunAllTests.cpp
│ aservertest/AServerTest.cpp (testing src AServer)
│ aclienttest/AClientTest.cpp (testing src AClient)
│ aconfigtest/AConfigTest.cpp (testing src AConfig)