上面的例子只有单个源文件。现在假如把 power
函数单独写进一个名为 MathFunctions.c
的源文件里,使得这个工程变成如下的形式:
./Demo2
|
+--- main.cc
|
+--- MathFunctions.cc
|
+--- MathFunctions.h
这个时候,CMakeLists.txt 可以改成如下的形式:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo2)
# 指定生成目标
add_executable(Demo main.cc MathFunctions.cc)
唯一的改动只是在 add_executable
命令中增加了一个 MathFunctions.cc
源文件。这样写当然没什么问题,但是如果源文件很多,把所有源文件的名字都加进去将是一件烦人的工作。更省事的方法是使用 aux_source_directory
命令,该命令会查找指定目录下的所有源文件,然后将结果存进指定变量名。其语法如下:
aux_source_directory(<dir> <variable>)
因此,可以修改 CMakeLists.txt 如下:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo2)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable(Demo ${DIR_SRCS})
这样,CMake 会将当前目录所有源文件的文件名赋值给变量 DIR_SRCS
,再指示变量 DIR_SRCS
中的源文件需要编译成一个名称为 Demo
的可执行文件。
xuke@ubuntu:~/work/cmake-demo/Demo2$ ls
CMakeLists.txt main.cc MathFunctions.cc MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo2$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xuke/work/cmake-demo/Demo2
xuke@ubuntu:~/work/cmake-demo/Demo2$ ls
CMakeCache.txt cmake_install.cmake main.cc MathFunctions.cc
CMakeFiles CMakeLists.txt Makefile MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo2$ make
Scanning dependencies of target Demo
[ 33%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[ 66%] Building CXX object CMakeFiles/Demo.dir/MathFunctions.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
xuke@ubuntu:~/work/cmake-demo/Demo2$ ./Demo 3 2
3 ^ 2 is 9
现在进一步将 MathFunctions.h
和 MathFunctions.cc
文件移动到 math
目录下。
./Demo3
|
+--- main.cc
|
+--- math/
|
+--- MathFunctions.cc
|
+--- MathFunctions.h
对于这种情况,需要分别在项目根目录 Demo3
和 math
目录里各编写一个 CMakeLists.txt
文件。为了方便,我们可以先将 math
目录里的文件编译成静态库再由 main
函数调用。
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo3)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加 math 子目录
add_subdirectory(math)
# 指定生成目标
add_executable(Demo main.cc)
# 添加链接库
target_link_libraries(Demo MathFunctions)
该文件添加了下面的内容: 使用命令 add_subdirectory
指明本项目包含一个子目录 math
,这样 math
目录下的 CMakeLists.txt
文件和源代码也会被处理 。使用命令 target_link_libraries
指明可执行文件 main
需要连接一个名为 MathFunctions
的链接库 。
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})
在该文件中使用命令 add_library
将 math
目录中的源文件编译为静态链接库。
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeLists.txt main.cc math
xuke@ubuntu:~/work/cmake-demo/Demo3$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xuke/work/cmake-demo/Demo3
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeCache.txt cmake_install.cmake main.cc math
CMakeFiles CMakeLists.txt Makefile
xuke@ubuntu:~/work/cmake-demo/Demo3$ cd math/
xuke@ubuntu:~/work/cmake-demo/Demo3/math$ ls
CMakeFiles CMakeLists.txt MathFunctions.cc
cmake_install.cmake Makefile MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo3/math$ cd ..
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeCache.txt cmake_install.cmake main.cc math
CMakeFiles CMakeLists.txt Makefile
xuke@ubuntu:~/work/cmake-demo/Demo3$ make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
xuke@ubuntu:~/work/cmake-demo/Demo3$ ./Demo 3 2
3 ^ 2 is 9
[CMake 入门实战] http://www.hahack.com/codes/cmake/ [代码参考] https://github.com/wzpan/cmake-demo