Can I force cmake to include my header file in specific targets?

a 夏天 提交于 2020-01-06 05:08:24

问题


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


来源:https://stackoverflow.com/questions/48322670/can-i-force-cmake-to-include-my-header-file-in-specific-targets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!