Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple source directories for one executable with CMake

I want my source organised in a number of subdirectories but be able to create a single executable without having to build a library for each subdirectory. Can CMake do this? Something like:

ADD_EXECUTABLE(foo a/main.cpp a/other.cpp b/another.cpp)

Is it that simple? With the / working as a directory separator regardless of platform?

like image 618
realh Avatar asked Jul 29 '13 20:07

realh


People also ask

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.

How do I add a library folder to CMake?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories.

Where does CMake store object files?

For an OBJECT library, the output object files are created in a build directory named after the library (system. dir). In our case, for a debug build, this is the location build/debug/system/CMakeFiles/system. dir/ (this output directory store object files as a mirror of the directory structure of the source files).


1 Answers

Here the my simple example

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(foo CXX)
# get all *.cpp files recursively
file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
add_executable(foo ${SRC_FILES})
like image 194
Sergei Nikulov Avatar answered Oct 14 '22 20:10

Sergei Nikulov