工作环境

  • 系统:macOS Mojave 10.14.6
  • CMake: Version 3.15.0-rc4

Hello,World! 扩展-同一目录,多个源文件

(1) 新建 hello 目录,创建文件 CMakeLists.txt、main.cpp、MathFunctions.h、MathFunctions.cpp

$ mkdir hello
$ cd hello
$ touch CMakeLists.txt main.cpp MathFunctions.h MathFunctions.cpp
$ ll
-rw-r--r-- 1 staff staff 124B 8 14 17:19 CMakeLists.txt
-rw-r--r-- 1 staff staff 0B 8 15 16:22 MathFunctions.cpp
-rw-r--r-- 1 staff staff 0B 8 15 16:22 MathFunctions.h
-rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp

(2) 编写 MathFunctions.h

int power(int base, int exponent);

(3) 编写 MathFunctions.cpp

#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h" int power(int base, int exponent) {
int result = base;
int i; if (exponent == 0) {
return 1;
} for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}

(4) 编写 main.cpp

#include <iostream>
#include "MathFunctions.h" using namespace std; int main(int argc, char const *argv[]) {
/* code */
// cout << "Hello,World!" << power(2, 3) << endl;
printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
return 0;
}

(5) 编写 CMakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0) # 项目名
project(hello) # 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
# set(SRC_LIST main.cpp MathFunctions.h MathFunctions.cpp)
aux_source_directory(. SRC_LIST) # 指定生成目标
add_executable(hello ${SRC_LIST})

(6) 编译运行

$ mkdir build
$ cd build
$ pwd
/Users/staff/Desktop/hello/build $ cmake ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- 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/staff/Desktop/hello/build $ make
Scanning dependencies of target hello
[ 33%] Building CXX object CMakeFiles/hello.dir/MathFunctions.cpp.o
[ 66%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello $ ./hello
Hello,World! power(2,3)=8

Hello,World! 扩展-多个目录,多个源文件

(1) 新建 hello 目录,创建文件 CMakeLists.txt、main.cpp、math/MathFunctions.h、math/MathFunctions.cpp

$ mkdir hello
$ cd hello
$ mkdir math
$ touch CMakeLists.txt main.cpp math/MathFunctions.h math/MathFunctions.cpp
$ tree
.
├── CMakeLists.txt
├── main.cpp
└── math
├── MathFunctions.cpp
└── MathFunctions.h

(2) 编写代码

  • math/MathFunctions.h
int power(int base, int exponent);
  • math/MathFunctions.cpp
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h" int power(int base, int exponent) {
int result = base;
int i; if (exponent == 0) {
return 1;
} for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
  • main.cpp
#include <iostream>
// 这里要加上 math 目录
#include "math/MathFunctions.h" using namespace std; int main(int argc, char const *argv[]) {
/* code */
// cout << "Hello,World!" << power(2, 3) << endl;
printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
return 0;
}
  • CMakeLists.txt
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0) # 项目名
project(hello) # 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)
# 查找 math 目录下的所有源文件,并将名称保存到 MATH_SRC_LIST 变量
aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST) # 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

(3) 再多学一点

由于多了 math 目录,我们看到 main.cpp 中,#include "math/MathFunctions.h" 也要加上目录。

如果我们不想加 math 目录,直接 #include "MathFunctions.h",操作如下:

  • main.cpp
#include <iostream>
// 这里去掉 math 目录
#include "MathFunctions.h" using namespace std; int main(int argc, char const *argv[]) {
/* code */
// cout << "Hello,World!" << power(2, 3) << endl;
printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
return 0;
}
  • CMakeLists.txt 添加 include_directories
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0) # 项目名
project(hello) # 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)
# 查找 math 目录下的所有源文件,并将名称保存到 MATH_SRC_LIST 变量
aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST) # 添加头文件路径
include_directories(${PROJECT_SOURCE_DIR}/math) # 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

相关参考:

