CmakeList - Not able to create correct CMakelist for C++ Project subfolders

こ雲淡風輕ζ 提交于 2019-12-12 02:26:00

问题


I have a C++ Project folder which contains two folders.

  1. a C++ src folder with multiple sub-folders and CmakeList.txt file. The src folder contains main.cpp file.

    **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})
    
  2. a C++ build standard external folder ExtLib containing multiple subfolders and a build.sh script for its compilation and CmakeList.txt file. The .cpp files from the src folder are linked (includes header(.h) files) to this ExtLib folder.

The Project folder contains a main build.sh file which compiles the project and creates a build folder including debug and release configurations. It also contains a CmakeList.txt file which I suppose gets executed when build.sh file is executed. The task that I need to perform on this project is to create a new tests folder and add a file (RunAllTests.cpp) which performs unit-testing (using catch unit-test) on the code written in the src folder .cpp files.

Problem I face:

  • The first problem is when I am creating a source test file in my tests folder, and trying to include header files from src folder, the compiler throws a fatal error that no such (.h) files found which means if i include a file in tests folder source file, which includes a header file from ExtLib folder - then i get error in linking files from ExtLib folder in tests folder files via src folder files. Are my all Cmakelist files correctly written?

    **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
    )
    
  • The second question is, if by some how I am able to include files from other src or ExtLib folders successfully, how can I be able to run the test source file RunAllTests.cpp for executing my test cases? Are my all Cmakelist files correctly written?

I am stucked on this problem since 3 weeks, I desperately need a solution for this. Below is the compiler error:

Compiler error:

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.

The project heirarchy is shown below:

/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)

来源:https://stackoverflow.com/questions/43250341/cmakelist-not-able-to-create-correct-cmakelist-for-c-project-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!