8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

无法构建 Cmake Qt 程序;ld 警告“未定义的引用...”

collab-with-tushar-raj 1月前

13 0

我试图构建一个使用 qt 的 cmake 程序。在完成所有设置后,我通过 C v13.2.0-gcc 通过 v3.30.3 cmake --build .-- qETRC 在 master [!] 上收到来自 ld:qETRC/build 的错误

我试图构建一个使用 qt 的 cmake 程序。设置好一切后,我从 ld 收到此错误:

qETRC/build on  master [!] via C v13.2.0-gcc via  v3.30.3 
 cmake --build .
-- qETRC version 1.7.5
-- QtPrintSupport is found
-- SARibbon lib /usr/local/lib/libSARibbonBar.so.2.0.0
-- Could NOT find QWindowKit (missing: QWindowKit_DIR)
-- QWK not found, install not performed
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/jeremy/code/qETRC/build
[  0%] Built target qt6advanceddocking_autogen_timestamp_deps
[  1%] Built target qt6advanceddocking_autogen
[ 10%] Built target qt6advanceddocking
[ 11%] Built target qETRC_lrelease
[ 11%] Built target qETRC_autogen_timestamp_deps
[ 11%] Automatic MOC for target qETRC
[ 11%] Built target qETRC_autogen
[ 12%] Linking CXX executable qETRC
/usr/bin/ld: CMakeFiles/qETRC.dir/qETRC_autogen/mocs_compilation.cpp.o:(.data.rel.ro._ZTV10MainWindow[_ZTV10MainWindow]+0x110): undefined reference to `SARibbonMainWindow::resizeEvent(QResizeEvent*)'
/usr/bin/ld: CMakeFiles/qETRC.dir/qETRC_autogen/mocs_compilation.cpp.o:(.data.rel.ro._ZTV10MainWindow[_ZTV10MainWindow]+0x170): undefined reference to `SARibbonMainWindow::changeEvent(QEvent*)'
/usr/bin/ld: CMakeFiles/qETRC.dir/src/mainwindow/mainwindow.cpp.o: in function `MainWindow::initToolbar()':
mainwindow.cpp:(.text+0xf7e1): undefined reference to `SARibbonMainWindow::setRibbonTheme(SARibbonMainWindow::RibbonTheme)'
/usr/bin/ld: CMakeFiles/qETRC.dir/src/mainwindow/mainwindow.cpp.o: in function `MainWindow::onRibbonConfigChanged(bool)':
mainwindow.cpp:(.text+0x157e0): undefined reference to `SARibbonMainWindow::setRibbonTheme(SARibbonMainWindow::RibbonTheme)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/qETRC.dir/build.make:4293: qETRC] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:126: CMakeFiles/qETRC.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

我做了快速研究,谷歌告诉我这可能与 CMakeLists.txt 有关,但我和原作者都不知道如何解决这个问题。

这是 CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.14)

set(qETRC_VERSION_MAJOR 1)
set(qETRC_VERSION_MINOR 7)
set(qETRC_VERSION_PATCH 5)
#set(qETRC_RELEASE_CODE 40)

option(ENABLE_MSVC_PROFILE "Enable MSVC profiler" OFF)
option(AUTO_INSTALL_SARibbon "Install SARibbon libraries" ON)
option(AUTO_INSTALL_QWK "Install QWindowKit libraries, dependent by SARibbon" ON)

set(qETRC_VERSION "${qETRC_VERSION_MAJOR}.${qETRC_VERSION_MINOR}.${qETRC_VERSION_PATCH}")

message(STATUS "qETRC version ${qETRC_VERSION}")

project(qETRC LANGUAGES CXX VERSION ${qETRC_VERSION})


################# Qt basic ################
#list(PREPEND CMAKE_PREFIX_PATH $ENV{QTDIR}/lib/cmake)
#message(STATUS "env QTDIR $ENV{QTDIR}")
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} 5.11 COMPONENTS Core Gui Widgets LinguistTools REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} 5.11 COMPONENTS PrintSupport)

################## main ##################
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

configure_file(
    version_predef.h.in
    version_predef.h
    @ONLY
)

file(GLOB_RECURSE qETRC_SOURCES "src/*.cpp")
file(GLOB_RECURSE qETRC_HEADERS "src/*.h")
file(GLOB_RECURSE qETRC_HEADERS_PP "src/*.hpp")

if (MSVC)
    set(app_icon_resource_windows "${CMAKE_CURRENT_SOURCE_DIR}/icon.rc")
endif()

# add_executable(qETRC
#     ${qETRC_SOURCES}
#     rsc/resource.qrc
#     ${app_icon_resource_windows}
# )
set(PROJECT_SOURCES ${qETRC_SOURCES} ${qETRC_HEADERS} ${qETRC_HEADERS_PP}
    rsc/resource.qrc ${app_icon_resource_windows})