CMake 官方网站

CMake 多个源文件-多个目录

联系作者:


CMake入门-02-HelloWorld扩展的更多相关文章

  1. CMake入门-03-还是HelloWorld

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

  2. CMake入门(二)

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

  3. Netty入门之HelloWorld

    Netty系列入门之HelloWorld(一) 一. 简介 Netty is a NIO client server framework which enables quick and easy de ...

  4. CSS3基础入门02

    CSS3 基础入门02 边框相关属性 border-radius 通过这个属性我们可以设置边框圆角,即可以将四个角设置为统一的圆角,也可以单独的设置具体的某一个角的圆角. grammer: borde ...

  5. CMake入门

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

  6. CMake入门教程(转帖)

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

  7. Shell入门02

    Shell入门-02 1.重定向 标准输入(<) 标准输出 标准错误重回定向 程序 = 指令 + 数据 命令   变量 在程序中,数据如何输入?又如何输出? 数据输入:键盘 – 标准输入,但是并 ...

  8. CMake 入门实战 | HaHack

    CMake 入门实战 | HaHack undefined

  9. 【网络爬虫入门02】HTTP客户端库Requests的基本原理与基础应用

    [网络爬虫入门02]HTTP客户端库Requests的基本原理与基础应用 广东职业技术学院  欧浩源 1.引言 实现网络爬虫的第一步就是要建立网络连接并向服务器或网页等网络资源发起请求.urllib是 ...

  10. PHP扩展开发--编写一个helloWorld扩展

    为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍. 另外C扩展是在进程启动时加载的,PHP代码只能操作Request生命周期的数据 ...

随机推荐

  1. k8s学习 - 概念 - master/node

    k8s学习 - 概念 - master/node 在k8s中,有各种各样的概念和术语.这些概念是必须要学习和掌握的.我们先罗列下所有概念,然后再一个个看具体实例. 大概说一下这些概念: Master: ...

  2. java网络爬虫,乱码问题终于完美解决

    第一次写爬虫,被乱码问题困扰两天,试了很多方法都不可以,今天随便一试,居然好了. 在获取网页时创建了一个缓冲字节输入流,问题就在这个流上,添加标红代码即可 BufferedReader in = nu ...

  3. java接口自动化(一) - 接口自动化测试整体认知 - 开山篇(超详解)

    简介 了解什么是接口和为什么要做接口测试.并且知道接口自动化测试应该学习哪些技术以及接口自动化测试的落地过程.其实这些基本上在python接口自动化的文章中已经详细的介绍过了,不清楚的可以过去看看.了 ...

  4. Gin 框架 - 使用 logrus 进行日志记录

    目录 概述 日志格式 Logrus 使用 推荐阅读 概述 上篇文章分享了 Gin 框架的路由配置,这篇文章分享日志记录. 查了很多资料,Go 的日志记录用的最多的还是 github.com/sirup ...

  5. 个人永久性免费-Excel催化剂功能第36波-新增序列函数用于生成规律性的循环重复或间隔序列

    啃过Excel函数的表哥表姐们,一定对函数的嵌套.数组公式等高级的应用有很深的体会,威力是大,但也烧死不少脑细胞,不少人就在这样的绕函数中光荣地牺牲了,走向从入门到放弃.Excel催化剂的创立,初衷就 ...

  6. leetcode 198. House Robber (Easy)

    https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...

  7. springMVC保存数据到mysql数据库中文乱码问题解决方法

    1.web.xml中添加过滤器 <filter> <filter-name>CharacterEncodingFilter</filter-name> <fi ...

  8. Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two

    传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...

  9. jQuery 小测试

    1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? 提示使用has() 答案: $(div:has(s ...

  10. On The Way—Step 1 :python入门之Python的历程

    1.python的历史 2004 Django框架 python2 和 python3的区别 python2 源码不统一 有重复功能代码 python3 源码统一 没有重复功能代码 Python的发展 ...