1

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

  • a C++ src folder with multiple sub-folders and CmakeList.txt file. The src folder contains main.cpp file.
  • a C++ build standard external folder ExtLib containing multipe subfolders and a build.sh script for its compilation and CmakeList.txt file. The .cpp files 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 (runtests.cpp) which performs unit-testing (using catch unit-test) on the code written in the src folder .cpp files.

Problem I face:

  1. 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.(from src folder). Also sometimes the problem occurs in the src folder files, as they are unable to locate ExtLib folder files.

  2. 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 runtests.cpp for executing my test cases?

The project heirarchy is shown below:

TestProject │ build.sh │ CMakeList.txt
└───ExtLib │ │ buildscript.sh │ │ CMakeList.txt │ └───subfolder1 │ └───subfolder2(containing sub-folders) └───src │ │ main.cpp │ │ CMakeList.txt │ └───folder1 │ └───folder2(containing sub-folders) └───tests │ runtests.cpp │ catch.hpp

Do I need to include a CMakelist.txt file for my tests folder too? How to make it if I have to? I am very new to this field. Hope you guys can help. Thanks Rg

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77
  • 1
    ***Do I need to include a CMakelist.txt file for my tests folder too?*** I do. ***How to make it if I have to?*** Its the same as any other CMakelist.txt that is not the root (meaning you don't have a `project()`) with the addition of the code to run your tests. – drescherjm Apr 05 '17 at 14:48
  • 1
    ***the compiler throws a fatal error that no such (.h) files found.(*** put `include_directories(../src)` in your `CMakeLists.txt` that is in your tests folder. – drescherjm Apr 05 '17 at 14:50
  • Can you give me a sample of it with respect to the heirarchy I showed including catch.hpp and runtests.cpp file –  Apr 05 '17 at 14:53
  • Just create a `CMakeList.txt` in your tests folder like you have in your src and ExtLib folders. – drescherjm Apr 05 '17 at 14:54
  • cmake_minimum_required(VERSION 3.5) include_directories(../src) include_directories(../ExtLib) file(GLOB_RECURSE GLOBAL_SOURCES "${PROJECT_SOURCE_DIR}/tests/catch.hpp") add_executable(tests runtests.cpp) –  Apr 05 '17 at 14:57
  • file(GLOB_RECURSE GLOBAL_SOURCES "${PROJECT_NAME}/tests/catch.hpp") . Is this correct now? –  Apr 05 '17 at 15:05
  • You probably should not use GLOB at all. The CMake recommended way is to list all source files individually. – drescherjm Apr 05 '17 at 15:12
  • It's difficult to help because I still don't see what could cause your problem or how `runtests.cpp` was being compiled if it was not in a `CMakeLists.txt` in the `tests` folder. – drescherjm Apr 05 '17 at 15:16
  • Below is the error. **uaplatformlayer.h** is the file from ExtLib. In file included from /home/XXX/TestProject /tests/aggrconfig/AggrConfigTest.cpp:2:0: /home/XXX/ddcl-TestProject /tests/../src/AggrConfig/Config.h:16:29: fatal error: uaplatformlayer.h: No such file or directory tests/CMakeFiles/AggrConfigTest.dir/build.make:62: recipe for target 'tests/CMakeFiles/AggrConfigTest.dir/aggrconfig/AggrConfigTest.cpp.o' failed –  Apr 05 '17 at 15:22
  • Is `uaplatformlayer.h` in the `src` or `ExtLib` folder? – drescherjm Apr 05 '17 at 15:24
  • is in the ExtLib folder –  Apr 05 '17 at 15:26
  • Also include_directories() can take a list of things. Like this `include_directories(../src ../ExtLib ../ExtLib/subfolder1)` – drescherjm Apr 05 '17 at 15:27
  • Are you sure the `CMakeLists.txt` you have in `tests` is being used at all. I mean if `runtests.cpp` was being compiled before you added the `CMakeLists.txt` then the `CMakeLists.txt` in `tests` is not being used. – drescherjm Apr 05 '17 at 15:28
  • Yes you are correct. Even I think so. But then what could be the problem? –  Apr 05 '17 at 15:32
  • But when i made some error in Cmakelist, the compiler showed me I Think it works –  Apr 05 '17 at 15:39
  • @drescherjm For more information please check this question: http://stackoverflow.com/questions/43250341/cmakelist-not-able-to-create-correct-cmakelist-for-c-project-subfolders –  Apr 06 '17 at 10:03

0 Answers0