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

源文件一共有三个:main.cpp、MathFunctions.h、MathFunctions.cpp

文件内容分别如下:

main.cpp

 #include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.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 ;
}

MathFunctions.h

 #ifndef POWER_H
#define POWER_H extern double power(double base, int exponent); #endif

MathFunctions.cpp

  /**
* 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 == ) {
return ;
} for(i = ; i < exponent; ++i){
result = result * base;
} return result;
}

CMakeLists.txt内容如下:

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8) # 项目信息
project (Demo2) # 查找目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS) # 指定生成目标
add_executable(Demo ${DIR_SRCS})

这里需要学习的是使用变量获取目录下的所有源文件:

aux_source_directory(. DIR_SRCS)

# 指定生成目标
add_executable(Demo ${DIR_SRCS})

编译及执行结果如下:

wmz@ubuntu:~/Desktop/testcmake1$ cmake .
-- The C compiler identification is GNU 5.4.
-- The CXX compiler identification is GNU 5.4.
-- 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/wmz/Desktop/testcmake1
wmz@ubuntu:~/Desktop/testcmake1$ make
Scanning dependencies of target Demo
[ %] Building CXX object CMakeFiles/Demo.dir/MatnFunctions.cpp.o
[ %] Building CXX object CMakeFiles/Demo.dir/main.cpp.o
[%] Linking CXX executable Demo
[%] Built target Demo
wmz@ubuntu:~/Desktop/testcmake1$ ls
CMakeCache.txt CMakeLists.txt Makefile
CMakeFiles Demo MathFunctions.h
cmake_install.cmake main.cpp MatnFunctions.cpp
wmz@ubuntu:~/Desktop/testcmake1$ ./Demo
^ is

cmake 单个目录多个文件的情况的更多相关文章

  1. 51-du 显示关于目录层次结构或文件磁盘使用情况的信息

    显示关于目录层次结构或文件磁盘使用情况的信息 du [options] [path-list] 参数 不带任何参数的du将显示工作目录及其子目录磁盘使用情况的信息,path-list指定要获取磁盘占用 ...

  2. git clone 指定的单个目录或文件夹

    git clone 指定的单个目录或文件夹 针对自己的项目 方法一 基于sparse clone变通方法 创建一个空仓库 拉取远程仓库信息 开启 sparse clone 设置过滤 更新仓库 创建空仓 ...

  3. cmake指定程序输出目录和库文件输出目录和拷贝文件

    概述 本文样式环境: win10+cmake 3.18 本文将介绍使用CMAKE配置项目输出目录和 LIbrary项目的输出目录 本文将介绍 cmake的file函数的基础用法之拷贝文件 重点, 这些 ...

  4. 利用WatchService监控C盘根目录下的文件情况

    public static void main(String[] args) throws IOException, InterruptedException { WatchService watch ...

  5. 【Maven jar】打包单个或多个文件,有依赖jar包的将架包一起打包成一个jar包供别的项目引用

    之前有一片文章,是打包单个java文件的.这次想要将http://www.cnblogs.com/sxdcgaq8080/p/8398780.html  打包成jar包,发现这个java文件中引用了多 ...

  6. linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good

    linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...

  7. [转帖]linux /proc目录下的文件为何无法用vi编辑保存

    linux /proc目录下的文件为何无法用vi编辑保存 https://blog.51cto.com/xlogin/1216914 学习一下 之前看过书 这一点 没太仔细看.. xlogin关注8人 ...

  8. VS Build目录下各文件的作用

    VS2010中各种类型文件的作用: .sln 相当于VC6中 .dsw    .vcxproj 相当于VC6中 .dsp    .suo 相当于VC6中 .ncb    .vcxproj.filter ...

  9. Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)

    文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1.  IOException parsing XML docum ...

随机推荐

  1. LaTeX竖立公式符号

    1.LaTeX技巧668:LaTeX如何实现直立积分号 \usepackage{amsmath,amssymb} \DeclareSymbolFont{EulerExtension}{U}{euex} ...

  2. pytest学习5-mark用例分类

    使用Mark标记测试用例 通过使用pytest.mark你可以轻松地在测试用例上设置元数据.例如, 一些常用的内置标记: skip - 始终跳过该测试用例 skipif - 遇到特定情况跳过该测试用例 ...

  3. 【metasploit教程】之建立数据库

    在我们通过search命令搜索时,我们会发现搜索的很慢(慢搜索)而且会报错: 启动postgresql: service postgresql strart 启动metasploit会开始建立数据表: ...

  4. 【转载】各种SQL在PIG中实现

    转自:http://guoyunsky.iteye.com/blog/1317084 我这里以Mysql 5.1.x为例,Pig的版本是0.8 同时我将数据放在了两个文件,存放在/tmp/data_f ...

  5. Codeforces Round #570 (Div. 3) B. Equalize Prices

    原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...

  6. 第三十八篇 入门机器学习——Numpy.array的基本操作——查看向量或矩阵

    No.1. 初始化状态 No.2. 通过ndim来查看数组维数,向量是一维数组,矩阵是二维数组 No.3. 通过shape来查看向量中元素的个数或矩阵中的行列数 No.4. 通过size来查看数组中的 ...

  7. AcWing 913. 排队打水

    #include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; in ...

  8. MySQL学习(八)删除表数据

    表空洞的产生 删除某个行数据 或删除某个页     如下图所示,这个删除过程只是标记了某行的位置为删除,假如此时在300与600之间插入了一行数据,那么 同理,当删除某个页时,该页就会被复用.所以当删 ...

  9. LED Decorative Light Manufacturer Introduction: LED Metal Table Light

    Nowadays, when many people choose the desk light, they are worried that it will not be used for a lo ...

  10. 每天进步一点点------Allegro手工建立电路板

    手工建立电路板步骤(以某个四层板为例): (1)file---new---board/board wizard来建立.brd电路板文件.设置名称和保存路径后,要设置绘图区域的大小,setup---dr ...