简体   繁体   中英

cmake, clarification on include_directories

I'm trying to use cmake, but I'm new both to cmake and c++. I'd like to have a piece of code to use in different other programs. The folder structure is like this:

/modules/foo
/modules/foo/src
/modules/foo/include/foo
/cmake
/apps/bar
/apps/bar/src
/apps/bar/include

My problem is that in the bar main I can include (and compiling and running) both

#include "foo.h"
#include "foo/foo.h"

I think this is a symptom that something is wrong. What I'm looking for is something like

#include "foo.h" // only inside the foo source code

#include "foo/foo.h" // outside

If I remove include_directories(include/foo) from /modules/foo/CMakeLists.txt I get a compiling error that says 'foo.h' file not found in /modules/foo/src/foo.cpp

While if I remove include_directories(${CMAKE_CURRENT_LIST_DIR}/../modules/foo/include) from FindFoo.cmake I get the error in 'foo/foo.h' file not found /apps/bar/src/main.cpp

What can I do? What is the common practice to resolve this kind of problems? Do I have to ignore the fact that in apps/bar/src/main.cpp I can include both foo.h and foo/foo.h ? Do I have to write the include_directories only once and use #include "foo/foo.h also in foo.cpp?

  • Create a separate directory foo with foo.h and foo.cpp. In foo.cpp put #include "foo.h" , possibly as first of all its includes
  • Add there a CMakeLists.txt file to create a foo library, add_library command.
  • Add include_directories(${CMAKE_SOURCE_DIR}) in the preamble of the main CMakeLists.txt
  • From everywhere else in the project, you can include your foo.h by specifying #include "foo/foo.h" . Whereever you do this, don't forget to link with the foo library with a target_link_libraries command.

Precious link: http://www.cmake.org/cmake/help/v2.8.11/cmake.html#section_Commands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM