跟CMake入门(2. 多个文件)的文件差不多
test2
├── CMakeLists.txt
├── main.c
├── math
│ ├── CMakeLists.txt
│ ├── myadd.c
│ └── myadd.h
myadd.c, myadd.h完全一样.
main.c只需要将#include "myadd.h"
改成#include "math/myadd.h"
main.c 同目录的CMakeLists.txt改成
cmake_minimum_required(VERSION 3.0)
project(test1)
aux_source_directory(. DIR_SRCS)
add_subdirectory(math)
add_executable(Demo ${DIR_SRCS})
target_link_libraries(Demo myadd)
说一下最后一句是在链接库,具体可以参考这里
math下的CMakeLists.txt是:
aux_source_directory(. DIR_LIB_SRCS)
add_library(myadd ${DIR_LIB_SRCS})
add_library
是编译成一个静态库
linux下静态链接库是.a
动态链接库是.so
$ mkdir build && cmake ..
$ make
$ ll
CMakeCache.txt CMakeFiles cmake_install.cmake Demo Makefile math
$ ls math
CMakeFiles cmake_install.cmake libmyadd.a Makefile
可以看到math文件夹下有我们编译的.a
静态库