Home > Blockchain >  Combining Qt with OpenGL: does not compile
Combining Qt with OpenGL: does not compile

Time:03-20

I'm trying to create a project in Qt with an QOpenGLWidget and CMake as a building tool. The problem is that it does not compile and I don't know why.

[...]\include\ui_MainWindow.h:79: błąd: undefined reference to `__imp__ZN13QOpenGLWidgetC1EP7QWidget6QFlagsIN2Qt10WindowTypeEE'

That's my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.5)

project(QtTest VERSION 0.1 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)

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

set(PROJECT_SOURCES
        src/main.cpp
        src/MainWindow.cpp
        src/MainWindow.hpp
        src/MainWindow.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(QtTest
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
else()
    add_executable(QtTest
        ${PROJECT_SOURCES}
    )
endif()

target_link_libraries(QtTest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::OpenGL)

set_target_properties(QtTest PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

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

In my MainWindow.ui file I only create an QOpenGLWidget widget.

CodePudding user response:

In Qt6 QOpenGLWidget was moved to a new module named OpenGLWidgets. To make your program work you need to add OpenGLWidgets to your find_package command and Qt6::OpenGLWidgets to your target_link_libraries command.

find_package(Qt6 COMPONENTS Widgets OpenGL OpenGLWidgets REQUIRED)
target_link_libraries(QtTest PRIVATE Qt6::Widgets Qt6::OpenGL Qt6::OpenGLWidgets)
  • Related