35

I'm learning CMake for building C++ code, and struggling with the following concept. On my root level directory I have some cpp files and a CMakeLists.txt that succesfully generates some thrift code in a gen-cpp directory. My root level CMakeLists.txt contains :

include_directories("path-to-root"/gen-cpp). (along with the relevant thrift auto-generating and includes.

Everything compiles ok but I get run time dynamic library linked errors for undefined symbol referencing a class defined in the gen-cpp directory. When I move the files in the directory to the root level, it runs fine. what am I missing? (I had also adjusted the #include in the root level cpp directorie s to point to "path-to-root"/gen-cpp).

Is this a misunderstanding of using include_directory, where I should be using add_subdirectory. If the latter, would the code in gen-cpp needs its own CMakeLists.txt? Why is this additional file not needed, when the contents of said directory are root level?

InfernalRapture
  • 572
  • 7
  • 19
yodafan86
  • 565
  • 1
  • 6
  • 14

2 Answers2

22

add_subdirectory(source_dir): Used to add a subdirectory to the build. There is also a CMakeLists.txt file in the source_dir. This CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command.

include_directories(dir): Add the given directories to those the compiler uses to search for include files. These directories are added to the directory property INCLUDE_DIRECTORIES for the current CMakeLists file.

Unterbelichtet
  • 628
  • 2
  • 7
  • 18
Jie Yin
  • 560
  • 5
  • 9
15

The include_directories() is used for adding headers search paths (-I flag) and add_subdirectory() will make no difference in this case.

I suppose, you need to list *.cpp files from gen-cpp folder in add_executable() or add_library() calls, in which you wish these symbols to be.

Alternatively, you can compile all thrift sources into the library and link it with your code.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • To elaborate on your last point, that sounds like would be the ideal way to deal with this, is there an example of using a CMake macro used to auto-generate/compile/link thrift code that someone can point me to? – yodafan86 Oct 07 '12 at 22:23
  • I think I'm still not quite explaining my problem correctly, I believe it lies in my general understanding of Cmake, not specifically to thrift. The generic problem is given a top level directory structure that contains a CMakeLists.txt, some .cpp/.h files, and a directory that contains additional .cpp/.h files that those in the top level directory #include--- which commands should I be looking at in the root level CMakeLists.txt to properly compile/link the files in the inner directory? Would I also need an additional CMakeLists.txt in the inner directory, if so containing what? – yodafan86 Oct 07 '12 at 22:53
  • Nope, you don't need it. Just list these files in add_*() call: `add_executable(myexe file1.cpp folder/file2.cpp)`. – arrowd Oct 08 '12 at 05:42
  • In which case include_directories is as same as add_subdirectory, in which case is difference. if some one could please have an example. – user454083 May 21 '15 at 14:12