CMake - "Hello world"
Definition from Wikipedia
In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method.
So, clearly, CMake is not a build system but rather it generates another system's build files.
Source code
Lets have a simple mainapp.cpp that will flush to output the string "Hello world!"
CMakeLists
CMakeLists.txt that is put in the same level with mainapp.cpp with content:
Putting the two files into the same directory, the hierarchy file can be seen by command tree as
root@ubuntu-22:/home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code# tree
.
├── CMakeLists.txt
└── mainapp.cpp
The file CMakeList.txt acts like a definition that CMake could understand, here we goes into the project before running the following command:
Note: The dot "." is very important, it is the navigator to the path that store the file CMakeList.txt. In this case we can change it to $(PWD), the result should be the same.
Build the code
root@ubuntu-22:/home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code# cmake .
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code
As we can see, there are many files and folder created during the running of CMake. In this tutorial, we only focus on the Makefile created
Observe:
- CMake's duty is helping to generate Makefile, in other words, it is NOT helping us to build the project but to generate the Makefile only and then . . . done!
- As Wikipedia defines, CMake is NOT a build system.
So now, to build the project, we invoke Makefiles with command make
root@ubuntu-22:/home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code# make
[ 50%] Building CXX object CMakeFiles/helloworld.dir/mainapp.cpp.o
[100%] Linking CXX executable helloworld
[100%] Built target helloworld
The result will be an executable file named helloworld as below
root@ubuntu-22:/home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code# ls -la
total 64
drwxrwxr-x 3 henry henry 4096 八 31 21:56 .
drwxrwxr-x 10 henry henry 4096 八 31 20:49 ..
-rw-r--r-- 1 root root 14018 八 31 21:50 CMakeCache.txt
drwxr-xr-x 5 root root 4096 八 31 21:56 CMakeFiles
-rw-r--r-- 1 root root 1724 八 31 21:50 cmake_install.cmake
-rw-rw-r-- 1 henry henry 583 六 9 10:13 CMakeLists.txt
-rwxr-xr-x 1 root root 16000 八 31 21:56 helloworld
-rw-rw-r-- 1 henry henry 351 六 9 10:08 mainapp.cpp
-rw-r--r-- 1 root root 5467 八 31 21:50 Makefile
Enjoy the result
Execute the file, we see the expected result:
root@ubuntu-22:/home/henry/HENRY_LAB/How_to_write_CMake/cmake_practice_01_single_source_code# ./helloworld
Hello world!
Source code can be found here