其他分享
首页 > 其他分享> > 记录CMake中使用QT5的Multimedia的坑

记录CMake中使用QT5的Multimedia的坑

作者:互联网

记录Mac系统下的qt5项目在安装qt6后,项目无法编译成功的一次排错。

Mac系统下 使用brew安装了qt5 、qtcreator 后,使用qtcreator创建了一个qHello项目。

以下是当时创建的CMakeLists.txt文件。

cmake_minimum_required(VERSION 3.5)

project(qHello LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

# set (QT_DIR /usr/local/opt/qt@5/lib/cmake)
set (CMAKE_PREFIX_PATH /usr/local/opt/qt@5/lib/cmake)

find_package(QT NAMES Qt5 COMPONENTS Widgets Multimedia REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Multimedia REQUIRED)

IF (WIN32)
	MESSAGE(STATUS "Now is windows")
ELSEIF (APPLE)
	MESSAGE(STATUS "Now is Apple systens.")
ELSEIF (UNIX)
	MESSAGE(STATUS "Now is UNIX-like OS's. Including aPPLE os x  and CygWin")
ENDIF ()

MESSAGE(STSTUS "###################################")

MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
	MESSAGE(STATUS "current platform: Linux ")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
	MESSAGE(STATUS "current platform: Windows")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	MESSAGE(STATUS "current platform: FreeBSD")
ELSE ()
	MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")

MESSAGE(STSTUS "###################################")



if(ANDROID)
  add_library(qHello SHARED
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
else()
  add_executable(qHello
    main.cpp
    example/components/qtui/blank/mainwindow.cpp
#    example/components/qtui/blank/mainwindow.h
    example/components/qtui/blank/mainwindow.ui

#    example/components/qtui/player/mediaplayer.h
    example/components/qtui/player/mediaplayer.cpp
    example/components/qtui/player/mediaplayer.ui

#    example/components/qtui/player/myslider.h
    example/components/qtui/player/myslider.cpp
    
#    example/components/qtui/player/customslider.h
    example/components/qtui/player/customslider.cpp

    example/components/qtui/login/logindialog.cpp
#    example/components/qtui/login/logindialog.h
  )
endif()

target_link_libraries(qHello PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Multimedia)

问题是在使用brew安装了Qt6之后,就再也不能编译成功了。

编译时的报错信息如下:

fatal error: 'QVideoWidget' file not found
#include <QVideoWidget>

在文件中添加include目录配置后,又报错了,这次的报错信息如下:

Undefined symbols for architecture x86_64:
  "QVideoWidget::QVideoWidget(QWidget*)", referenced from:

在尝试了若干次配置更新后,发现qt5中的CMake组件有Multimedia 、MultimediaQuick、 MultimediaWidgets 3个framework的配置 。于是在CMake项目配置文件中添加 MultimediaWidgets后,终于编一成功了,以下是更新后的CMakeLists.txt文件。

cmake_minimum_required(VERSION 3.5)

project(qHello LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

# set (QT_DIR /usr/local/opt/qt@5/lib/cmake)
set (CMAKE_PREFIX_PATH /usr/local/opt/qt@5/lib/cmake)



find_package(QT NAMES Qt5 COMPONENTS Widgets Multimedia MultimediaWidgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Multimedia MultimediaWidgets REQUIRED)
# include_directories(/usr/local/opt/qt@5/include/QtMultimediaWidgets)
# include_directories(${QT_INCLUDE_DIR})

include_directories(${Qt5MultimediaWidgets_INCLUDE_DIRS})


message(STATUS " qtinclude ${QT_INCLUDE_DIR} ${Qt5MultimediaWidgets_INCLUDE_DIRS}" )
IF (WIN32)
	MESSAGE(STATUS "Now is windows")
ELSEIF (APPLE)
	MESSAGE(STATUS "Now is Apple systens.")
ELSEIF (UNIX)
	MESSAGE(STATUS "Now is UNIX-like OS's. Including aPPLE os x  and CygWin")
ENDIF ()

MESSAGE(STSTUS "###################################")

MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
	MESSAGE(STATUS "current platform: Linux ")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
	MESSAGE(STATUS "current platform: Windows")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	MESSAGE(STATUS "current platform: FreeBSD")
ELSE ()
	MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")

MESSAGE(STSTUS "###################################")



if(ANDROID)
  add_library(qHello SHARED
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
  )
else()
  add_executable(qHello
    main.cpp
    example/components/qtui/blank/mainwindow.cpp
#    example/components/qtui/blank/mainwindow.h
    example/components/qtui/blank/mainwindow.ui

#    example/components/qtui/player/mediaplayer.h
    example/components/qtui/player/mediaplayer.cpp
    example/components/qtui/player/mediaplayer.ui

#    example/components/qtui/player/myslider.h
    example/components/qtui/player/myslider.cpp
    
#    example/components/qtui/player/customslider.h
    example/components/qtui/player/customslider.cpp

    example/components/qtui/login/logindialog.cpp
#    example/components/qtui/login/logindialog.h
  )
endif()

target_link_libraries(qHello PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::MultimediaWidgets)

总结:

在系统安装有Qt6之后,CMake的项目配置文件中需要显式的添加MultimediaWidgets配置。

标签:QT5,CMAKE,Multimedia,qtui,set,components,CMake,MESSAGE,example
来源: https://blog.csdn.net/m0_51432135/article/details/120396208