|
大家好,上次给大家分享了第一篇 cmake 文章:cmake学习总结(一),今天继续给大家分享cmake。那么废话就不多说,开始内容分享。 一、用好 Cmake,高兴一整天(甚至...): 1、多个源文件,使用命令 aux_source_directory(dir var): 在上一篇文章最后结尾的时候,有一个问题,就是在同一目录下面,有多个源文件的时候,这个时候你不能都往下面第三条命令里面一直手动添加源文件,那工作效率多低啊: cmake_minimum_required(VERSION 2.8)project(main) + \2 s5 X2 b' V8 ^6 E add_executable(main main.c test1.c) 于是乎为了解决这种低效率的操作,在 cmake 里面有一条指令可以完全搞定这个问题;不过为了说明问题,在这之前我又添加了两个文件:test2.c 和 test2.h: root@txp-virtual-machine:/home/txp/test# ls& m' |2 j# T. U1 cmake_install.cmake main.c test1.h touch1.c3 d3 X9 C3 G7 \" e& U6 t CMakeCache.txt CMakeLists.txt Makefile test2.c touch1.h# {- r# ~! a, L8 S6 }; Y CMakeFiles main test1.c test2.h test2.c内容如下: #include <stdio.h>#include "test2.h" " R$ Z! |3 s( u: u: A void func1() { p X/ Q8 Q, w9 S' n3 z printf("i like the cmake\n"); I$ f1 X% p- q- B/ ~& t* Z }- k& J+ j5 D, m: u4 a; C0 v test2.h内容如下: #ifndef _TEST2_H_#define _TEST2_H_ ' V4 c' k- t* \. y/ x! J void func1();* |, D$ h* k( V ( q. i- x5 G- R* J9 M9 C# m% e #endif- `8 e0 i6 r3 i 最后main.c里面调用了func1函数: #include <stdio.h>0 p5 i' U" t9 h A#include "test1.h"5 I3 {* @( v# l4 o6 W. r7 ] #include "test2.h" int main(void) { func1();3 l& E6 a/ ~' P& f func(8);7 a v! z1 F f) v3 Q/ |3 K printf("TXP嵌入式\n");7 F1 e B; M5 | return 0; 0 |# O5 g# D& l1 g' l6 ^. _ }. E* ~" I1 S3 R& j/ S 7 Y( k4 |9 a* ?/ X! ? 接下来我们的重点就来了,在cmake里面可以使用aux_source_directory(dir var)就可以搞定上面效率低的问题,接下来我们在CMakeLists.txt这样操作: cmake_minimum_required(VERSION 2.8)' N, w9 K( m Z! |1 I5 i/ g/ _+ O2 R1 T6 j$ Y8 C/ i% F u project(main)% V% t- h) v4 k aux_source_directory(. SRC_LIST)) @2 i$ q9 p5 `' j # ^+ v* k g5 v' H G2 Z add_executable(main ${SRC_LIST}) ' C! c: x" u9 \ 然后再进行编译: root@txp-virtual-machine:/home/txp/test# cmake .-- Configuring done8 g& `& ]' V: W6 P -- Generating done -- Build files have been written to: /home/txp/test # s% U- t% Y7 X root@txp-virtual-machine:/home/txp/test# make Scanning dependencies of target main [ 25%] Building C object CMakeFiles/main.dir/main.c.o {7 N& F+ k+ A1 e8 |1 z" N [ 50%] Linking C executable main + N1 d8 e. q9 R/ L2 f root@txp-virtual-machine:/home/txp/test# ./main i like the cmake; y0 @+ e* z- `) ]- S the b is 8 TXP嵌入式( b$ S9 k0 F( |! u* G9 z , n0 l5 u; s" z- c: C 说明: aux_source_directory(. SRC_LIST):表示是把当当前目录下的所有源文件都添加到源列表变量里面去,最后用add_executable(main ${SRC_LIST})把所有有用的源文件加工成目标文件main。不过这方法也有他的缺点,就是把当前目录下的源文件都添加到变量SRC_LIST,如果我们不需要一些没有用的文件(只要拿到所需的源文件就行),可以进行这样操作: cmake_minimum_required(VERSION 2.8)project(main) . V0 ^$ U9 M4 R9 b( W set(SRC_LIST( R% a" U. L R/ G ./main.c ./test1.c& p* V! H9 ^' ~$ R, C ./test2.c& V( P$ `9 Y# O. M )7 k# Y: z5 K1 B" [ add_executable(main ${SRC_LIST}) V5 _$ o! S: R1 \& l2 I9 b ( A& {' _' @' U 这样是能够通过编译的: root@txp-virtual-machine:/home/txp/test# cmake .0 o) j- J# g' D% M9 q7 _-- Configuring done) j/ K) G6 y! N/ g+ A2 O -- Generating done0 P$ l$ ^2 ^: L; K! R -- Build files have been written to: /home/txp/test root@txp-virtual-machine:/home/txp/test# make [100%] Built target main / |1 n: U+ N F. e 2、在上面的例子中,我们会发现同一目录下源文件比较乱,所以在cmake里面有这样的规则,可以把相同类型以及相关的源文件放到同一个目录,比如说,现在我在test目录下创建test1和test2两个目录文件,并同时把test1.c、test1.h、test2.c、test2.h分别放到这两个目录下去: root@txp-virtual-machine:/home/txp/test# mkdir -p test1 test21 ]% `" [4 o7 O! j5 R- Kroot@txp-virtual-machine:/home/txp/test# ls3 u7 G$ p0 E4 \8 t5 g @ CMakeFiles main test1 test2 1 cmake_install.cmake main.c test1.c test2.c s9 |1 P$ E8 Q- P& B CMakeCache.txt CMakeLists.txt Makefile test1.h test2.h 然后把相关文件一到这两个目录文件下去: root@txp-virtual-machine:/home/txp/test# mv test1.c test1.h test1. z1 O4 p( S) `1 q, Mroot@txp-virtual-machine:/home/txp/test# mv test2.c test2.h test2 root@txp-virtual-machine:/home/txp/test# ls7 C2 M( \1 D9 H6 y1 o# f: Q' O @ CMakeCache.txt cmake_install.cmake main Makefile test2 1 CMakeFiles CMakeLists.txt main.c test1 g) ~; ` g* m" ?$ w: W 1 d! m8 g4 f5 F( q+ ?2 G! s# h root@txp-virtual-machine:/home/txp/test# tree # T" o& M4 w6 i6 g ├── cmake_install.cmake# o) L+ z* l+ w" E( X( p ├── CMakeLists.txt ├── main ├── main.c ├── Makefile ├── test1 │ ├── test1.c │ └── test1.h8 Y% v) ?' p$ m( H3 ` C0 D5 D0 \ └── test2) H" D P; |2 } ├── test2.c └── test2.h 0 D6 G# g3 ~% t2 l9 w% v 然后这个时候要修改CMakeLists.txt里面的规则属性了: cmake_minimum_required(VERSION 2.8)5 E5 T9 \% F9 r( Z1 x9 }: {8 B" G5 w% H& N project(main) include_directories(test1 test2); I+ j' w& {) F aux_source_directory(test1 SRC_LIST)+ H7 R3 t/ S3 e- C aux_source_directory(test2 SRC_LIST1)2 C7 K J4 O; H8 l4 { add_executable(main main.c ${SRC_LIST} ${SRC_LIST1})- _+ c; u$ a: S' @$ m ' P% Q* C" E9 x3 A+ L/ j7 \ 然后编译输出,也是能够通过的: root@txp-virtual-machine:/home/txp/test# cmake .) j4 w( C0 j; Y8 t$ Z) B: ~( t-- Configuring done -- Generating done -- Build files have been written to: /home/txp/test root@txp-virtual-machine:/home/txp/test# make Scanning dependencies of target main [ 25%] Building C object CMakeFiles/main.dir/main.c.o [ 50%] Building C object CMakeFiles/main.dir/test1/test1.c.o* F7 q5 F4 Y0 {# q( b( | [ 75%] Building C object CMakeFiles/main.dir/test2/test2.c.o4 q; t, f) \1 [+ I; F* V [100%] Linking C executable main [100%] Built target main root@txp-virtual-machine:/home/txp/test# ls3 g# h" b0 o: S3 F @ CMakeCache.txt cmake_install.cmake main Makefile test2- ~1 ^6 ]* X4 v# c 1 CMakeFiles CMakeLists.txt main.c test12 g- z9 V+ H2 u' Y f root@txp-virtual-machine:/home/txp/test# ./main i like the cmake the b is 84 d; ]# B% l/ R! O% y TXP嵌入式 说明: ! W( v: j5 \: Q9 N0 D& H8 D; E 这里出现了一个新的命令:include_directories。该命令是用来向工程添加多个指定头文件的搜索路径,路径之间用空格分隔。 其实在实际开发工程中,一般会把源文件放到src目录下,把头文件放入到include文件下,生成的对象文件放入到build目录下,最终输出的elf文件会放到bin目录下,这样让人看起来一目了然,哈哈,更多详细情况可以看下期分解。以上就是今天的学习总结。希望对你有用。 |
微信公众号
手机版