cmake使用详细教程(日常使用这一篇就足够了)
一、只有一个源文件的程序编译
首先在当前目录下创建两个文件
hello.cpp
#include
using namespace std;
int main()
{
cout << "Hello 今天是2023/2/26" << endl;
return 0;
}
CMakeLists.txt (注意CMakeLists大小写,不要写错了)
cmake_minimum_required (VERSION 2.8)
project (learn_cmake)
add_executable(hello hello.cpp)
第一行意思是cmake最低版本要求2.8,第二行是本项目的工程名第三行:第一个变量:要生成的可执行文件名为hello,后面的参数是需要的依赖。
接着在当前目录下执行 cmake .
[root@centOS learn_cmake]# cmake .
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument
CMake that the project does not need compatibility with older versions.
-- The C compiler identification is GNU 8.3.1
-- The CXX compiler identification is GNU 8.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /opt/rh/devtoolset-8/root/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: /opt/rh/devtoolset-8/root/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /root/learn_cmake
接着会发现目录下多生成了一些文件,例如Makefile
然后使用GNU make来编译程序
此时会发现已生成可执行程序,并可以正常执行