1

I have project where I need some specific preprocessor definitions. Nothing less nothing more. I tried various solutions:

set(DEFINES MY_DEFINE)
target_compile_definitions(my_target PRIVATE ${DEFINES})

and

set(DEFINES -DMY_DEFINE)
add_definitions(${DEFINES})

even

set(DEFINES MY_DEFINE)
set_property(TARGET my_target PROPERTY CACHE COMPILE_DEFINITIONS ${DEFINES})

Every time cmake still injects some other defines:

WIN32
_WINDOWS
CMAKE_INTDIR="$(CONFIG)"

My target is static library and project generator is Visual Studio 2015 if it might influence anything. How can I get cmake to set only my defines?

ostojan
  • 608
  • 1
  • 7
  • 17

2 Answers2

3

Since those are platform specific defines in CMake, you can only remove them "globally" for your current CMakeList.txt scope (not for an idividual taret) with something like:

string(REGEX REPLACE "(-D|/D)[^ ]* " "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
0

In CMake, you can use remove_definitions for any definitions added via add_definitions. I'm not sure if that will work here, though, because cmake may not be adding them via add_definitions.

You could also potentially #undef those definitions at the very top of your C++ files. This is a little bit dirtier, but it might work.

  • I thought about remove_definitions, but in that case I need to enter defines to remove directly inside my cmake file and I'm looking for cleaner (and more important - more generic) solution. – ostojan Nov 30 '17 at 18:47