cmake使用
cmake 與autoconf automake使用的舉例
目錄
一、簡(jiǎn)介
二、示例過(guò)程
1、源文件
2、生成CmakeLists.txt
3、使用cmake命令生成Makefile編譯
三、總結(jié)
一、簡(jiǎn)介
CMake是開(kāi)源、跨平臺(tái)的構(gòu)建工具,在需要編譯的目錄中,編輯簡(jiǎn)單的CMakeLists.txt配置文件,即可通過(guò)cmake命令來(lái)生成Makefile。非常好用。
?
二、示例過(guò)程
1、源文件
在inc目錄下為頭文件
在src目錄下為源文件
其中編譯過(guò)程依賴于libm.so
root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# tree . ├── inc │ └── example.h └── src└── example.croot@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# cat src/example.c #include <stdio.h> #include <math.h> #include "example.h"int is_prime_number(int num) {int i = 0;int j = 0;int max = 0;int prime = 1;max = sqrt(num);for(i = 2; i < num; i++){for(j = 2; j <= max; j++){if(num == i*j ){prime = 0;break;}}if(0 == prime)break;}return prime; }int main(void) {int i = 0;for(i = 2; i < 100; i++){if(is_prime_number(i)){printf("%4d is prime number, WXY=%s.\n", i, WXY);}} } root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# cat inc/example.h #define WXY "wangxinyu" root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket#?
2、生成CmakeLists.txt
root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# vi CMakeLists.txt 1 cmake_minimum_required(VERSION 3.11)2 PROJECT(example)3 4 INCLUDE_DIRECTORIES(./inc)5 LINK_DIRECTORIES("/lib/x86_64-linux-gnu/")6 7 AUX_SOURCE_DIRECTORY(src DIR_SRC)8 9 add_executable(example ${DIR_SRC})10 TARGET_LINK_LIBRARIES(example m)Cmake有一定語(yǔ)法結(jié)構(gòu),與Makefile相似。在這里僅舉簡(jiǎn)單示例,不做深入講解,意在了解過(guò)程。
第4行指定了頭文件路徑。
第5行指定庫(kù)的路徑 。
第7行添加src為源文件目錄
第9行源文件生成example可執(zhí)行文件
第10行生成時(shí)引用libm.so
?
3、使用cmake命令生成Makefile編譯
oot@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# cmake . -- The C compiler identification is GNU 8.3.0 -- The CXX compiler identification is GNU 8.3.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/wangxinyu/work/temp/cmaket root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# ls CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt inc Makefile src root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# make Scanning dependencies of target example [ 50%] Building C object CMakeFiles/example.dir/src/example.c.o [100%] Linking C executable example [100%] Built target example root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# ls CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt example inc Makefile src root@wangxinyu-PC:/home/wangxinyu/work/temp/cmaket# ./example 2 is prime number, WXY=wangxinyu.3 is prime number, WXY=wangxinyu.5 is prime number, WXY=wangxinyu.7 is prime number, WXY=wangxinyu.11 is prime number, WXY=wangxinyu. ......?
三、總結(jié)
相比automake生成Makefile, cmake的生成過(guò)程非常簡(jiǎn)單,只需配置CmakeLists.txt一個(gè)文件。簡(jiǎn)單的背后是復(fù)雜,寫一個(gè)好的CamkeLists.txt文件,
不比寫Makefile輕松,其大的優(yōu)勢(shì)更在于跨平臺(tái)。
總結(jié)
- 上一篇: 5.质量管理
- 下一篇: 服务器扩充后问题总结:Value too