Ashkan
Ashkan

Reputation: 1085

CMAKE COMPILE_DEFINITION not working

I am trying CMAKE and I have a problem with COMPILE_DEFINITION, I want to have custom debug target which defines a GIABUILD flag. The CMakeLists.txt is the following

# top level CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
FIND_PACKAGE(deal.II 8.0 REQUIRED
  HINTS
  ${DEAL_II_DIR} $ENV{DEAL_II_DIR} )

DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT(GIA)

SET(CLEAN_UP_FILES
 *gmv *gnuplot *gpl *eps *pov *vtk *vtu *ucd *.d2 *dat *.log *.m *.1
)

INCLUDE_DIRECTORIES(include)

FILE(GLOB headers ./include/*.h)
FILE(GLOB sources ./source/*.cc)
ADD_EXECUTABLE(elastic ${sources} ${headers})

ADD_CUSTOM_TARGET(clear
 COMMAND rm ${CLEAN_UP_FILES}
 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

ADD_CUSTOM_TARGET(debug
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
)
#add_definitions(-DGIABUILD)
SET_TARGET_PROPERTIES(debug PROPERTIES COMPILE_DEFINITIONS "GIABUILD")

ADD_CUSTOM_TARGET(release
 COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
 COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
 COMMENT "Switch CMAKE_BUILD_TYPE to Release"
)

set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /opt/local/lib)
TARGET_LINK_LIBRARIES(elastic /opt/local/lib/libboost_program_options-mt.a)
DEAL_II_SETUP_TARGET(elastic)

The problem is that SET_TARGET_PROPERTIES doesn't work here. add_definitions work but it defines the symbol for all targets which is not what I want. Thank you.

Upvotes: 2

Views: 6061

Answers (2)

user2288008
user2288008

Reputation:

The problem is that SET_TARGET_PROPERTIES doesn't work here

Because target is custom (created by add_custom_target command) it's no more CMake responsibility to check/use target properties.

You need to add definitions to non custom target (elastic I guess):

target_compile_definitions(
    elastic PUBLIC "$<$<CONFIG:Debug>:GIABUILD>"
)

Upvotes: 1

Josh
Josh

Reputation: 121

A couple of solutions that should work. My initial thought is to add the definition directly to your CXX_FLAGS:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGIABUILD")

You can try adding the flag directly to your target:

ADD_CUSTOM_TARGET(debug
  COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR} -DCMAKE_CXX_FLAGS_DEBUG="-DGIABUILD"
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
  COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
)

Upvotes: 0

Related Questions