大家好,我是雜燴君。今天,我們就通過(guò)一個(gè)簡(jiǎn)單的Linux C 語(yǔ)言實(shí)戰(zhàn)案例,帶你深度剖析嵌入式 CMake 的核心套路。
使用 CMake 的好處
平臺(tái)無(wú)關(guān)性:一套配置文件,適配 ARM、x86 等多種架構(gòu)。
模塊化管理:輕松分離驅(qū)動(dòng)層、庫(kù)和應(yīng)用程序,代碼結(jié)構(gòu)一目了然。
自動(dòng)化構(gòu)建:從編譯、鏈接到生成固件,一條make
命令搞定。
CMake例子
我們的例子包含三個(gè)核心模塊:
靜態(tài)庫(kù)(strutils):字符串處理工具。
動(dòng)態(tài)庫(kù)(mathutils):數(shù)學(xué)計(jì)算工具。
應(yīng)用程序(myapp):主程序,調(diào)用上述兩個(gè)庫(kù)。
1.項(xiàng)目結(jié)構(gòu)
linux-cmake-example/
├── CMakeLists.txt ? ? ? ? ?# 頂層配置文件
├── lib/
│ ? ├── CMakeLists.txt ? ? ?# 庫(kù)模塊配置
│ ? ├── include/
│ ? │ ? ├── string_utils.h ?# 字符串庫(kù)頭文件
│ ? │ ? └── math_utils.h ? ?# 數(shù)學(xué)庫(kù)頭文件
│ ? ├── string_utils.c ? ? ?# 字符串庫(kù)實(shí)現(xiàn)
│ ? └── math_utils.c ? ? ? ?# 數(shù)學(xué)庫(kù)實(shí)現(xiàn)
├── app/
│ ? ├── CMakeLists.txt ? ? ?# 應(yīng)用程序配置
│ ? ├── include/
│ ? └── main.c ? ? ? ? ? ? ?# 主程序代碼
└──?test/
? ? ├── CMakeLists.txt ? ? ?# 測(cè)試模塊配置
? ? └── test_string_utils.c?# 字符串庫(kù)測(cè)試代碼
每個(gè)文件夾都有獨(dú)立的CMakeLists.txt
,各司其職,這種 “分而治之” 的思想是嵌入式項(xiàng)目管理的精髓。
2. 頂層 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(LinuxSystem C)
# 核心編譯選項(xiàng):開(kāi)啟嚴(yán)格檢查 + 優(yōu)化
set(CMAKE_C_STANDARD 11)
add_compile_options(
? -Wall -Wextra -Werror
? -O2
)
# 引入子模塊:庫(kù)、應(yīng)用、測(cè)試
add_subdirectory(lib)
add_subdirectory(app)
add_subdirectory(test)
關(guān)鍵點(diǎn):
add_compile_options
統(tǒng)一設(shè)置編譯規(guī)則。
add_subdirectory
讓 CMake 自動(dòng)掃描子目錄的配置文件,簡(jiǎn)化頂層邏輯。
3. 庫(kù)模塊:靜態(tài)庫(kù)、動(dòng)態(tài)庫(kù)
靜態(tài)庫(kù)(strutils)配置
add_library(strutils STATIC
? string_utils.c
)
target_include_directories(strutils PUBLIC include)
關(guān)鍵點(diǎn):
add_library(strutils STATIC)
:創(chuàng)建靜態(tài)庫(kù),代碼將直接嵌入可執(zhí)行文件。
PUBLIC include
:將頭文件路徑暴露給依賴(lài)它的模塊,確保其他代碼能調(diào)用庫(kù)函數(shù)。
動(dòng)態(tài)庫(kù)(mathutils)配置
add_library(mathutils SHARED
? math_utils.c
)
target_include_directories(mathutils PUBLIC include)
關(guān)鍵點(diǎn)::SHARED
- 選項(xiàng):生成獨(dú)立的動(dòng)態(tài)鏈接庫(kù),可被多個(gè)程序復(fù)用。實(shí)際部署時(shí),需確保庫(kù)文件在系統(tǒng)庫(kù)路徑中,否則運(yùn)行會(huì)報(bào)錯(cuò)。
4. 應(yīng)用程序:整合所有模塊
add_executable(myapp
? main.c
)
target_include_directories(myapp PRIVATE include)
target_link_libraries(myapp?
? strutils?
? mathutils
? m # 鏈接標(biāo)準(zhǔn)數(shù)學(xué)庫(kù)
)
關(guān)鍵點(diǎn)::
target_link_libraries
將靜態(tài)庫(kù)、動(dòng)態(tài)庫(kù)與主程序綁定。
PRIVATE include
:僅在應(yīng)用程序內(nèi)部使用頭文件,不對(duì)外暴露。
5. 測(cè)試模塊
add_executable(test_strutils
? test_string_utils.c
)
target_link_libraries(test_strutils
? strutils
? cmocka # 引入單元測(cè)試框架
)
add_test(NAME test_strutils COMMAND test_strutils)
關(guān)鍵點(diǎn):使用cmocka
框架編寫(xiě)單元測(cè)試。
add_test
定義測(cè)試任務(wù),make test
一鍵運(yùn)行。
6、編譯執(zhí)行
CMake進(jìn)階技巧
1、?交叉編譯:在頂層CMakeLists.txt
中添加工具鏈配置,適配 ARM 等嵌入式平臺(tái)。
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
2、代碼覆蓋率分析:引入CodeCoverage
模塊,定位測(cè)試盲區(qū)
if(CMAKE_BUILD_TYPE MATCHES Coverage)
? include(CodeCoverage)
? append_coverage_compiler_flags()
? setup_target_for_coverage_lcov(
? ? NAME coverage
? ? EXECUTABLE ctest -j ${PROCESSOR_COUNT}
? ? DEPENDENCIES test_strutils
? )
endif()
3、自動(dòng)化部署:通過(guò)install
命令,一鍵將程序和庫(kù)部署到目標(biāo)系統(tǒng)
install(TARGETS myapp DESTINATION bin)
install(TARGETS strutils mathutils DESTINATION lib)
install(DIRECTORY include/ DESTINATION include)
本文例子可在公眾號(hào)聊天界面回復(fù)關(guān)鍵詞:CMake例子,即可獲取。