Skip to content

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
#include<stdio.h>
#include<greetings_english.h>
#include<greetings_chinese.h>
#include<greetings_vietnamese.h>
//​
// Main functions​
//​
int  main(int argc, char * argv[])
{
    greeting_en();
    greeting_cn();
    greeting_vi();
    return 0;
}

Header files

greetings_english.h
#include<stdio.h>
void  greeting_en();
greetings_chinese.h
#include<stdio.h>
void  greeting_cn();
greetings_vietnamese.h
#include<stdio.h>
void  greeting_vi();

Source files

greetings_english.cpp
1
2
3
4
5
#include<stdio.h>
void greeting_en()
{
    printf("Hello!\n");
}​​
greetings_chinese.cpp
1
2
3
4
5
#include<stdio.h>
void greeting_cn()
{
    printf("Nihao!\n");
}
greetings_vietnamese.cpp
1
2
3
4
5
#include<stdio.h>
void greeting_vi()
{
    printf("Xin chao!\n");
}

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​

root@Henry-Legion5:/cmake_practice_02_separated_build/build# ./helloworld
Hello!
Nihao!
Xin chao!

Source code can be found here

Share this page:

LinkedIn Twitter Facebook

Comments