CMake入门

CMake是一个跨平台的安装编译工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake

一、单个文件

main.cc

#include <stdio.h>
#include <stdlib.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;
} 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(CMakeLists 不区分大小写)

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 项目信息
project(Demo01)
# 指定生成目标
add_executable(Demo01 main.cc)

执行命令

cmake . # 执行cmake,自动生成对应的makefile
make # 生成二进制文件Demo01

编译输出

➜  cmakedemo cmake .
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/fanghao/Desktop/cmakedemo
➜ cmakedemo make
Scanning dependencies of target Demo01
[ 50%] Building CXX object CMakeFiles/Demo01.dir/main.cc.o
[100%] Linking CXX executable Demo01
[100%] Built target Demo01
➜ cmakedemo ./Demo01 5 4
5 ^ 4 is 625

二、多个文件

修改项目结构如下:

➜  cmakedemo tree
.
├── CMakeLists.txt
├── MathFunction.cc
├── MathFunctions.h
└── main.cc 0 directories, 4 files

CMakeLists.txt也应当添加多个文件

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 命令不区分大小写
# 项目信息
project(Demo02) #把源文件变成变量
aux_source_directory(. DIR_SRCS) # 指定生成目标
add_executable(Demo02 ${DIR_SRCS})

三、子模块

修改项目结构如下:

➜  cmakedemo tree
.
├── CMakeLists.txt
├── main.cc
└── math
├── CMakeLists.txt
├── MathFunction.cc
└── MathFunctions.h 1 directory, 5 files

cmakedemo目录下的CMakeLists.txt

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 命令不区分大小写
# 项目信息
set(NAME Demo03)
project(${NAME}) # 添加math子目录,会执行math目录下的cmake
add_subdirectory(math) # 指定生成目标
add_executable(${NAME} main.cc) # 添加链接库
target_link_libraries(${NAME} MathFunctions)

math目录下的CMakeLists.txt

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})

此时的输出,注意编译了子模块

➜  cmakedemo cmake .
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchai
ns/XcodeDefault.xctoolchain/usr/bin/cc-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchai
ns/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolch
ains/XcodeDefault.xctoolchain/usr/bin/c++-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolch
ains/XcodeDefault.xctoolchain/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: /Users/fanghao/Desktop/cmakedemo
➜ cmakedemo make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunction.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo03
[ 75%] Building CXX object CMakeFiles/Demo03.dir/main.cc.o
[100%] Linking CXX executable Demo03
[100%] Built target Demo03
➜ cmakedemo ./Demo03 3 4
3 ^ 4 is 81

生成的文件

➜  cmakedemo tree -L 2
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.11.3
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── Demo03.dir
│ ├── Makefile.cmake
│ ├── Makefile2
│ ├── TargetDirectories.txt
│ ├── cmake.check_cache
│ ├── feature_tests.bin
│ ├── feature_tests.c
│ ├── feature_tests.cxx
│ └── progress.marks
├── CMakeLists.txt
├── Demo03
├── Makefile
├── cmake_install.cmake
├── main.cc
└── math
├── CMakeFiles
├── CMakeLists.txt
├── Makefile
├── MathFunction.cc
├── MathFunctions.h
├── cmake_install.cmake
└── libMathFunctions.a 6 directories, 22 files

可以注意到生成了libMathFunctions.a

三、第三方库

glewglfw为例

cmake_minimum_required(VERSION 3.9)
project(helloworld) set(CMAKE_CXX_STANDARD 11) # 自动寻找库
find_library(OPENGL opengl) # 添加头文件
set(GLEW_H /usr/local/Cellar/glew/2.1.0/include/GL)
set(GLFW_H /usr/local/Cellar/glfw/3.2.1/include/GLFW)
include_directories(${GLEW_H} ${GLFW_H}) # 添加目标链接
set(GLEW_LINK /usr/local/Cellar/glew/2.1.0/lib/libGLEW.2.1.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/3.2.1/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK}) # 执行编译命令
set(SOURCE_FILES main.cc)
add_executable(helloworld ${SOURCE_FILES})

四、小结

cmake作为跨平台的项目管理工具,相比make更加简洁易用。

