💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ### QT工程CMakeLists.txt编写 ***** ### 以项目goflywayQT工程为例 ***** ``` -- GoflywayQT -- CMakeLists.txt -- -- goflyway -- -- CMakeLists.txt ``` goflywayQT目录的CMakeLists.txt 最小cmake版本 ``` cmake_minimum_required(VERSION 2.6) project(GoflywayQT LANGUAGES CXX) ``` 自动调用moc,uic,rcc处理qt的扩展部分 ``` set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) ``` 查找依赖库包 ``` if(APPLE AND EXISTS /usr/local/opt/qt5) # Homebrew installs Qt5 (up to at least 5.9.1) in # /usr/local/qt5, ensure it can be found by CMake since # it is not in the default /usr/local prefix. list(APPEND CMAKE_PREFIX_PATH "/usr/local/opt/qt5") endif() find_package(Qt5Core) find_package(Qt5Gui) find_package(Qt5Widgets) find_package(Qt5Network) find_package(PkgConfig) if(UNIX AND NOT APPLE) find_package(Qt5DBus) endif() if(WIN32 OR APPLE) add_definitions(-DFD_SETSIZE=1024) endif() ``` 添加子目录goflyway的cmakelists.txt: ``` add_subdirectory(goflyway) ``` 子目录goflyway的CMakeLists.txt: ``` cmake_minimum_required(VERSION 3.1) PROJECT(Goflyway) SET(PROJECT_OUTNAME GOFLYWAY) //头文件目录 INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/inc ${PROJECT_SOURCE_DIR}/ui ) //自动引用当前目录文件 set(CMAKE_INCLUDE_CURRENT_DIR ON) ``` `QT5_WRAP_CPP`包裹cpp文件, `QT5_WRAP_UI`使ui文件自动包裹成ui.h文件,`QT5_ADD_RESOURCES` 包裹资源文件 ``` # Sources ############################################################################## file(GLOB_RECURSE SRC_FILES src/*.cpp) #同理,查找当前文件夹中所有的头文件 file(GLOB_RECURSE HEAD_FILES inc/*.h) file(GLOB_RECURSE ui_files ui/*.ui) file(GLOB_RECURSE res_files res/*.qrc) QT5_WRAP_CPP(HEADER_MOC ${HEAD_FILES}) QT5_WRAP_UI(UI_MOC ${ui_files}) QT5_ADD_RESOURCES(QRC_MOC ${res_files}) ADD_EXECUTABLE(${PROJECT_OUTNAME} ${SRC_FILES} ${HEADER_MOC} ${UI_MOC} ${QRC_MOC}) ``` 链接库文件跟库使用的头文件目录: ``` target_link_libraries(${PROJECT_OUTNAME} PUBLIC Qt5::Core PUBLIC Qt5::Gui PUBLIC Qt5::Widgets PUBLIC Qt5::Network PRIVATE ${QSS_LIBRARY_VAR} PRIVATE ${QRENCODE_LIBRARY_VAR} PRIVATE ${ZBAR_LIBRARY_VAR}) target_include_directories(${PROJECT_OUTNAME} PRIVATE ${QSS_INCLUDE_DIRS} PRIVATE ${QRENCODE_INCLUDE_DIRS} PRIVATE ${ZBAR_INCLUDE_DIRS}) if (UNIX AND NOT APPLE) target_link_libraries(${PROJECT_OUTNAME} PRIVATE Qt5::DBus) endif() ``` ### 静态库的CMakelists.txt ***** ``` file(GLOB_RECURSE SRC_FILES src/*.cpp) #同理,查找当前文件夹中所有的头文件 file(GLOB_RECURSE HEAD_FILES inc/*.h) #ADD_LIBRARY(${PROJECT_OUTNAME} SHARED ${SRC_FILES} )//动态库 ADD_LIBRARY(${PROJECT_OUTNAME} STATIC ${SRC_FILES} )//静态库 SET_TARGET_PROPERTIES(${PROJECT_OUTNAME} PROPERTIES LINK_FLAGS /MAP) ``` ### 安装 ***** ``` install(TARGETS ${PROJECT_OUTNAME} RUNTIME DESTINATION bin) if (UNIX) #install(FILES shadowsocks-qt5.desktop DESTINATION share/applications) #install(FILES icons/shadowsocks-qt5.png DESTINATION share/icons/hicolor/512x512/apps) endif() MESSAGE(STATUS "${PROJECT_OUTNAME} is BINARY dir:" ${PROJECT_BINARY_DIR}) MESSAGE(STATUS "${PROJECT_OUTNAME} is SOURCE dir:" ${PROJECT_SOURCE_DIR}) ```