博客專欄

EEPW首頁 > 博客 > CMAKE(3)—— aux_source_directory包含目錄下所有文件以及自動(dòng)構(gòu)建系統(tǒng)

CMAKE(3)—— aux_source_directory包含目錄下所有文件以及自動(dòng)構(gòu)建系統(tǒng)

發(fā)布人:電子禪石 時(shí)間:2022-04-22 來源:工程師 發(fā)布文章
CMAKE(3)—— aux_source_directory包含目錄下所有文件以及自動(dòng)構(gòu)建系統(tǒng)



aux_source_directory 查找在某個(gè)路徑下的所有源文件。


aux_source_directory(< dir > < variable >)

1

搜集所有在指定路徑下的源文件的文件名,將輸出結(jié)果列表儲存在指定的變量中。該命令主要用在那些使用顯式模板實(shí)例化的工程上。模板實(shí)例化文件可以存儲在Templates子目錄下,然后可以使用這條命令自動(dòng)收集起來;這樣可以避免手工羅列所有的實(shí)例。


使用該命令來避免為一個(gè)庫或可執(zhí)行目標(biāo)寫源文件的清單,是非常具有吸引力的。


但是如果該命令貌似可以發(fā)揮作用,那么CMake就不需要生成一個(gè)感知新的源文件何時(shí)被加進(jìn)來的構(gòu)建系統(tǒng)了(也就是說,新文件的加入,并不會(huì)導(dǎo)致CMakeLists.txt過時(shí),從而不能引起CMake重新運(yùn)行)。


正常情況下,生成的構(gòu)建系統(tǒng)能夠感知它何時(shí)需要重新運(yùn)行CMake,因?yàn)樾枰薷腃MakeLists.txt來引入一個(gè)新的源文件。當(dāng)源文件僅僅是加到了該路徑下,但是沒有修改這個(gè)CMakeLists.txt文件,使用者只能手動(dòng)重新運(yùn)行CMake來產(chǎn)生一個(gè)包含這個(gè)新文件的構(gòu)建系統(tǒng)。


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

自動(dòng)構(gòu)建系統(tǒng)例子

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 會(huì)自動(dòng)根據(jù) CMakeLists 配置文件中的設(shè)置自動(dòng)生成 config.h 文件。


#CMake 最低版本號要求

cmake_minimum_required (VERSION 2.8)

#項(xiàng)目信息

project (Demo4)

#加入一個(gè)配置頭文件,用于處理 CMake 對源碼的設(shè)置

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)

#查找當(dāng)前目錄下的所有源文件將名稱保存到 DIR_SRCS 變量

aux_source_directory(. DIR_SRCS)

#指定生成目標(biāo)

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

自動(dòng)生成的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


*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。



關(guān)鍵詞: cmake

技術(shù)專區(qū)

關(guān)閉