参考: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. CentOS7识别不到win10启动项的解决方法

    Windows的文件系统是NTFS格式的,而CentOS是不支持NTFS格式的.因此,我们要安装另外的工具使CentOS能识别NTFS格式的文件系统. 这里我们选择ntfs-3g这个工具,安装过程如下 ...

  2. 关于jquery改变onclick方法,最保险的做法

    function a(){ alert("a"); } function b(){ alert("b"); } <input type="but ...

  3. 用阿里fastJson解析Json字符串

    一下总结来自工作代码: 1.第一种情况: 通过服务器端发送http请求获取的接送字符串. String jsonStr = HttpRequestUtil.sendGet(config.getAddr ...

  4. 题解 【Codeforces381A】 Sereja and Dima

    本题是很好的双指针练习题. 关于双指针,详见洛谷日报#73. 我们可以用两个指针l和r表示题中两人接下来要比较的数字,用fl标记下一个将要取的人,并分别用两个计数器统计双方的答案. 因此,我们有了如下 ...

  5. goahead 流程

    原文:https://blog.csdn.net/qq_32419007/article/details/80756643 1.全局变量 Web服务器的根目录 static char_t        ...

  6. C语言 strlen

    C语言 strlen #include <string.h> size_t strlen(const char *s); 功能:计算指定指定字符串s的长度,不包含字符串结束符‘\0’ 参数 ...

  7. Linux中通配符

    通配符是由shell处理的, 它只会出现在 命令的“参数”里.当shell在“参数”中遇到了通配符时,shell会将其当作路径或文件名去在磁盘上搜寻可能的匹配:若符合要求的匹配存在,则进行代换(路径扩 ...

  8. flex space-between最后一行对齐问题的解决方案

    背景 常见的一个图文列表设计,通常是这样的,两端顶着容器,中间的间距平均分,如下图的某东商品列表的设计: 列表是这样自适应的,当视窗足够放多少个商品就放多少个,然后各个商品项目之间的间距平均分.由于每 ...

  9. The Reason Why Cosmetic Airless Bottles Are Widely Used

    The contents of the Cosmetic Airless Bottles    can be isolated from the air, to prevent the product ...

  10. 计算几何-LA2218-HPI-第一次卡精度-vijos1087-铁人三项

    This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...