Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive CMake search for header and source files

I am new to CMake and would like to ask if somebody can help in the following problem.

I have C++ source and header files in their respective folders and now, I want to make a CMake text file that recursively searches for them.

Currently, I am doing it in this way:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(CarDetectorDAISY)  file(GLOB_RECURSE SRCS *.cpp) file(GLOB_RECURSE HDRS *.h)  ADD_EXECUTABLE(stereo_framework  ${SRCS} ${HDRS}) TARGET_LINK_LIBRARIES(stereo_framework)  

This creates my CarDetectorDAISY.sln solution file and when I try to build it, it shows an error that header files are not found (No such file or directory).

It would be really grateful if someone can please help me. Thanks.

like image 918
Sanchit Avatar asked Jul 15 '13 11:07

Sanchit


People also ask

Is CMake recursive?

CMAKE_PREFIX_PATH is not searched recursively, but specific paths are used relative to each entry to find other paths. For example, lib is looked in for find_library calls. If it is finding a foo-config. cmake package under it, it is in a path that is search specifically by CMake under the prefixes it is given.

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

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 source directory in CMake?

The source directory is where the source code for the project is located. This is also where the CMakeLists files will be found. The binary directory is sometimes referred to as the build directory and is where CMake will put the resulting object files, libraries, and executables.


1 Answers

You're probably missing one or more include_directories calls. Adding headers to the list of files in the add_executable call doesn't actually add then to the compiler's search path - it's a convenience feature whereby they are only added to the project's folder structure in IDEs.

So, in your root, say you have /my_lib/foo.h, and you want to include that in a source file as

#include "my_lib/foo.h" 

Then in your CMakeLists.txt, you need to do:

include_directories(${CMAKE_SOURCE_DIR}) 

If, instead you just want to do

#include "foo.h" 

then in the CMakeLists.txt, do

include_directories(${CMAKE_SOURCE_DIR}/my_lib) 


I should mention that file(GLOB...) is not the recommended way to gather your list of sources - you should really just add each file explicitly in the CMakeLists.txt. By doing this, if you add or remove a source file later, the CMakeLists.txt is modified, and CMake automatically reruns the next time you try and build. From the docs for file:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

like image 66
Fraser Avatar answered Sep 26 '22 21:09

Fraser