How to create a C #define for a certain target using CMake?

I feel a little stupid right now. After recently converting a few smaller projects to use CMake, I decided to also get rid of a few "Platform_Config.h" files. These files contain a few preprocessing directives like #define USE_NEW_CACHE and control compilation.

How would I 'convert' these defines to be controlled with CMake? Ideally by using these "cache" variables the user can easily edit.


There are two options. You can use the add_definitions method to pass defines as compiler flags: E.g. somewhere in your projects cmakelists.txt:

add_definitions( -DUSE_NEW_CACHE )

CMake will make sure the -D prefix is converted to the right flag for your compiler (/D for msvc and -D for gcc).

Alternatively, check out configure_file. It is more complex, but may be better suited to your original approach with a Platform_Config file.

You can create an input-file, similar to your original Platform_Config.h and add "#cmakedefine" lines to it.

Let's call in

// In
#cmakedefine USE_NEW_CACHE
// end of

When then running

configure_file( ${CMAKE_SOURCE_DIR}/ ${CMAKE_BINARY_DIR}/common/Platform_Config.h )

it will generate a new Platform_Config file in your build-dir. Those variables in cmake which are also a cmakedefine will be present in the generated file, the other ones will be commented out or undefed.

Of course, you should make sure the actual, generated file is then correctly found when including it in your source files.

option command might provide what you are looking for. use it with the COMPILE DEFINITIONS property on the target and i think you are done. To set the property on the target, use the command set target properties

option(DEBUGPRINTS "Prints a lot of debug prints")
target(myProgram ...)


The option i wrote in the example shows up as a checkbox in the CMake GUI.

In case you want to set defines per target: Since 2.8.11 you can use target_compile_definitions.

In earlier versions you probably don't want to use set_target_properties as is, since it overwrites any defines you set previously. Call get_target_property first instead, then merge with previous values. See add_target_definitions here.

Need Your Help

how to setCustomAnimations for FragmentTransaction on this code

android fragmenttransaction

i have 5 fragments and I use the following code to setCustomAnimations for FragmentTransaction:

ORM performance cost

sql database performance orm frameworks

Does anyone have any experience that indicates what kind of performance hit a developer could expect by choosing to use an ORM (in Django, RoR, SQLAlechemy, etc) over SQL and hand-designed database...