Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A How to do "out-of-source" build properly with cmake?

Use the following layout: project/ CMakeLists.txt project_1.cpp project_2.cpp libA CMakeLists.txt libA_1.cpp libA_2.cpp libB CMakeLists...

posted 4y ago by alex‭  ·  edited 3y ago by alex‭

Answer
#2: Post edited by user avatar alex‭ · 2021-06-27T16:38:03Z (over 3 years ago)
  • Use the following layout:
  • ```cmake
  • project/
  • CMakeLists.txt
  • project_1.cpp
  • project_2.cpp
  • libA
  • CMakeLists.txt
  • libA_1.cpp
  • libA_2.cpp
  • libB
  • CMakeLists.txt
  • libB_1.cpp
  • libB_2.cpp
  • ```
  • In `project/CMakeLists.txt`, you would have:
  • ```cmake
  • cmake_minimum_required(VERSION 3.16) # or newer
  • project(project)
  • # Set a global C++ version
  • set(CMAKE_CXX_VERSION 14)
  • set(CMAKE_CXX_VERSION_REQUIRED YES)
  • set(CMAKE_CXX_EXTENSIONS NO)
  • # Add libraries to the build
  • add_subdirectory(libA)
  • add_subdirectory(libB)
  • # Create main project executable and link to libs
  • add_executable(main project_1.cpp project_2.cpp)
  • target_link_libraries(main PRIVATE libA libB)
  • ```
  • Then both `project/libA/CMakeLists.txt` and `libB` will look like:
  • ```cmake
  • # Create "libX" (X=A or B) library
  • add_library(libX libX_1.cpp libX_2.cpp)
  • # Expose headers to internal build.
  • target_include_directories(
  • libX PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
  • ```
  • Then, when building, you can select an out-of-tree directory for _everything_. CMake will automatically create a parallel directory structure (that's part of what `add_subdirectory` does) for the sub-builds.
  • ```console
  • $ cd /path/to/project
  • $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
  • ...
  • $ cmake --build build
  • ...
  • $ ./build/main
  • ...
  • ```
  • As of CMake 3.19, you can use [presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html), which remove most needs for wrapper scripts that invoke CMake. See the documentation for more details.
  • Also, the build directory has never needed to exist before calling CMake; it will create it if it does not exist.
  • Use the following layout:
  • ```cmake
  • project/
  • CMakeLists.txt
  • project_1.cpp
  • project_2.cpp
  • libA
  • CMakeLists.txt
  • libA_1.cpp
  • libA_2.cpp
  • libB
  • CMakeLists.txt
  • libB_1.cpp
  • libB_2.cpp
  • ```
  • In `project/CMakeLists.txt`, you would have:
  • ```cmake
  • cmake_minimum_required(VERSION 3.16) # or newer
  • project(project)
  • # Set a global C++ version
  • set(CMAKE_CXX_STANDARD 14)
  • set(CMAKE_CXX_STANDARD_REQUIRED YES)
  • set(CMAKE_CXX_EXTENSIONS NO)
  • # Add libraries to the build
  • add_subdirectory(libA)
  • add_subdirectory(libB)
  • # Create main project executable and link to libs
  • add_executable(main project_1.cpp project_2.cpp)
  • target_link_libraries(main PRIVATE libA libB)
  • ```
  • Then both `project/libA/CMakeLists.txt` and `libB` will look like:
  • ```cmake
  • # Create "libX" (X=A or B) library
  • add_library(libX libX_1.cpp libX_2.cpp)
  • # Expose headers to internal build.
  • target_include_directories(
  • libX PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
  • ```
  • Then, when building, you can select an out-of-tree directory for _everything_. CMake will automatically create a parallel directory structure (that's part of what `add_subdirectory` does) for the sub-builds.
  • ```console
  • $ cd /path/to/project
  • $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
  • ...
  • $ cmake --build build
  • ...
  • $ ./build/main
  • ...
  • ```
  • As of CMake 3.19, you can use [presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html), which remove most needs for wrapper scripts that invoke CMake. See the documentation for more details.
  • Also, the build directory has never needed to exist before calling CMake; it will create it if it does not exist.
#1: Initial revision by user avatar alex‭ · 2021-01-29T11:36:02Z (almost 4 years ago)
Use the following layout:

```cmake
project/
    CMakeLists.txt
    project_1.cpp
    project_2.cpp
    libA
        CMakeLists.txt
        libA_1.cpp
        libA_2.cpp
    libB
        CMakeLists.txt
        libB_1.cpp
        libB_2.cpp
```

In `project/CMakeLists.txt`, you would have:

```cmake
cmake_minimum_required(VERSION 3.16) # or newer
project(project)

# Set a global C++ version
set(CMAKE_CXX_VERSION 14)
set(CMAKE_CXX_VERSION_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

# Add libraries to the build
add_subdirectory(libA)
add_subdirectory(libB)

# Create main project executable and link to libs
add_executable(main project_1.cpp project_2.cpp)
target_link_libraries(main PRIVATE libA libB)
```

Then both `project/libA/CMakeLists.txt` and `libB` will look like:

```cmake
# Create "libX" (X=A or B) library
add_library(libX libX_1.cpp libX_2.cpp)

# Expose headers to internal build.
target_include_directories(
    libX PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
```

Then, when building, you can select an out-of-tree directory for _everything_. CMake will automatically create a parallel directory structure (that's part of what `add_subdirectory` does) for the sub-builds.

```console
$ cd /path/to/project
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
...
$ cmake --build build
...
$ ./build/main
...
```

As of CMake 3.19, you can use [presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html), which remove most needs for wrapper scripts that invoke CMake. See the documentation for more details.

Also, the build directory has never needed to exist before calling CMake; it will create it if it does not exist.