ferraith
ferraith

Reputation: 929

Escaping quotes for COMPILE_DEFINITIONS

Currently I'm working on a cmake script to compile generated C code. I'm trying to generate a Visual Studio 10 project. The code contains several #include statements which have to be preprocessed before compilition:

#indude INC_FILE

Before starting with CMake the code was compiled with a manually maintained Visual Studio project. I added a preprocessor definition to replace INC_FILE with the final include header:

INC_FILE="foo.h"

For my new CMak script I tried to add this statement to the COMPILE_DEFINITION variable:

set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY COMPILE_DEFINITION "INC_FILE="foo.h"")

Sadly this doesn't work because the quotes are removed. In the generated visual studio project I found the in the project file:

INC_FILE=foo.h

I tried to escape the quotes:

All the above mentions possibilites didn't work. Are there any other tricks in cmake to get a correct quotes escaping for my preprocessor definition?

Upvotes: 1

Views: 1321

Answers (1)

user2288008
user2288008

Reputation:

Tested with cmake 2.8.12.1:

add_executable(foo foo.cpp)
target_compile_definitions(foo PUBLIC FOO_INCL="foo.hpp")

Note 1

You can use include option instead of file's name macro:

#if defined(FOO_INCLUDE_SOME_FILE)
# include "some_file.hpp"
#elif defined(FOO_INCLUDE_OTHER_FILE)
# include "other_file.hpp"
#endif

IMHO it looks much cleaner.

Note 2

If you have a bad time with special characters use configure_file command:

> cat some_file.hpp.in
#include "@FOO_INCLUDE_FILE@"
> cat CMakeLists.txt
set(FOO_INCLUDE_FILE "other_file.hpp")
configure_file(some_file.hpp.in ${foo_BINARY_DIR}/include/some_file.hpp @ONLY)

Upvotes: 2

Related Questions