Separate build files and source code
Scenario
After build the code, build files and its sub-files is in mixing. How to separate them to different directories?
Result
Separated build files and source code
Lets firstly look at the source code
$ tree
.
├── CMakeLists.txt
├── build
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
├── greetings_vietnamese.cpp
└── mainapp.cpp
| mainapp.cpp | |
|---|---|
Header files
Source files
| greetings_vietnamese.cpp | |
|---|---|
CMakeLists.txt
# -------------------------------------------
# The minimum version that required to be able to read this CMakeList.txt
# -------------------------------------------
cmake_minimum_required(VERSION 2.8.12)
# -------------------------------------------
# Project name often named as the binary (executable) file
# -------------------------------------------
project(helloworld)
# -------------------------------------
# declare the directories that store project's file headers (.h)
# -------------------------------------
include_directories(include)
# -------------------------------------
# adding source code file with command *set*
# -------------------------------------
# set(SOURCES source/mainapp.cpp source/greetings_english.cpp source/greetings_chinese.cpp source/greetings_vietnamese.cpp)
# -------------------------------------
# An alternative to set() is file(), this method is a good practice and
# time-saving and also more common among developer community
# And also, it support glob *
# -------------------------------------
file(GLOB SOURCES "source/*.cpp")
# -------------------------------------------
# Define the relationship between the the source code and the binary file
# -------------------------------------------
add_executable(helloworld ${SOURCES})
Build
We change the directory to build folder and run the command cmake .. Double dot ".." means the upper layer that store the CMakeList.txt
[root@base06 build]# cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/henryle/CMake_Practice/cmake_practice_02_separated_build/build
Compiling procedure is quite like CMake "Hello World"
Observe the file tree again
root@Henry-Legion5:/cmake_practice_02_separated_build# tree -L 2
.
├── CMakeLists.txt
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── Makefile
│ └── cmake_install.cmake
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
├── greetings_vietnamese.cpp
└── mainapp.cpp
Run the Makefile in folder build
root@Henry-Legion5:/cmake_practice_02_separated_build/build# make
Scanning dependencies of target helloworld
[ 20%] Building CXX object CMakeFiles/helloworld.dir/source/greetings_chinese.cpp.o
[ 40%] Building CXX object CMakeFiles/helloworld.dir/source/greetings_english.cpp.o
[ 60%] Building CXX object CMakeFiles/helloworld.dir/source/greetings_vietnamese.cpp.o
[ 80%] Building CXX object CMakeFiles/helloworld.dir/source/mainapp.cpp.o
[100%] Linking CXX executable helloworld
[100%] Built target helloworld
Double check the file tree, we see the executable/binary file helloworld is generated in build folder
root@Henry-Legion5:/mnt/d/PROJECTS/How_to_write_CMake/cmake_practice_02_separated_build# tree -L 2
.
├── CMakeLists.txt
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── Makefile
│ ├── cmake_install.cmake
│ └── helloworld
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
├── greetings_vietnamese.cpp
└── mainapp.cpp
Execute the file helloworld, we see the expected result
Source code can be found here