cmake 的add_library,add_executable,add_subdirectory等命令都有一个EXCLUDE_FROM_ALL参数.
这个参数的作用根据cmake官网的解释就是如果某个target或subdirectory被设置为EXCLUDE_FROM_ALL属性,那么这个target(或这个subdirectory中的所有target)就会被排除在all target列表之外,这样,当执行默认的make(或nmake)时,这个target(或这个subdirectory中的所有target)就不会被编译。
EXCLUDE_FROM_ALL
Exclude the target from the all target.
A property on a target that indicates if the target is excluded from the default build target. If it is not, then with a Makefile for example typing make will cause this target to be built. The same concept applies to the default build of other generators. Installing a target with EXCLUDE_FROM_ALL set to true has undefined behavior.
@cmake.org
在一个项目中不可避免会有一些测试代码,这些测试代码,我们并不一定需要每次都编译,尤其是编译正式版本的时候,这些测试代码是不会加入release版本的。为了加快编译速度,可以将这些测试用的target或不会加入release的target 加上EXCLUDE_FROM_ALL属性就不需要每次编译它了。
下面是代码示例:
add_library(cmimpl SHARED ${CMIMPL_SOURCE_FILES})
add_library(cmjnidrv SHARED ${JNI_SOURCE_FILES})
# fctest指定了EXCLUDE_FROM_ALL 属性,不会自动编译,只能手动编译
add_executable(fctest EXCLUDE_FROM_ALL FeatureCompareSpeedTest.cpp)
加了EXCLUDE_FROM_ALL属性的target在默认编译的时候,不会被编译,如果要编译它们,需要手动编译,
比如make fctest指定编译名为fctest
参考:
https://cmake.org/cmake/help/v3.1/prop_tgt/EXCLUDE_FROM_ALL.html#prop_tgt:EXCLUDE_FROM_ALL

本文详细介绍了CMake中的EXCLUDE_FROM_ALL属性的作用及使用方法。通过此属性可以将某些目标从默认构建中排除,从而提高编译速度。特别适用于不需要在正式版本中编译的测试代码。
1万+

被折叠的 条评论
为什么被折叠?



