当前位置: 首页 > news >正文

【CMake】使用 CMake 将多模块 C 项目构建为库并链接主程序

LuckiBit

目录

  • 使用现代 CMake 构建多模块 C 项目:以 art、color、password 模块为例
    • 📁 1. 项目结构总览
    • ⚙️ 2. 顶层 CMakeLists.txt
    • 🧩 3. 子模块构建文件
      • 📄 3.1 src/art/CMakeLists.txt
      • 📄 3.2 src/color/CMakeLists.txt
      • 📄 3.3 src/password/CMakeLists.txt
    • 📑 4. 头文件与实现文件
      • 🔹 include/art.h
      • 🔹 src/art/art.c
      • 🔹 include/color.h
      • 🔹 src/color/color.c
      • 🔹 include/password.h
      • 🔹 src/password/password.c
    • 🚀 5. 主程序入口 main.c
    • 🔧 6. 构建项目
      • ✅ 输出:
    • 📂 7. 构建输出目录说明
    • 📌 8. 项目优势与可扩展性
    • 🪄 9. 扩展建议
    • ✅ 最终输出结构示意(build/)
    • 🧹 清理构建
    • 🧭 结语
    • 相关文章:

使用现代 CMake 构建多模块 C 项目:以 art、color、password 模块为例

CMake 是目前最主流的 C/C++ 构建工具之一,具备跨平台、模块化、维护性强等优势。本文将以你提供的多模块 C 项目为例,介绍如何使用现代 CMake:

  • 组织清晰的模块目录结构;
  • 每个模块单独构建为静态库;
  • 主程序链接多个模块;
  • 配置输出产物目录;
  • 实现跨平台构建支持。

📁 1. 项目结构总览

your_project/
├── CMakeLists.txt             # 顶层构建脚本
├── include/                   # 所有模块的公共头文件
│   ├── art.h
│   ├── color.h
│   └── password.h
├── src/                       # 源文件和子模块
│   ├── main.c                 # 主程序入口
│   ├── art/
│   │   ├── art.c
│   │   └── CMakeLists.txt
│   ├── color/
│   │   ├── color.c
│   │   └── CMakeLists.txt
│   └── password/
│       ├── password.c
│       └── CMakeLists.txt
└── build/                     # 构建输出目录(自动生成)

⚙️ 2. 顶层 CMakeLists.txt

这是项目的统一构建入口:

cmake_minimum_required(VERSION 3.10)
project(MyApp C)set(CMAKE_C_STANDARD 99)# 设置输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)# 添加模块构建目录
add_subdirectory(src/art)
add_subdirectory(src/color)
add_subdirectory(src/password)# 构建主程序
add_executable(${PROJECT_NAME} src/main.c)# 链接静态库
target_link_libraries(${PROJECT_NAME}PRIVATEartcolorpassword
)

🧩 3. 子模块构建文件

📄 3.1 src/art/CMakeLists.txt

add_library(art STATIC art.c)
target_include_directories(art PUBLIC ${CMAKE_SOURCE_DIR}/include)

📄 3.2 src/color/CMakeLists.txt

add_library(color STATIC color.c)
target_include_directories(color PUBLIC ${CMAKE_SOURCE_DIR}/include)

📄 3.3 src/password/CMakeLists.txt

add_library(password STATIC password.c)
target_include_directories(password PUBLIC ${CMAKE_SOURCE_DIR}/include)

📑 4. 头文件与实现文件

🔹 include/art.h

#ifndef ART_H
#define ART_Hvoid set_art(void);#endif

🔹 src/art/art.c

#include <stdio.h>
#include "art.h"void set_art(void) {printf("Art module called.\n");
}

🔹 include/color.h

#ifndef COLOR_H
#define COLOR_Hvoid set_color(void);#endif

🔹 src/color/color.c

#include <stdio.h>
#include "color.h"void set_color(void) {printf("Color module called.\n");
}

🔹 include/password.h

#ifndef PASSWORD_H
#define PASSWORD_Hvoid check_password(void);#endif

🔹 src/password/password.c

#include <stdio.h>
#include "password.h"void check_password(void) {printf("Password checked.\n");
}

🚀 5. 主程序入口 main.c

#include "art.h"
#include "color.h"
#include "password.h"int main(void) {set_art();set_color();check_password();return 0;
}

🔧 6. 构建项目

在项目根目录下执行以下命令:

mkdir build
cd build
cmake ..
cmake --build .

如果使用 MinGW 构建(Windows):

cmake -G "MinGW Makefiles" ..
mingw32-make

构建完成后:

./bin/MyApp     # Linux/macOS
.\bin\MyApp.exe # Windows

✅ 输出:

Art module called.
Color module called.
Password checked.

📂 7. 构建输出目录说明

输出目录内容
build/bin/可执行程序 MyApp
build/lib/模块静态库 libart.alibcolor.alibpassword.a

📌 8. 项目优势与可扩展性

