CMake Header and Subfolder Processing Files

I am trying to configure my project to create several dynamic libraries that encompass its full functionality. There are subfolders for each library. Subfolder libraries are interdependent, so they must reference functions from each other. It seems that I managed to get CMake to work without errors in the project, but when I go to the build, I have problems with my headers that found each other. It seems that the switch path is not configured correctly during assembly. How can i fix this? Are additional steps required to properly configure the enable path?

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 CMakeLists.txt for the root directory, I have these declarations:

 set(ROOT /blah/blah/root) include_directories(${ROOT}/lib0) include_directories(${ROOT}/lib1) add_subdirectory(lib0) add_subdirectory(lib1) 

In CMakeLists.txt for subfolders, I have:

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

And my library headers look like (suppose it's lib0.h):

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

CMake works fine, without errors, but when I go to compilation, I get an error, for example:

 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 
+4
source share
3 answers

You told GCC #include the file "lib1/lib1.h" . When you create, CMake will ask you to find additional headers in "$ ROOT" / lib 0 and "$ ROOT" / lib 1 "

So, GCC is trying "$ ROOT" / lib 0 / lib1 / lib1.h "and" $ ROOT "/lib1/lib1/lib1.h" Yup, it doesn't work. "

To fix this:

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

IMO, I would choose the first option!

+7
source

You need to use a double naming scheme or specify the base directory as the include path:

 root CMakeLists.txt bin lib lib0 CMakeLists.txt lib0.cpp lib0 lib0.h lib1 CMakeLists.txt lib1.cpp lib1 lib1.h ... 
+1
source

I would ask CMake to simply consider the ROOT directory for C / C ++, including look-ups:

 set(ROOT /blah/blah/root) include_directories(${ROOT}) add_subdirectory(lib0) add_subdirectory(lib1) 

Then in C / C ++ use angular rejects ('<' and '>') instead of double quotes ('' '):

 #include <lib1/lib1.h> 
+1
source

All Articles