cmake使用详细教程(日常使用这一篇就足够了)

🎯 365bet手机在线注册 📅 2025-10-25 18:48:53 👤 admin 👀 9272 ❤️ 759
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 value or use a ... suffix to tell

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来编译程序

此时会发现已生成可执行程序,并可以正常执行

🎯 相关推荐

十大鞋子批发网站榜中榜
🎯 www.365bet.com亚洲版

十大鞋子批发网站榜中榜

📅 08-14 👀 2539
东海属于哪个省
🎯 www.365bet.com亚洲版

东海属于哪个省

📅 09-13 👀 6551
方法 1. 一键有效地将短信从 iPhone 传输到 Android
🎯 www.365bet.com亚洲版

方法 1. 一键有效地将短信从 iPhone 传输到 Android

📅 08-14 👀 9627

🎁 合作伙伴