文件多层次
text.file
- main.cpp
- CMakeLists.txt
- head.file
- main.h
- function.cpp
- CMakeLists.txt
最内层的CMakeList.txt
#查找当前目录下的所有源文件
#并将名称保存到DIR_LIB_SRCS变量
aux_source_directory(. DIR_LIB_SRCS)
#生成链接库cubes
add_library(cubes ${DIR_LIB_SRCS})
最外层的CMakeList.txt
#项目信息
project(main)
#版本信息
cmake_minimum_required(VERSION 2.6)
#包含的头文件
add_subdirectory(head)
#当前目录的源文件赋值给 DIR_SRCS
aux_source_directory(. DIR_SRCS)
#链接库静态库
link_libraries(-lncurses)
#将指示变量DIR_SRCS 中的源文件编译成为一个main的可执行文件
add_executable(main ${DIR_SRCS})
#链接动态库
target_link_libraries(main cubes)
link_libraries与target_link_libraries区别
- link_libraries:链接静态库,比如g++ -lxx,在add_executable之前使用
- target_link_libraries:链接动态库,比如自己写的库,在add_executable之后使用
- 这个和静态库和动态库的链接相关,静态库在编译时链接,动态库在运行时链接,记住这个就比较好区分了