Mohammad AhmadiAkbari Mohammad AhmadiAkbari - 4 months ago 9
C++ Question

using cmake to make header files descendants of the project's source directory

As in Google C++ Style Guide is mentioned all of a project's header files should be listed as descendants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). How can I do that in my project that is shortly described below.

My project directory hierarchy is like this:


  • GraphicsEngine


    • header_files.h

    • source_files.cc

    • CMakeLists.txt (1)

    • Light


      • CMakeLists.txt (2)

      • header_files.h

      • source_files.cc


    • Camera


      • CMakeLists.txt (3)

      • header_files.h

      • source_files.cc


    • Core


      • CMakeLists.txt (4)

      • header_files.h

      • source_files.cc





These are contents of CMakeLists.txt files:

CMakeLists.txt (1)

add_library(GraphicsEngineLib source_files.cc)
target_link_libraries(GraphicsEngineLib LightLib CameraLib CoreLib)
add_subdirectory(Light)
add_subdirectory(Camera)
add_subdirectory(Core)


CMakeLists.txt (2)

add_library(LightLib source_files.cc)


CMakeLists.txt (3)

add_library(CameraLib source_files.cc)


CMakeLists.txt (4)

add_library(CoreLib source_files.cc)


Now when for example I want to include header files from Camera folder in to files in Core folder, I have to use ../Camera/header_file.h but I want to use GraphicsEngine/Camera/header_file.h. How can I do this?

Answer

What I have done in the past is to set this in the top level CMakeLists.txt (which should be in your GraphicsEngine directory):

SET(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/..")

INCLUDE_DIRECTORIES(
  ${PROJECT_ROOT}
)

where according to this, CMAKE_CURRENT_SOURCE_DIR is

this is the directory where the currently processed CMakeLists.txt is located in

Note that by defining Project_Root in this way, your GraphicsEngine project can also #include headers from sister projects to GraphicsEngine.

Hope this helps.