代码在git
一般来说,当程序文件比较多时,我们会进行分类管理,把代码根据功能放在不同的目录下,这样方便查找。那么这种情况下如何编写CMakeLists.txt呢?
我们把之前的源文件整理一下(新建2个目录test_func和test_func1),整理好后整体文件结构如下
.
├── build
├── CMakeLists.txt
├── main.c
├── temp
│ └── CMakeLists1.txt
├── test_func
│ ├── testFunc.c
│ └── testFunc.h
└── test_func1
├── testFunc1.c
└── testFunc1.h
把之前的testFunc.c和testFunc.h放到test_func目录下,testFunc1.c和testFunc1.h则放到test_func1目录下。
其中,CMakeLists.txt和main.c在同一目录下,内容修改成如下所示,
cmake_minimum_required (VERSION 2.8)
project (demo)
include_directories (test_func test_func1)
aux_source_directory (test_func SRC_LIST)
aux_source_directory (test_func1 SRC_LIST1)
add_executable (main main.c ${SRC_LIST} ${SRC_LIST1})
mkdir build
cd build
cmake ..
make
./main
这种写法也可以
cmake_minimum_required (VERSION 2.8)
project (demo)
include_directories (test_func test_func1)
aux_source_directory (test_func SRC_LIST)
aux_source_directory (test_func1 SRC_LIST)
#add_executable (main main.c ${SRC_LIST} ${SRC_LIST1})
add_executable (main main.c ${SRC_LIST} )
这里出现了一个新的命令:include_directories。该命令是用来向工程添加多个指定头文件的搜索路径,路径之间用空格分隔。
因为main.c里include了testFunc.h和testFunc1.h,如果没有这个命令来指定头文件所在位置,就会无法编译。当然,也可以在main.c里使用include来指定路径,如下
#include "test_func/testFunc.h"
#include "test_func1/testFunc1.h"
#include “test_func/testFunc.h”
#include “test_func1/testFunc1.h”