Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and handling subfolder header files

Tags:

c++

cmake

I'm trying to set up my project to build several dynamic libraries that encompass its complete functionality. There are subfolders for each library. The subfolder libraries are dependent on each other, so they must reference functions from each other. I seem to have managed to get CMake to run without errors on the project, but when I go to build, I have trouble with my headers finding each other. It seems at build time, the include path isn't set up correctly. How can I fix this? Are there additional steps I need to take to set up the include path correctly?

The structure looks something like this

root
    CMakeLists.txt
    bin
    lib
    lib0
        CMakeLists.txt
        lib0.h
        lib0.cpp
    lib1
        CMakeLists.txt
        lib1.h
        lib1.cpp
    ...

In the CMakeLists.txt for the root directory I have declarations like this:

set(ROOT /blah/blah/root)    

include_directories(${ROOT}/lib0)
include_directories(${ROOT}/lib1)

add_subdirectory(lib0)
add_subdirectory(lib1)

In the CMakeLists.txt for the subfolders, I have:

set(lib0_SOURCES "")
list(APPEND lib0_SOURCES lib0.cpp)
add_library(lib0_lib ${lib0_SOURCES})

And my headers for the libraries look like (suppose this is lib0.h):

#include "lib1/lib1.h"
...

CMake runs fine with no errors, but when I go to compile, I get an error like:

In file included from /blah/blah/root/lib0/lib0.cpp:1:0:
/blah/blah/root/lib0/lib0.h:1:30: fatal error: lib1/lib1.h: No such file or directory
like image 954
dusktreader Avatar asked Aug 17 '12 20:08

dusktreader


People also ask

Do you include header files in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is CMakeLists txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

What is .CMake file?

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 told GCC to #include the file "lib1/lib1.h". When you build, CMake will ask to look for extra headers in "${ROOT}/lib0" and "${ROOT}/lib1"

So, GCC is trying "${ROOT}/lib0/lib1/lib1.h" and "${ROOT}/lib1/lib1/lib1.h" Yup, cant work.

To fix it:

  • you can use in your root CMakeLists.txt : include_directories(".")
  • keep your CMakeLists but #include the file "lib1.h"
  • remove the include_directories in CMakeLists and #include the file "../lib1/lib1.h"

IMO, I'd go for the first option!

like image 195
ablm Avatar answered Sep 19 '22 11:09

ablm