Undefined reference to `QMediaPlayer::QMediaPlayer(QObject*)' linker error when building under Qt 6.4
-
Hi.
I'm creating a project with a class, which inherits from the Qt's QMediaPlayer class, in order to be used as a QML element.
Here's the code.
#pragma once /// /// ::: SamplePlayer | Interface ::: /// #include <QtMultimedia/QMediaPlayer> #include <QtQml> class SamplePlayer : public QMediaPlayer { Q_OBJECT QML_ELEMENT public: SamplePlayer(QObject* parent = nullptr); ~SamplePlayer( ); };
/// /// ::: SamplePlayer | Implementation ::: /// #include "sample_player.hpp" SamplePlayer::SamplePlayer(QObject* parent) : QMediaPlayer(parent) { } SamplePlayer::~SamplePlayer( ) { }
I can successfully build the project under Qt 6.7.0, but under Qt 6.4.2 I get the following linker error.
8.501 [36/36] Linking CXX executable /build/bin/sample 8.501 FAILED: /build/bin/sample 8.501 : && /usr/bin/g++ CMakeFiles/sample.dir/sample_autogen/mocs_compilation.cpp.o CMakeFiles/sample.dir/main.cpp.o CMakeFiles/sample.dir/sample_qmltyperegistrations.cpp.o CMakeFiles/sample.dir/.rcc/qrc_qmake_Sample.cpp.o CMakeFiles/sample.dir/.rcc/qmlcache/sample_qmlcache_loader.cpp.o CMakeFiles/sample.dir/.rcc/qmlcache/sample_Main_qml.cpp.o CMakeFiles/sample.dir/.rcc/qrc_sample_raw_qml_0.cpp.o -o /build/bin/sample CMakeFiles/sample_player_plugin_init.dir/./sample_player_plugin_init.cpp.o CMakeFiles/sample_player_resources_1.dir/./.rcc/qrc_qmake_SamplePlayer.cpp.o SamplePlayer/libsample_player_plugin.a /usr/lib/x86_64-linux-gnu/libQt6Multimedia.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Gui.so.6.4.2 /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libOpenGL.so libsample_player.a /usr/lib/x86_64-linux-gnu/libQt6Qml.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Network.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Core.so.6.4.2 && : 8.501 /usr/bin/ld: libsample_player.a(sample_player.cpp.o): in function `SamplePlayer::SamplePlayer(QObject*)': 8.501 sample_player.cpp:(.text+0xdb): undefined reference to `QMediaPlayer::QMediaPlayer(QObject*)' 8.501 /usr/bin/ld: libsample_player.a(sample_player.cpp.o): in function `SamplePlayer::~SamplePlayer()': 8.501 sample_player.cpp:(.text+0x11a): undefined reference to `QMediaPlayer::~QMediaPlayer()' 8.501 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o): in function `SamplePlayer::qt_metacast(char const*)': 8.501 mocs_compilation.cpp:(.text+0x167): undefined reference to `QMediaPlayer::qt_metacast(char const*)' 8.501 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o): in function `SamplePlayer::qt_metacall(QMetaObject::Call, int, void**)': 8.501 mocs_compilation.cpp:(.text+0x196): undefined reference to `QMediaPlayer::qt_metacall(QMetaObject::Call, int, void**)' 8.501 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o):(.data.rel.ro+0x0): undefined reference to `QMediaPlayer::staticMetaObject' 8.501 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o):(.data.rel.ro._ZTI12SamplePlayer[_ZTI12SamplePlayer]+0x10): undefined reference to `typeinfo for QMediaPlayer' 8.501 collect2: error: ld returned 1 exit status 8.501 ninja: build stopped: subcommand failed. ERROR: process "/bin/sh -c cmake --build build" did not complete successfully: exit code: 1
What is the reason of this error? How can it be fixed? How to build the project under Qt 6.4.2?
Thanks.
Building environments
Arch: CMake 3.29.3, Ninja 1.12.1, GCC 14.1.1, Qt 6.7.0 — the build is successful. The program works without any issues.
Debian 12.5: CMake 3.25, Ninja 1.11.1, GCC 12.2.0, Qt 6.4.2 — the build fails with the aforementioned error.
Here's the link to the sample project.
Clone the repository. First create a Docker image with the Arch environment using the command
./build -f Dockerfile.arch -t arch .
. Run the image in the container with the command./run -r sample_media_player -t arch
. Make sure that the player starts and shows the cartoon about the rabbit.Second try to create a Docker image with the Debian environment using the command
./build -f Dockerfile.debian -t debian .
. Observe the error that I described above.Please note, that the container will only work in the Linux OS with the X Window system environment.
-
So do you actually link against the Qt multimedia library?
-
Linking CXX executable /build/bin/sample
I don't see an executable named 'sample' in your CMakeLists.txt you linked to.
-
That was the old executable name, sorry.
Here's the correct error output.
8.450 [36/36] Linking CXX executable /build/bin/sample_media_player 8.450 FAILED: /build/bin/sample_media_player 8.450 : && /usr/bin/g++ CMakeFiles/sample_media_player.dir/sample_media_player_autogen/mocs_compilation.cpp.o CMakeFiles/sample_media_player.dir/main.cpp.o CMakeFiles/sample_media_player.dir/sample_media_player_qmltyperegistrations.cpp.o CMakeFiles/sample_media_player.dir/.rcc/qrc_qmake_Components.cpp.o CMakeFiles/sample_media_player.dir/.rcc/qmlcache/sample_media_player_qmlcache_loader.cpp.o CMakeFiles/sample_media_player.dir/.rcc/qmlcache/sample_media_player_Main_qml.cpp.o CMakeFiles/sample_media_player.dir/.rcc/qrc_sample_media_player_raw_qml_0.cpp.o -o /build/bin/sample_media_player CMakeFiles/sample_player_plugin_init.dir/./sample_player_plugin_init.cpp.o CMakeFiles/sample_player_resources_1.dir/./.rcc/qrc_qmake_SamplePlayer.cpp.o SamplePlayer/libsample_player_plugin.a /usr/lib/x86_64-linux-gnu/libQt6Multimedia.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Gui.so.6.4.2 /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libOpenGL.so libsample_player.a /usr/lib/x86_64-linux-gnu/libQt6Qml.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Network.so.6.4.2 /usr/lib/x86_64-linux-gnu/libQt6Core.so.6.4.2 && : 8.450 /usr/bin/ld: libsample_player.a(sample_player.cpp.o): in function `SamplePlayer::SamplePlayer(QObject*)': 8.450 sample_player.cpp:(.text+0xdb): undefined reference to `QMediaPlayer::QMediaPlayer(QObject*)' 8.450 /usr/bin/ld: libsample_player.a(sample_player.cpp.o): in function `SamplePlayer::~SamplePlayer()': 8.450 sample_player.cpp:(.text+0x11a): undefined reference to `QMediaPlayer::~QMediaPlayer()' 8.450 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o): in function `SamplePlayer::qt_metacast(char const*)': 8.450 mocs_compilation.cpp:(.text+0x167): undefined reference to `QMediaPlayer::qt_metacast(char const*)' 8.450 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o): in function `SamplePlayer::qt_metacall(QMetaObject::Call, int, void**)': 8.450 mocs_compilation.cpp:(.text+0x196): undefined reference to `QMediaPlayer::qt_metacall(QMetaObject::Call, int, void**)' 8.450 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o):(.data.rel.ro+0x0): undefined reference to `QMediaPlayer::staticMetaObject' 8.450 /usr/bin/ld: libsample_player.a(mocs_compilation.cpp.o):(.data.rel.ro._ZTI12SamplePlayer[_ZTI12SamplePlayer]+0x10): undefined reference to `typeinfo for QMediaPlayer' 8.450 collect2: error: ld returned 1 exit status 8.450 ninja: build stopped: subcommand failed.
-
sample_player.cpp is not built by sample_media_player executable but sample_player lib so this library should correctly link against Qt6::Multimedia. Otherwise the link order is wrong (and the whole dependency handling - when some lib is using this library then it should also link against it).
-
<sample_player> library should correctly link against Qt6::Multimedia
How that can be done in my CMakeLists.txt file?
-
@shamaoke said in Undefined reference to `QMediaPlayer::QMediaPlayer(QObject*)' linker error when building under Qt 6.4:
How that can be done in my CMakeLists.txt file?
The same way you did it with your executable by using target_link_libraries()... Why should this be different between an executable and a library?
-
-
You should link Qt6::Multimedia PUBLIC as the public header of your library is using this library (header) in a public header. Same goes for Qt6:Qml