声明下:本人想把CMakeLists中的内容写好,csdn格式化了,无能为力,以下是本人在生产环境中已经使用过了。
测试代码:
#include
int main()
{
//不支持linux
//#ifndef _DEBUG
#ifndef DEBUG
std::cout << “release” << std::endl;
#else
std::cout << “debug” << std::endl;
#endif
}
使用命令
cmake -DCMAKE_BUILD_TYPE=Debug … //编译出debug版本
cmake -DCMAKE_BUILD_TYPE=Release … //编译出release版本
以下是CMakeLists的编写
#Cmake最低版本要求
cmake_minimum_required(VERSION 3.10.0)
#设置编译进程名字
set(project_name name)
project(${project_name})
#设置版本号 1.0.1
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
set (VERSION_PATCH 1)
#取消第三方库头文件进行依赖检查,liunx加快编译速度
set(CMAKE_DEPENDS_IN_PROJECT_ONLY ON)
#输出编译器的类型
if(CMAKE_CONFIGURATION_TYPES)
message("–Multi-configuration generator")
else()
message("----Single-configuration generator")
endif()
#默认开启Debug
#liunx下是not,注意为空格式
if(NOT CMAKE_BUILD_TYPE OR “CMAKEBUILDTYPE"MATCHES"None"OR"{CMAKE_BUILD_TYPE}" MATCHES "None" OR "CMAKEBUILDTYPE"MATCHES"None"OR"{CMAKE_BUILD_TYPE}” MATCHES " “)
set(CMAKE_BUILD_TYPE “Debug” CACHE STRING “Build configuration” FORCE)
message(”----CMAKE_BUILD_TYPE FORCE SET Debug")
else()
#校验
if("CMAKEBUILDTYPE"MATCHES"Release"OR"{CMAKE_BUILD_TYPE}" MATCHES "Release" OR "CMAKEBUILDTYPE"MATCHES"Release"OR"{CMAKE_BUILD_TYPE}" MATCHES “RelWithDebInfo” OR “CMAKEBUILDTYPE"MATCHES"MinSizeRel"OR"{CMAKE_BUILD_TYPE}" MATCHES "MinSizeRel" OR "CMAKEBUILDTYPE"MATCHES"MinSizeRel"OR"{CMAKE_BUILD_TYPE}” MATCHES “Debug”)
set(CMAKE_BUILD_TYPE “${CMAKE_BUILD_TYPE}”)
message("----set CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
else()
message("----CMAKE_BUILD_TYPE is not right")
endif()
endif()
#检测linux下c++编译版版本
if(CMAKE_SYSTEM_NAME MATCHES “Linux”)
include(CheckCXXCompilerFlag)
foreach(cxx_feature_name “c++14” “c++1y” “c++11” “c++0x”)
set(cxx_feature_flag “-std=${cxx_feature_name}”)
string(REPLACE “+” “x” cxx_feature_suffix cxxfeaturename)CHECKCXXCOMPILERFLAG({cxx_feature_name}) CHECK_CXX_COMPILER_FLAG(cxxfeaturename)CHECKCXXCOMPILERFLAG({cxx_feature_flag} COMPILER_SUPPORTS_KaTeX parse error: Expected group after '_' at position 47: …MPILER_SUPPORTS_̲{cxx_feature_suffix})
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} ${cxx_feature_flag}”)
message("-- The compiler ${BUILDTOOL_PREFIX}-g++ has ${cxx_feature_name} support")
break()
endif()
endforeach()
endif()
#设置编译器格式
if (MSVC)
if (MSVC_VERSION LESS 1900)
message("— less vs2015 version")
endif()
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /O2 /bigobj")# was causing conflics with gtest build
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})if ("${MSVC_RUNTIME_LIBRARY_CONFIG}" STREQUAL "")set(MSVC_RUNTIME_LIBRARY_CONFIG "/MT")
endif ()foreach (flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE)if ("${MSVC_RUNTIME_LIBRARY_CONFIG}" STREQUAL "/MT")string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")elseif ("${MSVC_RUNTIME_LIBRARY_CONFIG}" STREQUAL "/MD")string(REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}")else ()string(REPLACE "/MD" "${MSVC_RUNTIME_LIBRARY_CONFIG}" ${flag_var} "${${flag_var}}")string(REPLACE "/MT" "${MSVC_RUNTIME_LIBRARY_CONFIG}" ${flag_var} "${${flag_var}}")endif ()
endforeach ()#增加宏定义
#add_definitions(-DTEST)
#add_definitions(-DUNICODE)
#add_definitions(-DWIN32_LEAN_AND_MEAN)
else ()
#设置c++编辑器
set(CMAKAE_CXX_COMPILER g++)
if(NOT CMAKE_SYSTEM_NAME MATCHES “Linux”)
set(make_mode c++14)
message("-- set makemodemakemode")set(CMAKECXXFLAGS−std={make_mode} make mode") set(CMAKE_CXX_FLAGS -std=makemodemakemode")set(CMAKECXXFLAGS−std={make_mode})
endif()
#设置C++编辑选项
set(CMAKE_CXX_FLAGS “CMAKECXXFLAGS−g−W−fpic−fPIC−lssl−lcrypto−ldl−lpthread−lboostsystem−lboostregex−lboostlocale−lboosttimer""−lboostlogsetup−lboostlog−lboostthread−lboostcoroutine−lboostdatetime−lboostfilesystem−lboostserialization−lboostprogramoptions")if(CMAKESIZEOFVOIDPEQUAL8)set(CMAKECXXFLAGS"{CMAKE_CXX_FLAGS} -g -W -fpic -fPIC -lssl -lcrypto -ldl -lpthread -lboost_system -lboost_regex -lboost_locale -lboost_timer" "-lboost_log_setup -lboost_log -lboost_thread -lboost_coroutine -lboost_date_time -lboost_filesystem -lboost_serialization -lboost_program_options") if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(CMAKE_CXX_FLAGS "CMAKECXXFLAGS−g−W−fpic−fPIC−lssl−lcrypto−ldl−lpthread−lboostsystem−lboostregex−lboostlocale−lboosttimer""−lboostlogsetup−lboostlog−lboostthread−lboostcoroutine−lboostdatetime−lboostfilesystem−lboostserialization−lboostprogramoptions")if(CMAKESIZEOFVOIDPEQUAL8)set(CMAKECXXFLAGS"{CMAKE_CXX_FLAGS} -m64”)
else()
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -m32”)
endif()
if(${CMAKE_BUILD_TYPE} MATCHES “Debug”)
message("-- CMAKE_BUILD_TYPE is KaTeX parse error: Expected 'EOF', got '#' at position 28: …_TYPE} mode") #̲设置C++debug版本编辑选…ENV{CXXFLAGS} -O0 -DDEBUG -D_DEBUG -ggdb")
else()
message("-- CMAKE_BUILD_TYPE is KaTeX parse error: Expected 'EOF', got '#' at position 28: …_TYPE} mode") #̲设置C++release编辑选…ENV{CXXFLAGS} -O3 -g1 -DNDEBUG -DN_DEBUG")
endif()
endif ()
#第三方库搜索头文件
include_directories(/usr/local/include)
#第三方库的lib
link_directories(/usr/local/lib)
link_directories(/usr/lib64)
include_directories( ${CMAKE_SOURCE_DIR}/src )
#可执行文件生成位置
set(EXECUTABLE_OUTPUT_PATH “${CMAKE_SOURCE_DIR}/bin”)
file(GLOB SOURCE_MAIN “${CMAKE_SOURCE_DIR}/src/*.cpp”)
add_executable("${PROJECT_NAME}" ${SOURCE_MAIN})
set(OPENSSL_USE_STATIC_LIBS TRUE)
#添加链接库 静态库
target_link_libraries("${PROJECT_NAME}"libssl.a libcrypto.a)