Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: C++ include relative to base directory

Tags:

c++

include

cmake

Say I have the following file structure:

project/
  - src/
    - main.cpp
    moduleA/
      - moduleA.cpp
      - moduleA.hpp
    moduleB/
      - moduleB.cpp
      - moduleB.hpp

Within moduleB I would like to use moduleA. As such, I would like to include moduleA.hpp but as a relative path to src. Aka, I would like to be able to write #include moduleA/moduleA.hpp to use moduleA. I have control over all files, and can put CMakeLists.txt or FindModule<>.cmake in any directories.

Note: I want to do this because there are files in each module that have the same name (in this case, parameters.hpp). Therefore, I would like to be able to include parameters.hpp relative to src directory for readability purposes, and to ensure that the correct parameters file is being included.

If this is confusing, or any other information is required, please ask. Thanks for all the help everyone!

like image 969
Sam Avatar asked May 09 '17 06:05

Sam


People also ask

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

How ADD includes in CMake?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory. The include directories are added to the INCLUDE_DIRECTORIES directory property for the current CMakeLists file.

What is the difference between Include_directories and Target_include_directories?

1, include_directories() is accessible for all the files in the source-tree 2, target_include_directories() is-only accessible for a specific target when compile.

What does CMake include do?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


1 Answers

You can use a structure similar to the following:

project/
  - CMakeLists.txt
  - src/
    - CMakeLists.txt
    - main.cpp
  - moduleA/
      - CMakeLists.txt
      - moduleA/
          - moduleA.cpp
          - moduleA.hpp
  - moduleB/
      - CMakeLists.txt
      - moduleB/
          - moduleB.cpp
          - moduleB.hpp

with the following files:


project/CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)

project(myProject)

add_subdirectory(moduleA)
add_subdirectory(moduleB)
add_subdirectory(src)

project/moduleX/CMakeLists.txt (X = A or B):

add_library(X STATIC ${moduleX_sources})
# The following line is very practical:
# it will allow you to automatically add the correct include directories with "target_link_libraries"
target_include_directories(X PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# only in CMakeLists.txt of module B, possibly with PRIVATE instead of PUBLIC
target_link_libraries(X PUBLIC A)

project/src/CMakeLists.txt:

add_executable(myExe ${exe_sources})
target_link_libraries(myExe A B) # <-- Automatically sets the correct include directories!!

With all this, you can use

#include "moduleX/moduleX.hpp"

See here for a similar question and more detailed answer.

like image 196
oLen Avatar answered Oct 02 '22 17:10

oLen