CMAKE
CMAKE_BUILD_TYPE:STRING
CMAKE_BUILD_TYPE:STRING=[ Debug, Release, MinSizeRel, RelWidthDebInfo ]
Tips and HowTos
CMAKE with deal.ii
recommended structure e.g.:
- CMakeLists.txt
- build
- include
- h1.h
- h2.h
- …
- src
- CMakeLists.txt
- h1.cpp
- h2.cpp
- …
CMakeList.txt in top folder
#
# It is good practice to specify a version requirement:
#
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
set(TARGET target_name)
#
# Find and import the deal.II project configuration:
#
FIND_PACKAGE(deal.II 8.0 QUIET
HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
)
IF(NOT ${deal.II_FOUND})
MESSAGE(FATAL_ERROR "\n"
"*** Could not locate deal.II. ***\n\n"
"You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to cmake\n"
"or set an environment variable \"DEAL_II_DIR\" that contains this path."
)
ENDIF()
# Initialize cached variables: This will set the compiler and
# compiler flags to the values deal.II was configured with, as well as,
# CMAKE_BUILD_TYPE to "Debug".
# These values can be altered by editing the cache via
# $ make edit_cache
DEAL_II_INITIALIZE_CACHED_VARIABLES()
project(${TARGET})
# sources for own files; must be defined before calling the executable
include_directories(${PROJECT_SOURCE_DIR}/include)
## Deal.II
include_directories(${DEAL_II_INCLUDE_DIRS})
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
## executable and set the folder for the executable
add_executable(${TARGET} name_of_executable)
set_target_properties(${TARGET} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
### link against needed libraries, in this case just the own files + dealii files
target_link_libraries(${TARGET} src_lib)
target_link_libraries(${TARGET} ${DEAL_II_LIBRARIES})
## optional custom properties and folder
set_property(TARGET ${TARGET} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-unused-variable -Wno-deprecated-declarations")
ADD_CUSTOM_TARGET(run
COMMAND make all
COMMAND ${TARGET}
COMMENT "Run ${TARGET} with ${CMAKE_BUILD_TYPE} configuration"
)
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_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"
)
CMakeList.txt in src folder
SET(SRCS
${SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/h1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/h2.cpp
....
)
## build a library out of the files
add_library(src_lib ${SRCS})