Hello, CMake

So how to write an initial CMake managed application?

Most modern Linux operating systems come with CMake, GCC, and make, because this is required by many Linux open source software. We are now going to write a simple CMake generate executable program project, this example can be used in various common Linux distributions, whether it is Ubuntu, centos, Deepin, manjaro.

I’m using manjaro and have the VS Code editor installed with the CMake plugin.

New construction structure

As a first step, we create a blank folder hello_cmake and open it with VS Code.

Create a simple project with three source files: main. CPP,hello. h,hello. CPP. The document reads as follows:

/// hello.h
# pragma once

void PrintHello(a);

/// hello.cpp
# include "hello.h"
#include <iostream>

using namespace std;

void PrintHello( )
{
    cout << "Hello CMake!" << endl;
}

/// main.cpp
#include "hello.h"

int main(a)
{
    PrintHello();
    return 0;
}
Copy the code

For three very neat source files, we have our main file reference functions declared in hello.h and defined in hello.cpp

Edit CMakeLists. TXT

Next, create a new CMake project file in the directory named cmakelists.txt with the correct filename, or you may not find the file.

Fill in the following contents in cmakelists. TXT:

# project name, you can choose anything you like
project(HELLO_CMAKE)    

Put the compiled executable files in the output folder
The output path is XXX /bin
# ${} is a way for CMake to access the content of a variable, replaced by the actual content of the variable name
# PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR are CMAKE variables that point to the project's source file path and the actual compilation path
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

Set the export executable to hello.
The name of the executable program is independent of the project name. A project can compile multiple executable programs
# ADD_EXECUTABLE(program_name file1,file2,...)
add_executable(hello main.cpp hello.cpp)
Copy the code

Compile the project

Usually, we compile our programs using an external build, which separates the source files from the target files and various intermediate files, keeping the project path cleaner.

mkdir build
cd build
cmake ..
make
Copy the code

If you execute the code above, you might see the following output

[chaoqun@manjaro hello_cmake]$ cd hello_cmake/
[chaoqun@manjaro hello_cmake]$ ls
CMakeLists.txt  hello.cpp  hello.h  main.cpp
[chaoqun@manjaro hello_cmake]$ mkdir build
[chaoqun@manjaro hello_cmake]$ cdbuild/ [chaoqun@manjaro build]$ ls [chaoqun@manjaro build]$ cmake .. The C compiler Identification is GNU 9.2.0 The CXX compiler Identification is GNU 9.2.0 Checkfor 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chaoqun/workspace/learning/hello_cmake/hello_cmake/build
[chaoqun@manjaro build]$ make
Scanning dependencies of target hello
[ 33%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
[100%] Linking CXX executable bin/hello
[100%] Built target hello
[chaoqun@manjaro build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  bin  cmake_install.cmake
[chaoqun@manjaro build]$ ./bin/hello 
Hello CMake!
Copy the code

At this point, we have compiled the simplest CMake project possible.

Reference

  1. Code sample
  2. CMake Partice
  3. Wikipedia