【问题标题】:How to integrate QtMultimedia component into a project using CMake?如何使用 CMake 将 QtMultimedia 组件集成到项目中?
【发布时间】:2018-09-04 15:55:44
【问题描述】:

我正在尝试使用 Qt5 和 CMake 编写一个 GUI 应用程序(在 Ubuntu 18.04 下)。我未能将 Qt 多媒体模块集成到我的项目中。

我的CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(Chess)

#set(CMAKE_PREFIX_PATH "/home/mashplant/Qt5.9.6/5.9.6/gcc_64")
#whether I comment it off or not doesn't have an effect on the result

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 COMPONENTS Widgets Network Multimedia REQUIRED)

set(CMAKE_CXX_STANDARD 11)

include_directories(.)

add_executable(Main Main.cpp
    MainWindow.cpp MainWindow.hpp ChessFrame.cpp ChessFrame.hpp resource.qrc)

target_link_libraries(Main Qt5::Widgets Qt5::Network Qt5::Multimedia)

我在运行 CMake 时收到以下警告:

CMake Warning at CMakeLists.txt:18 (add_executable):
Cannot generate a safe runtime search path for target Main because files in
some directories may conflict with libraries in implicit directories:

runtime library [libQt5Widgets.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Network.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Gui.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Core.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
  /home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
Some of these libraries may not be found correctly.

当我编译时,我得到一个链接错误。

/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib/libQt5Multimedia.so.5.9.6:undefined reference to ‘operator delete(void*, unsigned long)@Qt_5’
collect2: error: ld returned 1 exit status
CMakeFiles/Main.dir/build.make:203: recipe for target 'Main' failed
make[3]: *** [Main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Main.dir/all' failed
make[2]: *** [CMakeFiles/Main.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/Main.dir/rule' failed
make[1]: *** [CMakeFiles/Main.dir/rule] Error 2
Makefile:118: recipe for target 'Main' failed
make: *** [Main] Error 2

【问题讨论】:

  • 问题不在于多媒体 QT 组件,而在于一般的 QT。您的系统上安装了 2 个 QT:系统一个,在/usr/lib/x86_64-linux-gnu 下,您自己的一个,在/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib 下。但是 CMake 无法为链接器生成命令行,因此它可以选择正确的 QT 安装。这是因为系统和您的目录中的库都在您的项目中使用。如果要使用 QT 安装,请删除系统范围的安装。在新尝试之前不要忘记清除 CMake 缓存(构建目录中的CMakeCache.txt 文件)。
  • @Tsyvarev 但是在以前的一些没有多媒体的项目中,它运行良好。这就是为什么我想知道如何整合多媒体。
  • @MashPlant 它可能是偶然工作的。如果您在这些项目中也没有从 CMake 收到相同的警告,我会感到非常惊讶(甚至 QtCore 似乎也存在冲突)。您可以尝试使用此选项 -DQt5_DIR=/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib/cmake/Qt5 发布 CMake 吗?

标签: c++ linux qt cmake


【解决方案1】:

我最好的选择是 CMake 从不同的 Qt 安装中找到不同的组件。如果是这种情况,应该足以纠正所有与 Qt 相关的 CMake 变量以指向正确的路径(例如,Qt5Widgets_DIRQt5Multimedia_DIR 等变量)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-11
    • 2014-08-31
    • 2014-04-08
    • 2016-10-26
    • 2010-10-27
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多