To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .
In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).
How about the good old globbing?
FILE(GLOB MyCSources *.c)
ADD_EXECUTABLE(MyExecutable ${MyCSources})
Try this:
AUX_SOURCE_DIRECTORY
Find all source files in a directory.
AUX_SOURCE_DIRECTORY(dir VARIABLE)
Collects the names of all the source files in the specified directory and stores the list in the variable provided. This command is intended to be used by projects that use explicit template instantiation. Template instantiation files can be stored in a "Templates" subdirectory and collected automatically using this command to avoid manually listing all instantiations.
It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.
GLOB_RECURSE
recursive example
It basically makes the *
also go into subdirectories:
cmake_minimum_required(VERSION 3.0)
file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "src/*.c")
add_executable(main ${SOURCES})
And then our sources could be located for example as:
src/main.c
src/d/a.c
src/d/a.h
And main.c
uses #include "d/a.h"
and a.c
uses #include "a.h"
.
Using GLOB_RECURSE
on the CMake toplevel (i.e. "*.c"
instead of "src/*.c"
) is likely a bad idea, as it can pick up .c
files generated by CMake itself which are placed under build/
.
Runnable example on GitHub.
The only correct answer is to not do this. This has been detailed in multiple answers here on SO. I will summarize here, in decreasing order of importance.
aux_source_directory
function is outright incorrect since it cannot detect changes in the filesystem. For the same reason, using file(GLOB
or file(GLOB_RECURSE
without CONFIGURE_DEPENDS
is outright incorrect.CONFIGURE_DEPENDS
is not guaranteed to work. (See point 1)
1 Here's what the CMake developers have to say about it:
Note: 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. The
CONFIGURE_DEPENDS
flag may not work reliably on all generators, or if a new generator is added in the future that cannot support it, projects using it will be stuck. Even ifCONFIGURE_DEPENDS
works reliably, there is still a cost to perform the check on every rebuild.
Yes, you have two options. Let's assume you the folder structure something similar to this.
├── autopilot
│ ├── _AutoPilot.cpp
│ ├── _AutoPilot.h
│ └── action
│ ├── ActionBase.cpp
│ ├── ActionBase.h
│ ├── APcopter
│ │ ├── APcopter_avoid.cpp
│ │ ├── APcopter_avoid.h
If you are to use AUX_SOURCE_DIRECTORY
you have to add CMakeLists.txt each one of sub directories. Then you have to include and link all those subdirectories. This is quite a difficult task. So you can you GLOB and do the job very easily. This is how it is done.
file(GLOB autopilot_sources ./**.cpp ./**.c)
SET( autopilot ${autopilot_sources})
If you want to create a library using above source code this is the command:
ADD_LIBRARY ( autopilot ${autopilot_sources})
TARGET_LINK_LIBRARIES ( autopilot)
If you want to create an executable file using above source code this is the command:
ADD_EXECUTABLE(autopilot ${autopilot_sources})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With