参考: https://www.hahack.com/codes/cmake/

1. 单目标文件

main.c

#include <stdio.h>
#include <stdlib.h> double power(double base, int exponent)
{
int result = base;
int i; if (exponent == ) {
return ;
} for(i = ; i < exponent; ++i){
result = result * base;
}
return result;
} int main(int argc, char *argv[])
{
if (argc < ){
printf("Usage: %s base exponent \n", argv[]);
return ;
} double base = atof(argv[]);
int exponent = atoi(argv[]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result); return ;
}

CMakeLists.txt

cmake_minimum_required (VERSION 2.8)   # 指定最低的版本号

# 项目信息
project (Demo01_cmake) # 指定生成目标
add_executable(Demo_exe main.c) # Demo_exe为生成的可执行文件 main.c为依赖文件

然后

cmake .    # 生成makeFile文件
make # 执行make

2. 同一个目录,多个源文件

calc_power.h

double power(double a, int b);

calc_power.c

#include "calc_power.h"

double power(double base, int exponent)
{
int result = base; int i; if (exponent == ) {
return ;
} for(i = ; i < exponent; ++i){
result = result * base;
} return result;
}

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "calc_power.h" int main(int argc, char *argv[])
{
if (argc < ){
printf("Usage: %s base exponent \n", argv[]);
return ;
} double base = atof(argv[]);
int exponent = atoi(argv[]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result); return ;
}

CMakeLists.txt

cmake_minimum_required (VERSION 2.8)

# 项目信息
project (Demo2_cmake) # 指定生成目标
# add_executable(Demo2_cmake main.c calc_power.c) # 如果源文件多,这样写麻烦 # 该命令会查找指定目录下的所有源文件, 然后将结果存到指定变量名
# 如下: 查找当前目录下的所有源文件, 并将名称报错到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS) # 指定生成目标
# CMake 会将当前目录所有源文件名赋值给变量 DIR_SRCS
# 再指示变量 DIR_SRCS 中源文件 编译成一个 Demo2_exe的可执行文件
add_executable(Demo2_exe ${DIR_SRCS})

再执行

cmake .
make

3. 多目录,多文件

main.c

#include <stdio.h>
#include <stdlib.h>
#include "./mymath/calc_power.h" int main(int argc, char *argv[])
{
if (argc < ){
printf("Usage: %s base exponent \n", argv[]);
return ;
} double base = atof(argv[]);
int exponent = atoi(argv[]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result); return ;
}

mymath的目录下CMakeLists.txt

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS) # 生成链接库
# 使用add_library 将src目录中的源文件编译成静态链接库
add_library(calc_power $(DIR_LIB_SRCS))

main.c所在的目录下的CMakeLists.txt

# Make 最低版本号要求
cmake_minimum_required (VERSION 2.8) # 项目信息
project (Demo3_cmake) # 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS) # 添加 math 子目录, 这样mymath目录下的 CMakeLists.txt文件和源代码也会被处理
add_subdirectory(mymath) # 指定生成目标
add_executable(Demo3_exe main.c) # 添加链接库, 指明可执行文件Demo3_exe 需要连接一个名为 calc_power的链接库
target_link_libraries(Demo3_exe calc_power)

然后:

cmake .
make

最新文章

  1. TensorFlow官方文档中文版
  2. 深入理解Java虚拟机(一)、Java内存区域与内存溢出异常
  3. Connecting my Particle Photon Internet of Things device to the Azure IoT Hub(Translation)
  4. Core Animation编程指南
  5. cJSON_hacking
  6. Unable to resolve module LinkedStateMixin
  7. Electron开发环境部署
  8. Foundation 框架 NSArray、NSMutableArray排序
  9. 微信小程序初探【类微信UI聊天简单实现】
  10. Windows下安装配置go
  11. ABP中的Filter(下)
  12. Ubuntu 开启远程登录 SSH 的安装和配置
  13. UWP简单示例(二):快速开始你的3D编程
  14. iOS - iphoneX系列 - 全局配置的基本信息
  15. C#调用Delphi的dll之详解
  16. 手机e.pageX和e.pageY无效的原因
  17. [报错] Xcode Error-Could not insert new outlet connection: Could not find any information for the class named &quot;xxx&quot;
  18. WPF 同一窗口内的多线程 UI(VisualTarget)
  19. windows7配置python和django的开发环境
  20. centos 7 mini版中安装Python3.x

热门文章

  1. [20190416]process allocation latch.txt
  2. hbase 工作原理
  3. redis数据库安装 redis持久化及主从复制
  4. 基于nginx搭建yum源服务器
  5. Java基础系列--05_面向对象
  6. June 28th. 2018, Week 26th. Thursday
  7. 数据库MySQL和Redis实践
  8. DDctf 新得
  9. Luogu4655 [CEOI2017]Building Bridges
  10. 史上最全的Spring-Boot-Starter开发手册