Cmake 和 Qt5 链接错误

Cmake and Qt5 linking error

本文关键字:错误 链接 Qt5 Cmake      更新时间:2023-10-16

我正在尝试使用 Cmake 构建一个 Qt5 项目,以便添加一些新的库。cmake 运行顺利,但我在构建时遇到了链接问题:

Linking CXX executable bin/qGo
CMakeFiles/qGo.dir/src/main.cpp.o: dans la fonction « main »:
main.cpp:(.text+0x102b): undefined reference to « qInitResources_application() »
collect2: error: ld returned 1 exit status
make[2]: *** [bin/qGo] Erreur 1
make[1]: *** [CMakeFiles/qGo.dir/all] Erreur 2
make: *** [all] Erreur 2

这是我的CMakeList.txt :

cmake_minimum_required(VERSION 2.8.11)
project (qGo)
SET(CMAKE_MODULE_PATH /usr/local/lib/cmake/)
# Répertoire d'installation de Qt5 (dépend de l'installation)
SET(CMAKE_PREFIX_PATH "~/Qt/5.4/gcc/")
find_package (OpenCV REQUIRED)
find_package (aruco REQUIRED)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Use moc files in the bin folder
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
# Find the Qt5 components
find_package(Qt5Core)
find_package(Qt5Widgets)
find_package(Qt5Network)
find_package(Qt5Multimedia)
set(EXECUTABLE_OUTPUT_PATH bin)
SET(CMAKE_CXX_FLAGS "-std=c++11")
include_directories(src)
include_directories(src/audio)
include_directories(src/board)
include_directories(src/game_interfaces)
include_directories(src/game_tree)
include_directories(src/gtp)
include_directories(src/network)
include_directories(src/resources)
include_directories(src/sgf)
include_directories(src/translations)
file(
        GLOB_RECURSE
        source_files
        src/*
)
file(
        GLOB_RECURSE
        ui_files
        src/*.ui
)
file(
        GLOB_RECURSE
        header_files
        src/*.h
        src/*.hpp
)
QT5_WRAP_UI(header_ui ${ui_files})
# Tell CMake to create the helloworld executable
add_executable(qGo ${source_files} ${header_ui})
# Use the Widgets module from Qt 5.
target_link_libraries(qGo Qt5::Core Qt5::Widgets Qt5::Network Qt5::Multimedia ${OpenCV_LIBS} ${aruco_LIBS})

我也尝试添加带有 ${Qt5Widgets_INCLUDES} 或 ${Qt5Widgets_DEFINITIONS} 的库,但它做了同样的事情。

我也尝试使用QtCreator进行编译,它可以工作,所以问题出在cmake上。

我认为您忘记将moc和资源附加到可执行文件使用qt5_wrap_cpp()用于mocs使用qt5_add_resources()用于资源。

然后,您必须附加变量以add_executable查看此链接。

相关文章: