您好,登錄后才能下訂單哦!
小編給大家分享一下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 Versioncmake_minimum_required(VERSION 3.6.1)#Project Nameproject(hello)#把當前目錄(.)下所有源代碼文件和頭文件加入變量SRC_LISTAUX_SOURCE_DIRECTORY(. SRC_LIST)#生成應用程序hello(在windows下生成hello.exe)ADD_EXECUTABLE(hello ${SRC_LIST})
3. 運行cmake命令生成MakeFile,再運行make命令生成hello可執(zhí)行程序(為防止文件混亂,可建立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_Hextern void print();#endif
print.c
#include extern void print(){ printf("Hello World.\n"); }
3. 編輯CMakeLists.txt文件
這種多目錄的情況,需要在每個源文件路徑中分別編寫CMakeLists.txt文件,對應這個例子,需要在test根目錄和print目錄下編寫CMakeLists.txt文件。
為了方便,我們可以先將print目錄里的文件編譯成靜態(tài)庫再由main函數(shù)調用。
test目錄下的CMakeLists.txt文件:
#Minimum required CMake Versioncmake_minimum_required(VERSION 3.6.1)#Project Nameproject(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.
以上是“Linux下CMake怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。