Can I force cmake to include my header file in spe

2019-08-18 01:12发布

问题:

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")