我可以强制cmake将头文件包含在特定目标中吗? [英] Can I force cmake to include my header file in specific targets?
问题描述
我有一个头文件,该头文件用宏覆盖了malloc。使用 add_definitions(-include ../ include / failing-malloc-test.h)
,我可以强制cmake在所有目标文件中包含此头文件。问题是我只想将我的头文件包含在某些目标(测试目标等)中。我尝试使用 target_compile_definition
实现此目的,但我无法达到相同的效果,因为 target_compile_definition
似乎与 add_definitions
。目前,我唯一想到的解决方案是复制所有源文件并手动添加 #include failing-malloc-test.h
-我显然想避免这样做。
I have a header file which overrides malloc with a macro. Using add_definitions(-include ../include/failing-malloc-test.h)
I'm able to force cmake to include this header file in all targets. The problem is that I only want to have my header file included in some targets(test targets and so on). I tried achieving this using target_compile_definition
, but I couldn't achieve the same effect, because target_compile_definition
seems to work different than add_definitions
. Currently the only solution that I can think of is duplicating all source files and adding the #include "failing-malloc-test.h"
manually - what I obviously want to avoid doing.
推荐答案
CMake具有基于属性的机制。您可以分配给目标,源文件和其他部分的属性。使用目标是通常的默认操作,因此提供了一系列的 target _ *
命令来设置目标的属性。如果要为特定目标中的所有源添加编译选项,请使用 target_compile_options
。请勿使用 COMPILE_DEFINITIONS
属性,用于定义除预处理程序符号以外的其他选项。这样您就可以使用
CMake has a properties-based mechanism. You can assign to properties on targets, source files, and other parts. Working with targets is the usual and default action, so a whole family of target_*
commands are provided for setting properties on targets. If you want to add a compile option for all sources in a specific target then use target_compile_options
. Do not use the COMPILE_DEFINITIONS
property for other options than defining pre-processor symbols. So you be able to get what you want with
target_compile_options(<my-test-target> "-include ../include/failing-malloc-test.h")
这篇关于我可以强制cmake将头文件包含在特定目标中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!