特性描述
模块化构建每个模块独立,便于维护和复用
跨平台支持CMake 可输出 Makefile、Ninja、Visual Studio 工程
可维护性强项目结构清晰,新增模块无需改动主流程
现代风格 CMake使用 target_* 系列命令,更安全更显式

🪄 9. 扩展建议

  • ✅ 支持动态库构建(将 STATIC 改为 SHARED
  • ✅ 添加单元测试模块(使用 CTest、Unity、GoogleTest 等)
  • ✅ 添加 install() 与 export() 规则,支持安装部署
  • ✅ 使用 target_precompile_headers() 加速编译

✅ 最终输出结构示意(build/)

build/
├── bin/
│   └── MyApp.exe
├── lib/
│   ├── libart.a
│   ├── libcolor.a
│   └── libpassword.a
├── CMakeFiles/           # 构建中间文件,可删除
├── CMakeCache.txt        # 构建缓存,可删除
└── MyApp.sln             # Visual Studio 生成的解决方案(若使用)

若只需产物,可拷贝 bin/lib/output/,其余中间文件可清理。


🧹 清理构建

清理构建只需删除 build/

rm -rf build/         # macOS / Linux
rd /s /q build        # Windows PowerShell

🧭 结语

借助 CMake,你可以快速构建跨平台、结构清晰、模块可扩展的 C 项目。本文展示了从目录设计、模块组织到构建输出的完整流程,是进行中大型 C 项目的理想模板。

  1. 本节内容已经全部介绍完毕,希望通过这篇文章,大家对 CMake 有了更深入的理解和认识。
  2. 感谢各位的阅读和支持,如果觉得这篇文章对你有帮助,请不要吝惜你的点赞和评论,这对我们非常重要。再次感谢大家的关注和支持!点我关注❤️

相关文章:

  • 指针的神秘探险:从入门到精通的奇幻之旅 !

文章转载自:
http://argyrol.sxnf.com.cn
http://bebeerine.sxnf.com.cn
http://boltoperated.sxnf.com.cn
http://anemochorous.sxnf.com.cn
http://awning.sxnf.com.cn
http://bagman.sxnf.com.cn
http://chomp.sxnf.com.cn
http://archive.sxnf.com.cn
http://andirons.sxnf.com.cn
http://benediction.sxnf.com.cn
http://ballistite.sxnf.com.cn
http://certain.sxnf.com.cn
http://cannonry.sxnf.com.cn
http://autocratic.sxnf.com.cn
http://balconied.sxnf.com.cn
http://beeb.sxnf.com.cn
http://butterfingers.sxnf.com.cn
http://aglitter.sxnf.com.cn
http://adulterous.sxnf.com.cn
http://amebic.sxnf.com.cn
http://barbule.sxnf.com.cn
http://amytal.sxnf.com.cn
http://archipelago.sxnf.com.cn
http://avatar.sxnf.com.cn
http://alembic.sxnf.com.cn
http://android.sxnf.com.cn
http://chameleon.sxnf.com.cn
http://bugshah.sxnf.com.cn
http://body.sxnf.com.cn
http://allegiance.sxnf.com.cn
http://www.dtcms.com/a/281396.html

相关文章:

  • Spring MVC2
  • React 手动实现页面锚点导航
  • AI Agent 框架LangChain概述
  • 【MCU控制 初级手札】1.1 电阻
  • CUDA 环境下 `libcuda.so` 缺失问题解决方案
  • 自注意力机制:让 AI 像人类一样「读懂」上下文
  • 明远智睿SSD2351:开启嵌入式系统开发新时代
  • WebApplicationType.REACTIVE 的webSocket
  • dotnet命令详解
  • linux的数据库与web服务器
  • LSTM入门案例(时间序列预测)
  • 平升智慧水务整体解决方案,大数据驱动的智慧水务,让城市供水更智能
  • 康谋分享 | 破解数据瓶颈:智能汽车合成数据架构与应用实践
  • 改进_开源证券_VCF_多尺度量价背离检测因子!
  • 【从0-1的JavaScript】第1篇:JavaScript的引入方式和基础语法
  • 第五章 管道工程 5.2 燃气管道
  • 数据库第三次作业
  • 脚手架新建Vue2/Vue3项目时,项目文件内容的区别
  • yolo-world环境配置
  • 【PCIe 总线及设备入门学习专栏 5.1.1 -- PCIe PERST# 信号的作用】
  • 关于实习的经验贴
  • eSearch识屏 · 搜索 v14.3.0
  • Redis集群搭建(主从、哨兵、读写分离)
  • netstat -tlnp | grep 5000
  • 3.创建表-demo
  • 进程的内存映像,只读区,可读写区,堆,共享库,栈详解
  • 23.将整数转换为罗马数字
  • 磁悬浮轴承的“眼睛”:位移测量核心技术深度解析
  • 【监控实战】Grafana自动登录如何实现
  • 关于tresos Studio(EB)的MCAL配置之FEE