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?
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.
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.
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).
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})
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