Chandu
Chandu

Reputation: 57

CMake is dropping a preprocessor definition

cmake --build _build output

WARNING: Preprocessor definitions containing '#' may not be passed on the compiler command line because many compilers do not support it. CMake is dropping a preprocessor definition: BUILD_HOST="Linux cvuppala-bri-vm 2.6.32-573.7.1.el6.x86_64 #1 SMP Tue Sep 22 22:00:00 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux" Consider defining the macro in a (configured) header file.

Get host

corresponding code cmake code snippet

execute_process(
  COMMAND uname -a
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE BUILD_HOST
  OUTPUT_STRIP_TRAILING_WHITESPACE
  )

add_definitions(-DBUILD_HOST="${BUILD_HOST}")

Upvotes: 1

Views: 2229

Answers (2)

Richard Hodges
Richard Hodges

Reputation: 69854

The answer is to use a response file to set extra command line options for the compiler:

step 1:

create the response file template, in this case called extra_options.rsp.in

-DBUILD_HOST="@RESULT_OF_UNAME@"

step 2:

configure this file in the cmakelists.txt file:

execute_process(COMMAND "uname" "-a" 
                OUTPUT_VARIABLE RESULT_OF_UNAME
                OUTPUT_STRIP_TRAILING_WHITESPACE)
configure_file(extra_options.rsp.in
               ${CMAKE_CURRENT_BINARY_DIR}/extra_options.rsp 
               @ONLY)

step 3:

add a reference to the configured response file to the compiled options: (in this case, my target is called algo)

set_property(TARGET algo APPEND PROPERTY COMPILE_OPTIONS
             "@${CMAKE_CURRENT_BINARY_DIR}/extra_options.rsp")

Example output on my system:

extra_options.rsp:

-DBUILD_HOST="Darwin codeblaster.local 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64"

result of VERBOSE=1 make:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-isystem /Users/rhodges/.hunter/_Base/3973bc2/7c86f54/a5fd140/Install/include -g @/Users/rhodges/algo/cmake-build-debug/extra_options.rsp -std=gnu++14 -o CMakeFiles/algo.dir/main.cpp.o -c /Users/rhodges/algo/main.cpp

Upvotes: 0

suhdonghwi
suhdonghwi

Reputation: 1035

The problem is the output of command uname -a contains '#'. Even though the string is quoted, I wonder why cmake forbids it.

However, to solve this problem, you should remove '#' or replace '#' to another character from BUILD_HOST before adding definition.

string(REGEX REPLACE "\#"
       "" BUILD_HOST
       ${BUILD_HOST})

This would remove '#' from your BUILD_HOST string.

If you want to perform replacing rather than removing, try something like following:

string(REGEX REPLACE "\#"
           "$" BUILD_HOST
           ${BUILD_HOST})

This would replace '#' to '$'.


EDIT:

I think it is impossible to add definition which contains '#' via add_definitions. But, if you have to pass result as-is, you can add compile options manually.

set(EXTRA_COMPILE_OPTIONS -DBUILD_HOST="${BUILD_HOST}")
target_compile_options(${PROJECT_NAME} PRIVATE ${EXTRA_COMPILE_OPTIONS})

Upvotes: 4

Related Questions