赞
踩
include命令
说到cmake,可能最先想到的就是CmakeLists.txt文件,但是在很多情况下,也会看到.cmake文件。
cmake文件是干什么的,甚至会想.cmake文件是不是cmake的正统文件,而CmakeLists.txt并不是。但其实,CmakeLists.txt才是cmake的正统文件,而.cmake文件是一个模块文件,可以被include到CMakeLists.txt中。
include指令一般用于语句的复用,也就是说,如果有一些语句需要在很多CMakeLists.txt文件中使用,为避免重复编写,可以将其写在.cmake文件中,然后在需要的CMakeLists.txt文件中进行include操作就行了。
include指令的结构为:
include(<file|module> [OPTIONAL] [RESULT_VARIABLE ] [NO_POLICY_SCOPE])
注意:
为了使CMakeLists.txt能够找到该文件,需要指定文件完整路径(绝对路径或相对路径),当然如果指定了CMAKE_MODULE_PATH,就可以直接include该目录下的.cmake文件了。
举例说明:
默认的cmakelists
#工程名 project(uXXXXSoftware) #cmake最低版本 cmake_minimum_required(VERSION 3.17) #设定cmake模块的路径 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake # 项目cmake ${PROJECT_SOURCE_DIR}/cmake/base # 通用cmake ${CMAKE_MODULE_PATH}) #加载配置项 include(set_env) #加载项目配置 include(init_project) #加载编译配置 include(set_compile_arg)
base/set_env.cmake
message(STATUS "====================set_env.cmake===================") # 处理编译字符集问题 add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>") add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") #C++标准 set(CMAKE_CXX_STANDARD 11) # 开启QT用于预处理的组件 set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) #设置全局系统变量 set(PROJECT_EXTERNAL_DIR "D:/MyLib") # 设置webview2变量 set(wixlabrary_INCLUDE_DIRS "${PROJECT_EXTERNAL_DIR}/include/external/WixLibrary") set(webview2_INCLUDE_DIRS "${PROJECT_EXTERNAL_DIR}/include/external/WebView2") set(webview2_LIBS "${PROJECT_EXTERNAL_DIR}/lib/external") message(STATUS "wixlabrary_INCLUDE_DIRS is ${wixlabrary_INCLUDE_DIRS}") message(STATUS "webview2_INCLUDE_DIRS is ${webview2_INCLUDE_DIRS}") message(STATUS "webview2_LIBS is ${webview2_LIBS}") #配置QT位置 message(STATUS "CMAKE_SYSTEM_NAME is ${CMAKE_SYSTEM_NAME}") if (CMAKE_SYSTEM_NAME MATCHES "Windows") #下面是Windows下Qt的默认前缀 set(CMAKE_PREFIX_PATH "D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake") elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_NAME MATCHES "Linux") #下面是aarch64 Linux下Qt的默认前缀 set(CMAKE_PREFIX_PATH /opt/Qt5.14.2_aarch64/) elseif (CMAKE_SYSTEM_NAME MATCHES "Linux") #下面是Linux x64下Qt的默认前缀 set(CMAKE_PREFIX_PATH /opt/Qt5.14.2/5.14.2/gcc_64) endif () message(STATUS "====================set_env.cmake end===================")
base/init_project.cmake
# 查找QT的模块 find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED) #加载webview2依赖的头文件 include_directories(SYSTEM ${wixlabrary_INCLUDE_DIRS} ${webview2_INCLUDE_DIRS}) #设置要搜索库文件的路径 link_directories(${webview2_LIBS}) # 把目录Packages/MainFrame/src下面的所有文件作为变量存储 aux_source_directory(Packages/MainFrame/src SOURCES) # 添加源文件 add_executable(uChangAnSoftware ${SOURCES}) # 添加模块 target_link_libraries(uChangAnSoftware Qt5::Core Qt5::Gui Qt5::Widgets WebView2LoaderStatic )
set_compile_arg.cmake
# 默认的编译配置(可选) if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) set(DEBUG_SUFFIX) if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug") set(DEBUG_SUFFIX "d") endif () set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") if (NOT EXISTS "${QT_INSTALL_PATH}/bin") set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") if (NOT EXISTS "${QT_INSTALL_PATH}/bin") set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") endif () endif () if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/") add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/") endif () foreach (QT_LIB Core Gui Widgets) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>") endforeach (QT_LIB) endif ()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。