新聞中心
CMake是一個(gè)跨平臺(tái)的安裝(編譯)工具,可以用簡(jiǎn)單的語(yǔ)句來(lái)描述所有平臺(tái)的安裝(編譯過(guò)程)。他能夠輸出各種各樣的makefile或者project文件,能測(cè)試編譯器所支持的C++特性,類(lèi)似UNIX下的automake。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到富川網(wǎng)站設(shè)計(jì)與富川網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋富川地區(qū)。
一、單文件目錄
1. 編輯C程序文件,命名為main.c
#include
int main(void) {
printf("Hello World.\n");
return 0;
}
2. 編寫(xiě)CMakeLists.txt文件,保存在main.c同路徑下
#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)
#Project Name
project(hello)
#把當(dāng)前目錄(.)下所有源代碼文件和頭文件加入變量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#生成應(yīng)用程序hello(在windows下生成hello.exe)
ADD_EXECUTABLE(hello ${SRC_LIST})
3. 運(yùn)行cmake命令生成MakeFile,再運(yùn)行make命令生成hello可執(zhí)行程序(為防止文件混亂,可建立build目錄,在此目錄下運(yùn)行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. 目錄結(jié)構(gòu)
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test$ tree test
test
├── 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文件
這種多目錄的情況,需要在每個(gè)源文件路徑中分別編寫(xiě)CMakeLists.txt文件,對(duì)應(yīng)這個(gè)例子,需要在test根目錄和print目錄下編寫(xiě)CMakeLists.txt文件。
為了方便,我們可以先將print目錄里的文件編譯成靜態(tài)庫(kù)再由main函數(shù)調(diào)用。
test目錄下的CMakeLists.txt文件:
#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)
#Project Name
project(hello)
#當(dāng)前目錄下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#添加print子目錄
add_subdirectory(print)
#指定生成目標(biāo)
ADD_EXECUTABLE(hello ${SRC_LIST})
#添加鏈接庫(kù)
target_link_libraries(hello printFunc)
print目錄下的CMakeLists.txt文件:
#當(dāng)前目錄下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIB)
#生成鏈接庫(kù)
ADD_LIBRARY(printFunc ${SRC_LIB})
4. 編譯運(yùn)行
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.
當(dāng)前名稱(chēng):Linux下CMake使用實(shí)例
轉(zhuǎn)載來(lái)于:http://fisionsoft.com.cn/article/cohjjpi.html


咨詢
建站咨詢
