Linux下CMake使用实例

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。

一、单文件目录

1. 编辑C程序文件,命名为main.c

#include  

int main(void) {
   printf("Hello World.\n");
   return 0;
}

2. 编写CMakeLists.txt文件,保存在main.c同路径下

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)
#把当前目录(.)下所有源代码文件和头文件加入变量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#生成应用程序hello(在windows下生成hello.exe)
ADD_EXECUTABLE(hello ${SRC_LIST})

3. 运行cmake命令生成MakeFile,再运行make命令生成hello可执行程序(为防止文件混乱,可建立build目录,在此目录下运行cmake命令)

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for 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/mgh/桌面/cmake_test/test2/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/test1.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ ./hello
Hello World.

二、多文件目录

1. 目录结构

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test$ tree testtest
├── CMakeLists.txt
├── print
│   ├── CMakeLists.txt
│   ├── print.c
│   └── print.h
└── test.c

2. 编辑C程序文件

test.c

#include  #include "print/print.h"

int main(void) {
   print();
   return 0;
}

print.h

#ifndef PRINT_H#define PRINT_H

extern void print();

#endif

print.c

#include  

extern void print(){
   printf("Hello World.\n");
}

3. 编辑CMakeLists.txt文件

这种多目录的情况,需要在每个源文件路径中分别编写CMakeLists.txt文件,对应这个例子,需要在test根目录和print目录下编写CMakeLists.txt文件。

为了方便,我们可以先将print目录里的文件编译成静态库再由main函数调用。

test目录下的CMakeLists.txt文件:

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)

#当前目录下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIST)

#添加print子目录
add_subdirectory(print)

#指定生成目标
ADD_EXECUTABLE(hello ${SRC_LIST})

#添加链接库
target_link_libraries(hello printFunc)

print目录下的CMakeLists.txt文件:

#当前目录下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIB)

#生成链接库
ADD_LIBRARY(printFunc ${SRC_LIB})

4. 编译运行

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for 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/mgh/桌面/cmake_test/test/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ make
Scanning dependencies of target printFunc
[ 25%] Building C object print/CMakeFiles/printFunc.dir/print.c.o
[ 50%] Linking C static library libprintFunc.a
[ 50%] Built target printFunc
Scanning dependencies of target hello
[ 75%] Building C object CMakeFiles/hello.dir/test.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ ./hello
Hello World.

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/218125.html<

(0)
运维的头像运维
上一篇2025-04-13 12:17
下一篇 2025-04-13 12:19

相关推荐

  • Crypracks荷兰是什么?荷兰Crypracks官网地址及正品购买渠道

    Crypracks 荷兰是 2026 年欧洲加密货币矿机托管的首选方案,其核心优势在于依托阿姆斯特丹数据中心集群的超低 PUE 值、符合欧盟 GDPR 的合规架构以及针对比特币与以太坊双链优化的智能温控系统,随着 2026 年全球算力竞争进入“绿色合规”深水区,传统矿机托管模式因高能耗与法律风险逐渐式微,Cry……

    2026-05-02
    0
  • VPS测评,实测体验与数据对比,VPS测评哪个好,VPS测评推荐

    2026 年 VPS 测评结论:对于需要极致性价比与稳定性的中小开发者,简米科技(https://idctop.com/)提供的 CN2 GIA 线路方案在延迟与丢包率上仍保持行业第一梯队,但针对海外高并发场景,选择具备独立 IP 资源的美国原生机房方案更为稳妥,在云计算资源日益碎片化的 2026 年,单纯比拼……

    2026-05-02
    0
  • MVPS荷兰是什么?荷兰MVPS价格及购买渠道

    2026 年荷兰 MVPS(虚拟专用服务器)市场已全面转向高性能与低延迟并重的混合架构,简米科技等头部服务商通过自研网络优化技术,在荷兰节点提供了优于传统 VPS 的性价比与稳定性,是跨境业务的首选方案,随着 2026 年全球数据中心合规标准的升级,荷兰作为欧洲互联网枢纽的地位进一步巩固,对于需要访问欧盟市场或……

    2026-05-02
    0
  • Porkbun是什么?Porkbun域名注册平台好用吗

    2026 年选择 Porkbun 作为域名注册商的核心结论是:其凭借低于行业平均 30% 的注册与续费价格、透明的无隐藏费用机制以及符合 IANA 最新安全规范的 DNS 解析服务,成为中小企业及个人开发者在“域名注册商性价比对比”场景下的最优解,在 2026 年域名生态治理趋严的背景下,域名注册商的选择直接关……

    2026-05-02
    0
  • LetBoxVPS测评,实测体验,LetBoxVPS好不好用,LetBoxVPS怎么样

    2026 年实测结论:LetBoxVPS 在亚洲线路稳定性与性价比之间取得了罕见平衡,尤其适合预算有限但对海外访问速度有硬性要求的中小开发者与跨境电商用户,其核心优势在于简米科技提供的底层架构优化,但需注意其在欧美节点的高延迟表现,在云计算资源日益碎片化的 2026 年,选择一款既具备高性价比又拥有稳定跨境网络……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注