参考: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. 安装php-zbarcode的步骤方法

    1 安装ImageMagick依赖 yum install ImageMagick ImageMagick-devel 2 安装zbar拓展 wget -c http://jaist.dl.sourc ...

  2. TD - 单选框 - RadioButton

    基本方法 Html - 默认选中 //checked="true" - 默认选中 <input dojoType="bootstrap.form.RadioButt ...

  3. socket 简单实现HTTP服务器

    # -*- coding: utf-8 -*- # @Time : 2019-07-17 1:39 # @File : 网络socket实现http服务器.py # @Software: PyChar ...

  4. 在多租户(容器)数据库中如何创建PDB:方法1 从种子创建PDB

    基于版本:19c (12.2.0.3) AskScuti 创建方法:从零开始创建一个PDB(从PDB$SEED创建新的PDB) 对应路径:Creating a PDB --> Creating ...

  5. POJ 1330(LCA/倍增法模板)

    链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #inc ...

  6. windows10 +ubuntu双系统

    1,安装之前的准备: 制作启动盘 确定给ubuntu多少分区并且清理为free状态 确定电脑的开机引导方式,传统方式引号和uefi引导并不一样,因此我们需要根据引导方式选择新系统制作什么样的启动盘 在 ...

  7. laravel-middleware中间件常用使用方法

    介绍 中间件提供了一种方便的机制过滤进入应用程序的 HTTP 请求.例如,Laravel 包含一个中间件,验证您的应用程序的用户身份验证.如果用户未被认证,中间件会将用户重定向到登录界面.然而,如果用 ...

  8. 【算法学习记录-排序题】【PAT A1062】Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  9. layui之表单验证

    这篇文章的表单验证我只是随便记录下,望各位看官理解 1.排序 验证 html代码 <div class="layui-form-item"> <label cla ...

  10. 关于Javaweb的比较好用的jar包概述

    (连接数据库之前首先要导入这个数据库的驱动jar包 例如mysql 为mysql-connector-java-5.1.46.jar) 关于连接数据库的数据库连接池c3p0  jar包: c3p0-0 ...