First, install gcc-g++ and cmake. Taking CentOS as an example:
yum install gcc-c++
yum install cmake
First, create a main.cpp file that includes a main function, and create a CMakeLists.txt file. The purpose of the CMakeLists.txt file is to guide CMake in compiling the C++ program on the current operating system.
main.cpp
#include <iostream>
int main(){ std::cout << "Hello World!\n"; return 0;}
CMakeLists.txt
cmake_minimum_required(VERSION 2.4)
project(hello_world)
add_executable(app main.cpp)
Analysis:
① cmake_minimum_required(VERSION 2.4): Sets the minimum required CMake version for the current script.
② project(hello_world): Starts creating a new CMake project.
③ add_executable(app main.cpp): Builds the app program using the main.cpp file.
Non-recommended build commands:
> cmake ....> cmake --build .
① cmake .: This command will look for CMakeLists.txt in the current directory and generate a build directory there.
② cmake –build .: This is the build/make command.
Recommended build commands:
This method keeps the directory cleaner.
> mkdir build
> cd build
> cmake ..
> cmake --build .