CMake入门的更多相关文章

  1. CMake 入门实战 | HaHack

    CMake 入门实战 | HaHack undefined

  2. CMake入门(二)

    CMake入门(二) 最后更新日期:2014-04-25 by kagula 阅读前提:<CMake入门(一)>.Linux的基本操作 环境: Windows 8.1 64bit英文版.V ...

  3. CMake入门教程(转帖)

    本文转自:https://www.cnblogs.com/never--more/p/6921837.html CMake入门教程 参考文献:http://www.ibm.com/developerw ...

  4. CMake入门-04-自定义编译选项

    工作环境 系统:macOS Mojave 10.14.6 CMake: Version 3.15.0-rc4 Hello,World! - 自定义编译选项 CMake 允许为项目增加编译选项,从而可以 ...

  5. CMake入门-03-还是HelloWorld

    工作环境 系统:macOS Mojave 10.14.6 CMake: Version 3.15.0-rc4 Hello,World! 扩展-math 目录里的文件编译成静态库再由 main 函数调用 ...

  6. C++ CMake 入门实战[转载]

    C++ CMake 入门实战 2016-11-05 CMake用于跨平台的编译系统,对于通常的c/c++工程,都是通过make来进行编译的,CMake可以通过指令生成Makefile文件来指导整个项目 ...

  7. VScode 使用 CMake 入门

    参考 CMake 入门实战 在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下: 编写 CMake 配置文件 CMakeLists.txt . 执行命令 cmake PA ...

  8. CMake 入门实战【转】

    本文转载自:http://www.hahack.com/codes/cmake/ 什么是 CMake All problems in computer science can be solved by ...

  9. CMake入门以及学习笔记

    使用cef3替代chromium内核开发产品过程中,第一次接触到系统构建,使用了最常见的CMake.CMake虽然在构建系统中用的比较多,但是使用到的程序员还是很少的.现在在国内能找到的相关资料和博客 ...

随机推荐

  1. Vue(项目踩坑)_解决vue中axios请求跨域的问题

    一.前言 今天在做项目的时候发现axios不能请求跨域接口 二.主要内容 1.之前直接用get方式请求聚合数据里的接口报错如下 2.当前请求的代码 3.解决方法 (1)在项目目录中依次找到:confi ...

  2. PR视频剪辑

    PR视频剪辑 新手问题1: 将素材导入到Adobe Premiere Pro CC后为什么无法拖入到时间轴上 解决办法:没有建立有序列所致,CC不会一开始就让你新建序列,图中间处写的好清楚“无序列”. ...

  3. SSH框架之hibernate《三》

    Hibernate03     一.多表设计         1.1多表设计的总则             问题:我们为什么要学习多表映射?             答:                ...

  4. Chrome firefox ie等浏览器空格&nbsp;宽度不一样

    用半角空格 或者全角空格   相当于半格中文字符的宽度, 相当于一个中文字符宽度. 注:在chrome中两个 占一个汉字的宽度;,而在IE.firefox中四个 才占一个汉字的宽度.

  5. Stm32型号查阅手册

  6. BootStrap分页教程

    https://www.cnblogs.com/laowangc/p/8875526.html https://www.cnblogs.com/yinglunstory/p/6092834.html ...

  7. torch.utils.data.DataLoader对象中的迭代操作

    关于迭代器等概念参考:https://www.cnblogs.com/zf-blog/p/10613533.html 关于pytorch中的DataLoader类参考:https://blog.csd ...

  8. SQL join 连接时 条件加在 on后面和 where 的区别

    task 是用户任务表,manageuser是用户表,以left join 为参考: 此时主表是task,三条sql语句:注意区别.第一句无筛选条件,第二句筛选条件在on后面,第三句sql的筛选语句放 ...

  9. eclipse格式化代码快捷键失效

    原因是与搜狗输入法的“简繁切换”快捷键冲突(取消搜狗输入法的简繁切换快捷键,即可解决)

  10. 使用CompletionService结合ExecutorService批处理调用存储过程任务实例

    此实例为java多线程并发调用存储过程实例,只做代码记载,不做详细描述 1.线程池构造初始化类CommonExecutorService.java package com.pupeiyuan.go; ...