博客专栏

EEPW首页 > 博客 > CMAKE(3)—— aux_source_directory包含目录下所有文件以及自动构建系统

CMAKE(3)—— aux_source_directory包含目录下所有文件以及自动构建系统

发布人:电子禅石 时间:2022-04-22 来源:工程师 发布文章
CMAKE(3)—— aux_source_directory包含目录下所有文件以及自动构建系统



aux_source_directory 查找在某个路径下的所有源文件。


aux_source_directory(< dir > < variable >)

1

搜集所有在指定路径下的源文件的文件名,将输出结果列表储存在指定的变量中。该命令主要用在那些使用显式模板实例化的工程上。模板实例化文件可以存储在Templates子目录下,然后可以使用这条命令自动收集起来;这样可以避免手工罗列所有的实例。


使用该命令来避免为一个库或可执行目标写源文件的清单,是非常具有吸引力的。


但是如果该命令貌似可以发挥作用,那么CMake就不需要生成一个感知新的源文件何时被加进来的构建系统了(也就是说,新文件的加入,并不会导致CMakeLists.txt过时,从而不能引起CMake重新运行)。


正常情况下,生成的构建系统能够感知它何时需要重新运行CMake,因为需要修改CMakeLists.txt来引入一个新的源文件。当源文件仅仅是加到了该路径下,但是没有修改这个CMakeLists.txt文件,使用者只能手动重新运行CMake来产生一个包含这个新文件的构建系统。


FILE (GLOB ALL_SOURCES "*.cpp" "*.c" "./AFolder/*.cpp" )

FILE (GLOB ALL_INCLUDES "*.hpp" "*.h" "./AFolder/*.hpp"  "./AFolder/*.h" )



SET (ALL_SRCS 

${ALL_SOURCES}

${ALL_INCLUDES}

)

1

2

3

4

5

6

7

8

自动构建系统例子

https://blog.csdn.net/libaineu2004/article/details/78995740


./Demo4

    |

    +--- main.cc

    |  

    +--- config.h.in

    |

    +--- math/

          |

          +--- MathFunctions.cc

          |

          +--- MathFunctions.h

1

2

3

4

5

6

7

8

9

10

11

config.h.in


#cmakedefine USE_MYMATH

1

这样 CMake 会自动根据 CMakeLists 配置文件中的设置自动生成 config.h 文件。


#CMake 最低版本号要求

cmake_minimum_required (VERSION 2.8)

#项目信息

project (Demo4)

#加入一个配置头文件,用于处理 CMake 对源码的设置

configure_file (

  "${PROJECT_SOURCE_DIR}/config.h.in"

  "${PROJECT_BINARY_DIR}/config.h"

  )

#是否使用自己的 MathFunctions 库,和.h中#define的头文件不一样

option (USE_MYMATH 

       "Use provided math implementation" ON)

#是否加入 MathFunctions 库

if (USE_MYMATH)

  include_directories ("${PROJECT_SOURCE_DIR}/math")

  add_subdirectory (math)  

  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)

endif (USE_MYMATH)

#查找当前目录下的所有源文件将名称保存到 DIR_SRCS 变量

aux_source_directory(. DIR_SRCS)

#指定生成目标

add_executable(Demo ${DIR_SRCS})

target_link_libraries (Demo  ${EXTRA_LIBS})

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

自动生成的config.h为


#define USE_MYMATH

1

#include 

#include 

#include "config.h"

#ifdef USE_MYMATH

  #include "math/MathFunctions.h"

#else

  #include 

#endif

int main(int argc, char *argv[])

{

    if (argc < 3){

        printf("Usage: %s base exponent \n", argv[0]);

        return 1;

    }

    double base = atof(argv[1]);

    int exponent = atoi(argv[2]);

    

#ifdef USE_MYMATH

    printf("Now we use our own Math library. \n");

    double result = power(base, exponent);

#else

    printf("Now we use the standard library. \n");

    double result = pow(base, exponent);

#endif

    printf("%g ^ %d is %g\n", base, exponent, result);

    return 0;

}

————————————————


原文链接:https://blog.csdn.net/u012564117/article/details/95085360


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。



关键词: cmake

技术专区

关闭