2-Cmake项目使用

Cmake项目使用

基本目录结构:

project/
|
+--- CMakeList.txt
|
+--- src/
| |
| +--- main.cpp
| |
| +--- demo1.cpp
| |
| +--- demo2.cpp
|
+--- math/
| |
| +--- MathFunctions.cpp
| |
| +--- MathFunctions.h
| |
| +--- CMakeList.txt
|
+--- bin/
|
+--- build/

project目录下的CMakeList.txt

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)

# 项目信息
project (CountNum)

# 添加 math 和 src 子目录
add_subdirectory(math src)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)

# 指定生成目标和所需源文件
add_executable(CountNum ${DIR_LIB_SRCS})

# 添加链接库
target_link_libraries(CountNum MathFunctions)

math目录下的CMakeList.txt

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})