Mohammad AhmadiAkbari Mohammad AhmadiAkbari - 1 year ago 74
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


    • CMakeLists.txt (1)

    • Light

      • CMakeLists.txt (2)

      • header_files.h


    • Camera

      • CMakeLists.txt (3)

      • header_files.h


    • Core

      • CMakeLists.txt (4)

      • header_files.h


These are contents of CMakeLists.txt files:

CMakeLists.txt (1)

target_link_libraries(GraphicsEngineLib LightLib CameraLib CoreLib)

CMakeLists.txt (2)


CMakeLists.txt (3)


CMakeLists.txt (4)


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 Source

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



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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download