cmake使用
cmake 與autoconf automake使用的舉例
目錄
一、簡介
二、示例過程
1、源文件
2、生成CmakeLists.txt
3、使用cmake命令生成Makefile編譯
三、總結
一、簡介
CMake是開源、跨平臺的構建工具,在需要編譯的目錄中,編輯簡單的CMakeLists.txt配置文件,即可通過cmake命令來生成Makefile。非常好用。
?
二、示例過程
1、源文件
在inc目錄下為頭文件
在src目錄下為源文件
其中編譯過程依賴于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有一定語法結構,與Makefile相似。在這里僅舉簡單示例,不做深入講解,意在了解過程。
第4行指定了頭文件路徑。
第5行指定庫的路徑 。
第7行添加src為源文件目錄
第9行源文件生成example可執行文件
第10行生成時引用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. ......?
三、總結
相比automake生成Makefile, cmake的生成過程非常簡單,只需配置CmakeLists.txt一個文件。簡單的背后是復雜,寫一個好的CamkeLists.txt文件,
不比寫Makefile輕松,其大的優勢更在于跨平臺。
總結
- 上一篇: 5.质量管理
- 下一篇: 服务器扩充后问题总结:Value too