0

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)
  • You should have an add_subdirectory() in your root CMakeLists.txt for each subdirectory you have a CMakeLists.txt in. You should not have a project (TestProject) in any of the CMakeLists.txt that are not the single root CMakeLists.txt. – drescherjm Apr 06 '17 at 18:46
  • @drescherjm I have all the same conditions which you mention in your post. I do not understand why if i exclude add_subdirectory(tests), then the compilation is 100% but if I include tests,then the src .cpp files gives an error stating - "no such header file found" (the –  Apr 06 '17 at 21:28
  • Compiler invocation uses two options with include directories: `-I/home/Caspian/TestProject/tests/../src` and `-I/home/Caspian/TestProject/tests/../libraries`. They do not correspond to `CMakeLists.txt` file in your post. – Tsyvarev Apr 07 '17 at 12:02
  • @Tsyvarev Can you please elaborate on it. I am a pretty new to using cmakelists. Your help would be deeply appreciated.ALso here libraries is the ExtLib folder. –  Apr 07 '17 at 12:05
  • 1
    The error log shows the compiler invocation line (`/usr/bin/c++ ...`). In this line all compiler parameters should correspond to ones in `tests/CMakeLists.txt` file. But it isn't true for your case: your have 6 (in total) include directories in `tests/CMakeLists.txt`, but there are only 2 `-I` options passed to the compiler. Looks like the **actual** `tests/CMakeLists.txt` **differs from the one you show us**. – Tsyvarev Apr 07 '17 at 12:15
  • Yes you are correct i just replaced cmakelist lines 3,4 with: #include_directories(../src) #include_directories(../ExtLib). But because I was trying to complie my project, I was trying to make changes and run it. But with both it does not work. –  Apr 07 '17 at 12:28
  • 1
    Then show **actual error log**, which corresponds to the `tests/CMakeLists.txt` in your post. – Tsyvarev Apr 07 '17 at 12:40
  • @Tsyvarev Added the new Error log and the used CMakelist.txt file. –  Apr 07 '17 at 14:00
  • So, **which include directory** contains `xxplatformlayer.h` header file, `/home/Caspian/TestProject/src` or `/home/Caspian/TestProject/ExtLib`? If none of them contains it, you need to add appropriate directory to `include_directories()` call in your `tests/CMakeLists.txt`. Note, that given script misses `include_directories(${GLOBAL_INCLUDES} ".")`, which is used in another script, `src/CMakeLists.txt`. – Tsyvarev Apr 07 '17 at 14:51
  • **xxplatformlayer.h** is contained in ExtLib folder. I added the include_directories(${GLOBAL_INCLUDES} ".") in the tests CMakelist file. And after this I can have a successful compilation. But now I am facing some other issue. The executable **test** which i created, when I try to run does not execute and gives an error: The error is: **./tests: error while loading shared libraries: libuastack.so: cannot open shared object file: No such file or directory**. –  Apr 07 '17 at 19:24

0 Answers0