CMake Difference between include_directories and add_subdirectory?

yodafan86 picture yodafan86 · Oct 6, 2012 · Viewed 10.4k times · Source

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?

Answer

arrowd picture arrowd · Oct 6, 2012

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.