2023年7月9日发(作者:)
CMAKE(3)——aux_source_directory包含⽬录下所有⽂件以及⾃动构建系统aux_source_directory 查找在某个路径下的所有源⽂件。aux_source_directory(< dir > < variable >)搜集所有在指定路径下的源⽂件的⽂件名,将输出结果列表储存在指定的变量中。该命令主要⽤在那些使⽤显式模板实例化的⼯程上。模板实例化⽂件可以存储在Templates⼦⽬录下,然后可以使⽤这条命令⾃动收集起来;这样可以避免⼿⼯罗列所有的实例。使⽤该命令来避免为⼀个库或可执⾏⽬标写源⽂件的清单,是⾮常具有吸引⼒的。但是如果该命令貌似可以发挥作⽤,那么CMake就不需要⽣成⼀个感知新的源⽂件何时被加进来的构建系统了(也就是说,新⽂件的加⼊,并不会导致过时,从⽽不能引起CMake重新运⾏)。正常情况下,⽣成的构建系统能够感知它何时需要重新运⾏CMake,因为需要修改来引⼊⼀个新的源⽂件。当源⽂件仅仅是加到了该路径下,但是没有修改这个⽂件,使⽤者只能⼿动重新运⾏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})⾃动构建系统例⼦./Demo4 | +--- |
+--- | +--- math/ | +--- | +--- MathFunctions.h#cmakedefine USE_MYMATH这样 CMake 会⾃动根据 CMakeLists 配置⽂件中的设置⾃动⽣成 config.h ⽂件。#CMake
最低版本号要求cmake_minimum_required (VERSION 2.8)#项⽬信息project (Demo4)#加⼊⼀个配置头⽂件,⽤于处理 CMake
对源码的设置configure_file ( "${PROJECT_SOURCE_DIR}/" "${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})⾃动⽣成的config.h为#define USE_MYMATH#include
#include
#include "config.h"#ifdef USE_MYMATH #include "math/MathFunctions.h"#else #include
#endifint 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 %gn", base, exponent, result); return 0;}
发布者:admin,转转请注明出处:http://www.yc00.com/news/1688906870a182261.html
评论列表(0条)