CMAKE COMPILE_DEFINITION不起作用

时间:2014-05-31 18:08:37

标签: cmake

我正在尝试使用CMAKE而我有COMPILE_DEFINITION的问题,我想要定制一个定义GIABUILD标志的自定义调试目标。 CMakeLists.txt如下

# 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)

问题是SET_TARGET_PROPERTIES在这里不起作用。 add_definitions工作,但它定义了所有目标的符号,这不是我想要的。 谢谢。

2 个答案:

答案 0 :(得分:1)

  

问题是SET_TARGET_PROPERTIES在这里不起作用

因为目标是自定义的(由add_custom_target命令创建),所以它不再是CMake 检查/使用目标财产的责任。

您需要向非自定义目标添加定义(我猜是elastic):

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

答案 1 :(得分:0)

一些应该有效的解决方案。我最初的想法是将定义直接添加到您的CXX_FLAGS:

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

您可以尝试将旗帜直接添加到目标:

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"
)