#cmake最低版本 cmake_minimum_required(VERSION 3.19) SET(VCPKG_ROOT "F:/SourceCode/vcpkg") SET(CMAKE_TOOLCHAIN_FILE "F:/SourceCode/vcpkg/scripts/buildsystems/vcpkg.cmake") SET(VCPKG_TARGET_TRIPLET "x64-mingw-static") SET(CMAKE_C_COMPILER "x86_64-w64-mingw32-gcc.exe") SET(CMAKE_CXX_COMPILER "x86_64-w64-mingw32-g++.exe") #项目版本 project(XSteam VERSION 1.0.0 LANGUAGES CXX) configure_file (version.h.in version.h @ONLY) #设置moc ui rcc编译开关 SET(CMAKE_AUTOMOC ON) SET(CMAKE_AUTOUIC ON) SET(CMAKE_AUTORCC ON) #设置C++版本 SET(CMAKE_CXX_STANDARD 17) #包含所有.h文件 SET(CMAKE_INCLUDE_CURRENT_DIR ON) #release编译下依然输出符号和行号 ADD_DEFINITIONS(-DQT_MESSAGELOGCONTEXT) #编译类型 debug和release输出路径 SET(CMAKE_VERBOSE_MAKEFILE ON) SET(CMAKE_CXX_FLAGS"-Wall -Wextra") SET(CMAKE_CXX_FLAGS_RELEASE"-O3 -s") SET(CMAKE_BUILD_TYPE "Release") MESSAGE(STATUS ${PROJECT_BINARY_DIR}) MESSAGE(STATUS "----------编译模式 START-------------") if(NOT CMAKE_BUILD_TYPE) MESSAGE(STATUS "[CMAKE_BUILD_TYPE]当前值[Debug]") SET(CMAKE_BUILD_TYPE "Debug") SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/build/debug) else() MESSAGE(STATUS "[CMAKE_BUILD_TYPE]当前值[Release]") SET(CMAKE_BUILD_TYPE "Release") SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/release/) endif() MESSAGE(STATUS "----------编译模式 END---------------") #设置QT .cmake文件路径 #自编译QT静态库 SET(QT_STATU "static") if(QT_STATU STREQUAL "dynamic") SET(QT_DIR "D:/Dev/Qt/6.8.0/mingw_64/lib/cmake") else() SET(QT_DIR "D:/Dev/Qt/6.8.0/mingw1310_static_x64/lib/cmake") endif() SET(JSON ${PROJECT_SOURCE_DIR}/3rdparty/json) LIST(APPEND CMAKE_PREFIX_PATH ${QT_DIR} ${MSYS}) #查找QT模块 FIND_PACKAGE(Qt6 REQUIRED Core Gui Widgets) #输出路径 MESSAGE(STATUS "----------基础路径输出 START-------------") MESSAGE(STATUS "QT_DIR_PATH=${QT_DIR}") MESSAGE(STATUS "JSON_DIR_PATH=${JSON}") MESSAGE(STATUS "----------基础路径输出 END---------------") #LIBGIT2库 find_package(PkgConfig REQUIRED QUIET) pkg_check_modules(LIBGIT2 REQUIRED IMPORTED_TARGET libgit2) if(LIBGIT2_FOUND) include_directories(${LIBGIT2_INCLUDE_DIRS}) message(STATUS "------------LIBGIT2 INFO----------------------") message(STATUS "LIBGIT2_FOUND=Yes") message(STATUS "LIBGIT2 Found:->LIBGIT2_LIBRARIES:${LIBGIT2_LIBRARIES}") message(STATUS "LIBGIT2 Found:->LIBGIT2_Version:${LIBGIT2_VERSION}") message(STATUS "LIBGIT2 Found:->LIBGIT2_INCLUDE_DIR_PATH=${LIBGIT2_INCLUDE_DIRS}") message(STATUS "LIBGIT2 Found:->LIBGIT2_LIBRARY_DIRS:${LIBGIT2_LIBRARY_DIRS}") message(STATUS "LIBGIT2 Found:->LIBGIT2_LINK_LIBRARIES:${LIBGIT2_LINK_LIBRARIES}") message(STATUS "LIBGIT2 Found:->LIBGIT2_STATIC_LIBRARIES:${LIBGIT2_STATIC_LIBRARIES}") else() message(STATUS "LIBGIT2_FOUND=NOT") endif() #MSYS2 CURL库 if(WIN32) add_definitions("-DCURL_STATICLIB") endif() find_package(PkgConfig REQUIRED) pkg_check_modules(CURL REQUIRED IMPORTED_TARGET libcurl) if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIRS}) message(STATUS "------------CURL INFO START---------------------") message(STATUS "CURL_FOUND=Yes") message(STATUS "Curl Found:->CRUL_LIBRARIES:${CURL_LIBRARIES}") message(STATUS "Curl Found:->CURL_Version:${CURL_VERSION}") message(STATUS "Curl Found:->CURL_INCLUDE_DIR_PATH:${CURL_INCLUDE_DIRS}") message(STATUS "Curl Found:->CURL_LIBRARY_DIRS:${CURL_LIBRARY_DIRS}") message(STATUS "Curl Found:->CURL_LINK_LIBRARIES:${CURL_LINK_LIBRARIES}") message(STATUS "Curl Found:->CURL_STATIC_LIBRARIES:${CURL_STATIC_LIBRARIES}") message(STATUS "------------CURL INFO END---------------------") else() message(STATUS "CURL_FOUND=NOT") endif() #JSON库 include_directories(${JSON}/include) #增加XSteam的cpp FILE(GLOB src_xsteam "${PROJECT_SOURCE_DIR}/src/*.cpp" "${PROJECT_SOURCE_DIR}/src/*.h") SET(PROJECT_SOURCES ${src_xsteam}) # 设置消息策略为qFatal来禁用qDebug信息 LIST(APPEND CMAKE_CXX_FLAGS "-DQT_NO_DEBUG_OUTPUT") #资源文件 QT6_ADD_RESOURCES(RCFILES res.qrc) #WIN32 add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SOURCES} ${RCFILES} xsteam.rc) #安全编译 #关闭RPATH特性 SET(CMAKE_SKIP_RPATH TRUE) #开启栈保护 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong") #开启GOT表保护 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,relro,-z,now") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -Wl,-z,relro,-z,now") #开启栈不可执行 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -Wl,-z,noexecstack") #开启地址无关 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpic") #随机化 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpie") SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pie") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -pie") #隐藏暴露的符号 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fvisibility=hidden") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -fvisibility=hidden") SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fvisibility-inlines-hidden") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -fvisibility-inlines-hidden") #删除调试符号 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s") SET(CMAKE_EXECUTABLE_LINKER_FLAGS "${CMAKE_EXECUTABLE_LINKER_FLAGS} -s") #删除生成EXE符号信息 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-s") #开启整数溢出检查 SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -ftrapv") #开启栈检查 SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -fstack-check") #忽略多重定引用 target_link_options(${PROJECT_NAME} PUBLIC -Wl,--allow-multiple-definition) # 打印GCC版本号信息 message(STATUS "G++ version: ${CMAKE_CXX_COMPILER_VERSION}") # 获取Qt版本号 SET(QT_VERSION_STR "${Qt6_VERSION}") # 打印Qt版本号 message(STATUS "Qt Version: ${QT_VERSION_STR}") #设定版本 configure_file (version.h.in version.h @ONLY) # 设置链接标志为静态链接 SET(CMAKE_EXE_LINKER_FLAGS -static) target_link_libraries( ${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets PkgConfig::LIBGIT2 PkgConfig::CURL )