cmake 单个目录多个文件的情况

Posted 巨鹿王十二

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmake 单个目录多个文件的情况相关的知识,希望对你有一定的参考价值。

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

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

文件内容分别如下:

main.cpp

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "MathFunctions.h"
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     if (argc < 3){
 8         printf("Usage: %s base exponent \n", argv[0]);
 9         return 1;
10     }
11     double base = atof(argv[1]);
12     int exponent = atoi(argv[2]);
13     double result = power(base, exponent);
14     printf("%g ^ %d is %g\n", base, exponent, result);
15     return 0;
16 }

MathFunctions.h

1 #ifndef POWER_H
2 #define POWER_H
3 
4 extern double power(double base, int exponent);
5 
6 #endif

MathFunctions.cpp

 1  /**
 2  * power - Calculate the power of number.
 3  * @param base: Base value.
 4  * @param exponent: Exponent value.
 5  *
 6  * @return base raised to the power exponent.
 7  */
 8 double power(double base, int exponent)
 9 {
10     int result = base;
11     int i;
12 
13     if (exponent == 0) {
14         return 1;
15     }
16     
17     for(i = 1; i < exponent; ++i){
18         result = result * base;
19     }
20 
21     return result;
22 }

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.0
-- The CXX compiler identification is GNU 5.4.0
-- 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
[ 33%] Building CXX object CMakeFiles/Demo.dir/MatnFunctions.cpp.o
[ 66%] Building CXX object CMakeFiles/Demo.dir/main.cpp.o
[100%] Linking CXX executable Demo
[100%] 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 2 3
2 ^ 3 is 8

 

以上是关于cmake 单个目录多个文件的情况的主要内容,如果未能解决你的问题,请参考以下文章

CMake 快速入门

CMake 快速入门

在 Android 中使用具有多个布局的单个片段

Mac 下 CMake 的配置与使用

cmake超详细入门教程,学不会你捶我~

如何使用多个目录设置 CMAKE_INSTALL_RPATH?