January 15, 2016

CMake - useful tips

What you may need in your simple project and cannot find in tutorials (especially issues about per configuration settings -
for other usages than examples here please refer to https://cmake.org/cmake/help/v3.0/policy/CMP0043.html) 
  • get absolute path
get_filename_component(ALPHAROOT "../../../../../../../../../../../../.." ABSOLUTE CACHE)
  • black font 
message(STATUS "what I wanted to say")
  • set OutputDirectory
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY D:/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY D:/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY D:/bin)
  • some cache variables are set at the time of command "project(ProjectName)", co if you can cache these variables but set default value for them
set them before command "project(ProjectName)"
  • decide according to platform you are generating for
if( WIN32 )
if( MSVC )
if( CMAKE_COMPILER_IS_GNUCXX )
if( UNIX )
  • set custon set of configurations 
#(write before "project(ProjectName)" command)
set(CMAKE_CONFIGURATION_TYPES Debug Release ClientConfig DebugFileCamera ReleaseFileCamera  CACHE TYPE INTERNAL FORCE)
  • specify configurations which should be taken as debug (i.e. which should link with debug libs)
set_property(GLOBAL PROPERTY 
DEBUG_CONFIGURATIONS 
"Debug;DebugFileCamera")
  • compiler flags per configuration (i.e. configration with name ClientConfig)
set(CMAKE_CXX_FLAGS_CLIENTCONFIG "${CMAKE_CXX_FLAGS_RELEASE} /W1")
  • linker flags per configuration (i.e. configration with name ClientConfig) & how to avoid error with /SUBSYSTEM:WINDOWS
set(CMAKE_EXE_LINKER_FLAGS_CLIENTCONFIG "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup"
)
  • postfix per configuration
set(CMAKE_CLIENTCONFIG_POSTFIX "_ClientConfig" CACHE STRING "x")
#but it does not work for executables
#you must do it per target
set_target_properties( MySubprojct
    PROPERTIES
    DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}
    RELEASE_POSTFIX ${CMAKE_RELEASE_POSTFIX}
    CLIENTCONFIG_POSTFIX ${CMAKE_CLIENTCONFIG_POSTFIX}
    DEBUGFILECAMERA_POSTFIX ${CMAKE_DEBUGFILECAMERA_POSTFIX}
    RELEASEFILECAMERA_POSTFIX ${CMAKE_ RELEASEFILECAMERA_POSTFIX}
)
  • set compile definition

set_property(DIRECTORY APPEND PROPERTY
 COMPILE_DEFINITIONS _WIN32_WINNT=0x0501
)
  • set compile definition per configuration
set_property(DIRECTORY APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:ClientConfig>:CLIENT_CONFIG>
  )
  • embed OpenCV
#use first line if you want to use dynamically linked OpenCV
set(OpenCV_STATIC OFF)
FIND_PACKAGE(OpenCV REQUIRED)

#OpenCV does not distinguish between debug and release configurations
# as we have labeled it, we must set it separately(i.e.)
set_target_properties(${OpenCV_LIBS} PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
  • embed Boost
FIND_PACKAGE(Boost REQUIRED COMPONENTS 
  system
  filesystem
  thread
  date_time
  iostreams
  serialization
  chrono
  atomic
  regex
  log
)
  • embed VTK
FIND_PACKAGE(VTK REQUIRED)
include(${VTK_USE_FILE})
# again VTK does not distinguish between debug and release configurations 
#as we have labeled it, we must set it separately(i.e.)
set_target_properties(${VTK_LIBRARIES} PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
#another problem is with VTK, after use copy paste this
macro(REMOVE_VTK_DEFINITIONS)
   get_directory_property(_dir_defs DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
   set(_vtk_definitions)
   foreach(_item ${_dir_defs})
       if(_item MATCHES "vtk*")
           list(APPEND _vtk_definitions -D${_item})
       endif()
   endforeach()
   remove_definitions(${_vtk_definitions})
endmacro(REMOVE_VTK_DEFINITIONS)

REMOVE_VTK_DEFINITIONS()
  • embed Qt
find_package(Qt5Gui)
find_package(Qt5Core)
find_package(Qt5Widgets)

if (${Qt5Gui_FOUND} AND ${Qt5Core_FOUND} AND ${Qt5Widgets_FOUND})
message(STATUS "Qt5 found")
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#Qt also does not distinguish between debug and release configurations 
#as we have labeled it, we must set it separately(i.e.)
set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_RELEASEFILECAMERA "RELEASE")
set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_CLIENTCONFIG "RELEASE")

set_target_properties(Qt5::Widgets PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
set_target_properties(Qt5::Widgets PROPERTIES MAP_IMPORTED_CONFIG_RELEASEFILECAMERA "RELEASE")
set_target_properties(Qt5::Widgets PROPERTIES MAP_IMPORTED_CONFIG_CLIENTCONFIG "RELEASE")

set_target_properties(Qt5::Gui PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
set_target_properties(Qt5::Gui PROPERTIES MAP_IMPORTED_CONFIG_RELEASEFILECAMERA "RELEASE")
set_target_properties(Qt5::Gui PROPERTIES MAP_IMPORTED_CONFIG_CLIENTCONFIG "RELEASE")

set_target_properties(Qt5::WinMain PROPERTIES MAP_IMPORTED_CONFIG_DEBUGFILECAMERA "DEBUG")
set_target_properties(Qt5::WinMain PROPERTIES MAP_IMPORTED_CONFIG_RELEASEFILECAMERA "RELEASE")
set_target_properties(Qt5::WinMain PROPERTIES MAP_IMPORTED_CONFIG_CLIENTCONFIG "RELEASE")
else()
message("Qt5 not found")
endif()


No comments:

Post a Comment