CMake教程!
一、单个源文件
1、新建demo文件夹内含有①main.cc ② CMakeLists.txt两个文件
文件内容如下:
main.cc主要打包一个幂运算的工程文件
#include <stdio.h>
#include <stdlib.h>
/**
* power - Calculate the power of number.
* @param base: Base value.
* @param exponent: Exponent value.
*
* @return base raised to the power exponent.
*/
double power(double base, int exponent)
{
int result = base;
int i;
if (exponent == 0) {
return 1;
}
for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
CMakeLists.txt文件如下:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo)
# 指定生成目标
add_executable(Demo main.cc)
- cmake_minimum_required:指定运行此配置文件所需的 CMake 的最低版本;
- project:参数值是 Demo1,该命令表示项目的名称是 Demo 。
- add_executable:将名为 main.cc 的源文件编译成一个名称为 Demo 的可执行文件。
2、编译项目
前提自行安装①Cmake软件包、②make编译命令行: make编译命令行
为了防止windows cmake没有生成Makefile为此:执行 参考
在文件目录下cmd回车打开后台
- 1、删除目录下CMakeCache.txt(如果有的话);
- 2、执行cmake . -G “Unix Makefiles”
- 3、然后可以执行make了。
- 4、在 Windows 系统下,您可以使用 .\Demo 来运行当前目录下的可执行文件。
二、同源目录多个源文件
一样的操作!!!现在假如把 power 函数单独写进一个名为 MathFunctions.c 的源文件里,使得这个工程变成如下的形式:
// MathFunctions.cc
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"
double power(double base, int exponent)
{
int result = base;
int i;
if (exponent == 0) {
return 1;
}
for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
// MathFunctions.h
double power(double base, int exponent);
// demo.cc
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
// CMakeLists.txt
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo2)
# 指定生成目标
add_executable(Demo2 demo.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})
三、多个目录,多个源文件
对于这种情况,需要分别在项目根目录 Demo3 和 math 目录里各编写一个 CMakeLists.txt 文件。为了方便,我们可以先将 math 目录里的文件编译成静态库再由 main 函数调用。
根目录中的 CMakeLists.txt :
# CMake 最低版本号要求
include_directories(math)
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)
该文件添加了下面的内容: 第3行,使用命令 add_subdirectory 指明本项目包含一个子目录 math,这样 math 目录下的 CMakeLists.txt 文件和源代码也会被处理 。第6行,使用命令 target_link_libraries 指明可执行文件 main 需要连接一个名为 MathFunctions 的链接库 。
math中的 CMakeLists.txt :
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})
在该文件中使用命令 add_library 将 src 目录中的源文件编译为静态链接库。
自定义编译选项
修改 CMakeLists 文件
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo4)
# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
# 是否使用自己的 MathFunctions 库
option (USE_MYMATH
"Use provided math implementation" ON)
# 是否加入 MathFunctions 库
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/math")
add_subdirectory (math)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable(Demo ${DIR_SRCS})
target_link_libraries (Demo ${EXTRA_LIBS})
是否加入 MathFunctions 库
这段代码是一个条件语句,它检查 USE_MYMATH 变量的值是否为真。如果为真,则执行以下操作:
使用 include_directories 命令将 P R O J E C T S O U R C E D I R / m a t h 目录添加到头文件搜索路径中。 {PROJECT_SOURCE_DIR}/math 目录添加到头文件搜索路径中。 PROJECTSOURCEDIR/math目录添加到头文件搜索路径中。{PROJECT_SOURCE_DIR} 变量表示项目的根目录。
使用 add_subdirectory 命令将 math 子目录添加到构建中。这意味着 CMake 会进入 math 目录,并处理该目录下的 CMakeLists.txt 文件。
将 MathFunctions 库添加到 EXTRA_LIBS 变量中。这个变量可以在后面用于指定链接时需要使用的库。
如果 USE_MYMATH 变量的值为假,则不执行任何操作。