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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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")