Cmake 和 Qt5 链接错误

Cmake and Qt5 linking error

我正在尝试使用 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

这是我的CMakeLists.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 查看 this link