CMakeLists 模板

CMakeLists 中根据编译器gcc或者clang自适应设置sanitizer,根据cmake参数启用ASAN或者TSAN。

项目目录结构

project_root
├── CMakeLists.txt
├── include
│   ├── problem.hpp
│   ├── ceres_costfunctor.hpp
│   └── common.hpp
├── src
│   └── problem.cpp
└── test
    └── test_demo.cpp
# Minimum required version of CMake
cmake_minimum_required(VERSION 2.8)
project(demo)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Options to enable sanitizers
cmake_policy(SET CMP0054 NEW)
set(ENABLE_SAN "OFF" CACHE STRING "Options(ASAN, TSAN) to enable sanitizers")
if(ENABLE_SAN STREQUAL "ASAN")
 MESSAGE("ASAN")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
endif()
if(ENABLE_SAN STREQUAL "TSAN")
 MESSAGE("TSAN")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
endif()

# GCC/Clang compiler flags for each sanitizer
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
 MESSAGE("USE Clang")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Werror -Wno-overloaded-virtual -Wno-uninitialized -g -O0")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
 MESSAGE("USE GNU")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -static-libasan -Wall -Werror -Wno-overloaded-virtual -Wno-maybe-uninitialized -g -O0")
endif()
MESSAGE("cmake cxx compiler ${CMAKE_C_COMPILER}")
MESSAGE("cmake build type ${CMAKE_BUILD_TYPE}")
MESSAGE("cmake flags ${CMAKE_CXX_FLAGS}")

find_package(OpenCV 3 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
message("Opencv ${OpenCV_INCLUDE_DIRS}")
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIRS})
message("Eigen ${EIGEN3_INCLUDE_DIRS}")
find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})
message("Ceres ${CERES_INCLUDE_DIRS}")

include_directories(${PROJECT_SOURCE_DIR}/include)

file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_library(demo_lib ${SRC_FILES})
target_link_libraries(demo_lib ${OpenCV_LIBS} ${CERES_LIBRARIES} Eigen3::Eigen)

add_executable(demo test/test_demo.cpp)
target_link_libraries(demo ${demo_lib})

你可能感兴趣的:(问题解决办法,计算机通识,c++,linux)