Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake Difference between include_directories and add_subdirectory?

Tags:

c++

cmake

thrift

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?

like image 561
yodafan86 Avatar asked Oct 06 '12 16:10

yodafan86


People also ask

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is the difference between Include_directories and Target_include_directories?

1, include_directories() is accessible for all the files in the source-tree 2, target_include_directories() is-only accessible for a specific target when compile.

What is Cmake_current_list_dir?

CMAKE_CURRENT_LIST_DIR: Full directory of the listfile currently being processed. As CMake processes the listfiles in your project this variable will always be set to the directory where the listfile which is currently being processed (CMAKE_CURRENT_LIST_FILE) is located. The value has dynamic scope.


2 Answers

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.

like image 135
arrowd Avatar answered Sep 20 '22 07:09

arrowd


add_subdirectory(source_dir) is 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.

like image 31
Jie Yin Avatar answered Sep 17 '22 07:09

Jie Yin