if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(qETRC
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET QtAndroidCMakeTest APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(qETRC SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
   set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Android_source")
    else()
        add_executable(qETRC
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_include_directories(qETRC PRIVATE src/)

target_link_libraries(qETRC PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

if (Qt${QT_VERSION_MAJOR}PrintSupport_FOUND)
    message(STATUS "QtPrintSupport is found")
    target_link_libraries(qETRC PRIVATE Qt${QT_VERSION_MAJOR}::PrintSupport)
else()
    message(WARNING "QtPrintSupport not found")
endif()


################# external libraries ##################
include(FetchContent)

if (NOT ANDROID)
    # disable example building in SARibbon, since some of them requires Qt6 ..
    set(BUILD_EXAMPLES OFF)

    # 2024.03.28 trial: use find_package for SARibbon
    # add_subdirectory(external/SARibbon)
    find_package(SARibbonBar REQUIRED)
    
    # list(APPEND CMAKE_PREFIX_PATH "D:/codes/QtProjects/site-packages/Qt-Advanced-Docking-System/out/install/lib/cmake/qt5advanceddocking")
    # find_package(qt${QT_VERSION_MAJOR}advanceddocking CONFIG REQUIRED)

    if (NOT ADS_REPO_URL)
        set(ADS_REPO_URL https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System)
    endif ()

    # try: using FetchContent
    FetchContent_Declare(
        ads
        # GIT_REPOSITORY ${CMAKE_SOURCE_DIR}/external/Qt-Advanced-Docking-System
        GIT_REPOSITORY ${ADS_REPO_URL}
        GIT_TAG 4.0.3
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/ads
    )
    FetchContent_MakeAvailable(ads)

    target_link_libraries(qETRC PUBLIC SARibbonBar)
    target_link_libraries(qETRC PRIVATE ads::qt${QT_VERSION_MAJOR}advanceddocking)

    if (AUTO_INSTALL_SARibbon)
        get_target_property(SARibbonBar_LIB_FILE SARibbonBar LOCATION_${CMAKE_BUILD_TYPE})
        message(STATUS "SARibbon lib ${SARibbonBar_LIB_FILE}")
        install(FILES ${SARibbonBar_LIB_FILE} DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif (AUTO_INSTALL_SARibbon)

    if (AUTO_INSTALL_QWK) 
        if (NOT QWindowKit_DIR)
            set (QWindowKit_DIR ${SARibbonBar_DIR}/../QWindowKit)
        endif ()

        find_package(QWindowKit CONFIG)
        if (QWindowKit_FOUND)
            get_target_property(QWK_CORE_FILE QWindowKit::Core LOCATION_${CMAKE_BUILD_TYPE})
            get_target_property(QWK_WIDGETS_FILE QWindowKit::Widgets LOCATION_${CMAKE_BUILD_TYPE})
            message(STATUS "QWK lib ${QWK_CORE_FILE}")
            install(FILES ${QWK_CORE_FILE} ${QWK_WIDGETS_FILE} DESTINATION ${CMAKE_INSTALL_BINDIR})
        else ()
            message(STATUS "QWK not found, install not performed")
        endif ()
    endif (AUTO_INSTALL_QWK)
endif()


#################### miscellaneous #####################

if (ANDROID)
    target_compile_definitions(qETRC PUBLIC QETRC_MOBILE QETRC_MOBILE_2)
endif()

if (MSVC)
    target_compile_options(qETRC PRIVATE /utf-8 /wd4267 /Zc:__cplusplus ) 

    # MSVC profiler  https://learn.microsoft.com/zh-cn/cpp/build/reference/profile-performance-tools-profiler?view=msvc-170
    if (ENABLE_MSVC_PROFILE)
        message(STATUS "MSVC profiler enabled")
        #SET_TARGET_PROPERTIES(ads::qt${QT_VERSION_MAJOR}advanceddocking PROPERTIES LINK_FLAGS "/PROFILE")
        SET_TARGET_PROPERTIES(qETRC PROPERTIES LINK_FLAGS "/PROFILE")
    endif ()

endif()

if (WIN32 AND (CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
    message("disable console for Release buildings.")
    # disable console for release version
    set_property(TARGET qETRC PROPERTY WIN32_EXECUTABLE true)
endif()

if(WIN32)
    include(cmake/WinResource.cmake)
    create_win32_resource_version(
                TARGET qETRC
                FILENAME qETRC
        EXT "exe"
        DESCRIPTION "qETRC train working diagram system."
    )
endif()

# translations
qt_add_lupdate(qETRC TS_FILES tr/qETRC_zh.ts tr/qETRC_en.ts)
qt_add_lrelease(qETRC TS_FILES tr/qETRC_zh.ts tr/qETRC_en.ts)

install(TARGETS qETRC
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

# install PDB
if (MSVC AND NOT CMAKE_BUILD_TYPE STREQUAL "Release")
    install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}> 
    DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(qETRC)
endif()

预期的行为是程序构建并输出 qetrc 可执行文件。

如果有帮助的话,可以在 Windows 上构建该程序。

帖子版权声明 1、本帖标题:无法构建 Cmake Qt 程序;ld 警告“未定义的引用...”
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由collab-with-tushar-raj在本站《qt》版块原创发布, 转载请注明出处!
最新回复 (0)
  • - 您没有实现该函数(但在标题中定义了它)或者没有编译它 - 由于您没有提供最小的可编译再现器,我们无法说出哪里出了问题。

返回
作者最近主题: