0

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?

nkint
  • 11,513
  • 31
  • 103
  • 174
  • 1
    IMO when using src and include directories, the src should contain exactly the source (.cpp) files and the include should contain the headers (.h), where the headers typically have subdirectory structures. Why do you have foo.h in a source folder (/modules/foo/src/foo.h)? The advantage of separating headers and source is to be able to ship just the headers (i.e. include dir) for use in other projects. – Ralf Oct 09 '13 at 19:05
  • sorry the error. i am not yet used to think about those structures and it is quite confusing to me take care about those different includes, source, cmakelists etc – nkint Oct 09 '13 at 21:10
  • It really is about personal preference how you structure your project. Your sources and headers can be in the same directory if you want, with the main advantage of separation as stated. – Ralf Oct 10 '13 at 06:53

1 Answers1

1
  • 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

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • ok so in the same directory.. but so why a lot of cpp libs have the structure: `mylib/include/mylib` and `mylib/src` ? – nkint Oct 10 '13 at 21:17
  • @nkint As someone already said, it's a matter of taste, and a little bit on the context of what you are doing: if you are writing this sublibrary and you will be the one using this sublibrary, I suggest that you keep and header and sources together. If this is a library that you are going to distribute as a binary you might want to put in an include directory the headers with the interface. Again, it's a matter of taste, and you are going to see many possible motivations check this and related: http://stackoverflow.com/questions/13967296/why-place-headers-in-a-separate-directory – Antonio Oct 11 '13 at 06:58