Trying to play sound, via QSound (QT 5, Ubuntu 13.04)
The code:
#include <QtMultimedia/QSound>
void MainWindow::on_pushButton_2_clicked()
{
QSound::play("sounds/win.wav");
}
And getting 2 errors:
undefined reference to `QSound::play(QString const&)'
collect2: error: ld returned 1 exit status
What am i doing wrong ? In documentation here i see the same code.
You need to include the multimedia module. Add this to your .pro file:
QT += multimedia
Related
I am getting several undefined reference errors with using Qt. I am using MinGW as my compiler and I am using Qt version 5.15.2.
This is my CMake file:
cmake_minimum_required(VERSION 3.17)
project(jerseytraffic)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
set(CMAKE_PREFIX_PATH "A:\\CppProjects\\CppLibraries\\Qt5\\5.15.2\\mingw81_64\\lib\\cmake\\")
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
This is main.cpp:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << "Hello World";
return QCoreApplication::exec();
}
These are the errors I'm getting:
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `main':
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZNK14QMessageLogger5debugEv'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN7QString8fromUtf8EPKci':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qstring.h:706: undefined reference to `_imp___ZN7QString15fromUtf8_helperEPKci'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebug10maybeSpaceEv':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:129: undefined reference to `_imp___ZN11QTextStreamlsEc'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebuglsEPKc':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:159: undefined reference to `_imp___ZN11QTextStreamlsERK7QString'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN15QTypedArrayDataItE10deallocateEP10QArrayData':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qarraydata.h:239: undefined reference to `_imp___ZN10QArrayData10deallocateEPS_jj'
This is what I get in the CMake output when it reloads:
"A:\ProgramFiles\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" A:\CppProjects\jerseytraffic
-- Configuring done
-- Generating done
-- Build files have been written to: A:/CppProjects/jerseytraffic/cmake-build-debug
[Finished]
One thing I have tried is modifying the cmake file by adding this around the add_executable line, but it did not make any changes:
find_package(Qt5 COMPONENTS Core REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} Qt5::Core)
Some other things that I have attempted include:
set(CMAKE_BUILD_TYPE "Debug -G \"MinGW Makefiles\"")
set(CMAKE_CXX_COMPILER "C:\\MinGW\\bin\\gcc.exe")
option(G "MinGW Makefiles")
I added this CMake option in Settings > Build, Execution, Deployment > CMake > Debug > CMake options: -G "MinGW Makefiles" %*
However, none of these seemed to fix the error.
I am trying to use Taglib with Qt5.7. But my programm crashes:
The GDB process terminated unexpectedly (exit code 0).
Cannot continue debugged process: The program is not being run.
During startup program exited with code 0xc0000135
There did not seem to be any error messages while building Taglib.
*.pro file
QT += core
QT -= gui
CONFIG += c++11
TARGET = untitled2
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
TAGLIBDIR = $$quote(D:/D/Programmieren/Libraries/taglib)
INCLUDEPATH += $$quote( $${TAGLIBDIR}/include )
LIBS += -L$$quote($${TAGLIBDIR}/lib) -ltag
main.cpp
#include <QCoreApplication>
#include <taglib/tag.h>
#include <taglib/fileref.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TagLib::FileRef ref("D:/D/Media/Musik/Disturbed/The Sickness/Disturbed - Stupify.mp3");
return a.exec();
}
Edit:
I created Taglib-buildfiles using CMake-GUI and native MinGW-Installation since I did not manage to create them using Qt. My OS is Windows 10 x64 Education and I use Qt-Creator. I downloaded the latest Taglib-Release (1.11.1)
Why I cant create buildfiles using CMake:
First some *dll-files where missing, i coud solve that by just copying the ones from native MinGW-Installation to QT-MinGW.
Second I could not find any of the *exe-files desired by CMake so i could not change the path from native MinGW-Installation.
After rebuilding Taglib in debugmode I get this errors:
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib8FileNameC1EPKc'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefC1ENS_8FileNameEbNS_15AudioProperties9ReadStyleE'
main.cpp:11: error: undefined reference to `_imp___ZNK6TagLib7FileRef3tagEv'
main.cpp:11: error: undefined reference to `_imp___ZNK6TagLib6String9toCStringEb'
main.cpp:11: error: undefined reference to `_imp___ZN6TagLib6StringD1Ev'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
main.cpp:11: error: undefined reference to `_imp___ZN6TagLib6StringD1Ev'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
collect2.exe:-1: error: error: ld returned 1 exit status
Solved it by using new version (5.8 instead of 5.7) of Qt.
I'm trying to use the library gdcm in my project. However I'm with some problem.
gdcmGlobal.cxx:-1: error: undefined reference to gdcm::System::FileIsDirectory(char const*)'
gdcmGlobal.cxx:-1: error: undefined reference togdcm::System::FileIsDirectory(char const*)'
gdcmGlobal.cxx:-1: error: undefined reference to gdcm::System::FileExists(char const*)'
:-1: error: C:\Program_Library\gdcm-2.0.18-mingw32\bin/libgdcmDICT.a(gdcmGlobal.cxx.obj): bad reloc address 0xf in section.text$ZNK4gdcm7Modules7IsEmptyEv[_ZNK4gdcm7Modules7IsEmptyEv]'
I included the libs:
LIBS += -L"C:\Program_Library\gdcm-2.0.18-mingw32\bin" \
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmMSFF.a" \
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmCommon.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmDICT.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmDSED.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmexpat.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmIOD.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmjpeg8.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmjpeg12.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmjpeg16.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmopenjpeg.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmzlib.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmcharls.a"\
-L"C:\Program_Library\gdcm-2.0.18-mingw32\bin\libgdcmgetopt.a"
LIBS += -lgdcmMSFF -lgdcmCommon -lgdcmDICT -lgdcmDSED -lgdcmexpat
-lgdcmIOD -lgdcmjpeg8 -lgdcmjpeg12 -lgdcmjpeg16 -lgdcmopenjpeg -lgdcmzlib -lgdcmcharls -lgdcmgetopt
Anybody would know what is happening?
Thanks!
Qt 5.1 uses g++ 4.8 and I used g++ 4.6 to compile gdcm! That is the problem! However when I included:
include < gdcmGlobal.h>
appear this error:
gdcmGlobal.cxx:-1: error: undefined reference to
`gdcm::System::FileIsDirectory(char const*)'
but when I remove: include < gdcmGlobal.h>, compiles!
I am trying to get OpenCv2.4.6 to work with QT5.1.0
I've followed this Guide so far Qt creator 5.0.1 with OpenCv 2.3.4 on windows
Unfortunatly after finishing trying out the sample Application(Display an Image using OpenCv in the QtCreator I get these Errors:
[Project-Path]\main.o:-1: In function `main':
[Project-Path]\main.cpp:8: error: undefined reference to `cv::imread(std::string const&, int)'
[Project-Path]\main.cpp:9: error: undefined reference to `cv::namedWindow(std::string const&, int)'
[Project-Path]\main.cpp:10: error: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
[Project-Path]\main.cpp:10: error: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
[Project-Path]\main.cpp:11: error: undefined reference to `cv::waitKey(int)'
[Project Build Path Desktop Debug]\debug\main.o:-1: In function `ZN2cv3MatD1Ev':
[OpenCV/Install/include]\opencv2\core\mat.hpp:278: error: undefined reference to `cv::fastFree(void*)'
[Project Build Path Desktop Debug]\debug\main.o:-1: In function `ZN2cv3Mat7releaseEv':
[OpenCV/Install/include]\opencv2\core\mat.hpp:367: error: undefined reference to `cv::Mat::deallocate()'
collect2.exe:-1: error: error: ld returned 1 exit status
the "[]" Paths are my formatting and supposed to help keep track .
main.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main() {
// read an image
Mat image = imread("img.jpg");
namedWindow("My Image");
imshow("My Image", image);
waitKey(5000);
return 1;
}
myFirstOpenCVProject.pro
QT += core
QT -= gui
TARGET = myFirstOpenCVProject
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:/qt/OpenCV246_bin/install/include
LIBS += -LC:/qt/OpenCV246_bin/install/lib/lopencv_core246.dll
LIBS += -LC:/qt/OpenCV246_bin/install/lib/lopencv_highgui246.dll
LIBS += -LC:/qt/OpenCV246_bin/install/lib/lopencv_imgproc246.dll
LIBS += -LC:/qt/OpenCV246_bin/install/lib/lopencv_features2d246.dll
LIBS += -LC:/qt/OpenCV246_bin/install/lib/lopencv_calib3d246.dll
Can you help me resolve this?
I can't find any solutions that apply to my case ...
-L is to add directories to the search path. The actual libs are added via -l. The correct line would be:
LIBS += -LC:/qt/OpenCV246_bin/install/lib -llopencv_core246 -llopencv_highgui246 ...
I think you are missing the static libs of opencv:
opencv_core246.lib
opencv_highgui246.lib
opencv_video246.lib
opencv_ml26d.lib
opencv_legacy246.lib
opencv_imgproc246.lib
opencv_whatever.lib
Don t know the pro commands very well. Maybe something like:
LIBS += -Lc:/blabla/opencv_imgproc246.lib
I generated moc files for QT5 using
set (CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
Then I add the moc files into SRC using
SET(SRC
src/main.cpp
src/video_widget_surface.cpp
src/video_widget.cpp
src/video_player.cpp
#moc files
moc/moc_video_player.cpp
moc/moc_video_widget.cpp
moc/moc_video_widget_surface.cpp
Finally I add the executable using
add_executable(somegui ${SRC})
But I get errors in moc files saying :
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:54:6: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:62:19: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:68:20: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:68:46: error: non-member function 'const QMetaObject* metaObject()' cannot have cv-qualifier
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h: In function 'const QMetaObject* metaObject()':
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:21: error: within this context
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: invalid use of non-static data member 'QObject::d_ptr'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:21: error: from this location
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:50: error: within this context
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: invalid use of non-static data member 'QObject::d_ptr'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:50: error: from this location
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: At global scope:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:73:7: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'void* qt_metacast(const char*)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected type-specifier before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected '>' before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected '(' before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: 'VideoWidget' was not declared in this scope
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:59: error: expected primary-expression before '>' token
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:61: error: invalid use of 'this' in non-member function
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:67: error: expected ')' before ';' token
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:78:40: error: cannot call member function 'virtual void* QWidget::qt_metacast(const char*)' without object
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: At global scope:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:81:5: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'int qt_metacall(QMetaObject::Call, int, void**)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:83:43: error: cannot call member function 'virtual int QWidget::qt_metacall(QMetaObject::Call, int, void**)' without object
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'void* qt_metacast(const char*)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'const QMetaObject* metaObject()':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
make[2]: *** [CMakeFiles/somestuff.dir/moc/moc_video_widget.cpp.o] Error 1
make[1]: *** [CMakeFiles/somestuff.dir/all] Error 2
make: *** [all] Error 2
My understanding is that there is some error in the moc files that are created. But I don't have any control over how that is created. Now how do I solve this bug ?
CMake documentation is not that bad, do not neglect reading it. You misunderstood the concept of AUTOMOC:
AUTOMOC is a boolean specifying whether CMake will handle the Qt moc preprocessor automatically, i.e. without having to use the QT4_WRAP_CPP() macro. Currently Qt4 is supported. When this property is set to TRUE, CMake will scan the source files at build time and invoke moc accordingly. If an #include statement like #include "moc_foo.cpp" is found, the Q_OBJECT class declaration is expected in the header, and moc is run on the header file. If an #include statement like #include "foo.moc" is found, then a Q_OBJECT is expected in the current source file and moc is run on the file itself. Additionally, all header files are parsed for Q_OBJECT macros, and if found, moc is also executed on those files.
So, first of all, you should not add generated moc files explicitly to sources and push them into executable compilation. In other words, you only push your sources:
SET(SRC
src/main.cpp
src/video_widget_surface.cpp
src/video_widget.cpp
src/video_player.cpp)
and moc ones are handled automatically by CMake.
Secondly, as stated in the documentation:
If Q_OBJECT is in the foo.h (i.e. QObject is declared in the header file),
then in the corresponding foo.cpp
don't forget to add #include "moc_foo.cpp", preferably at the end
of the file;
If Q_OBJECT is in the foo.cpp (i.e. QObject is declared in the source file),
then, again, in the foo.cpp itself
don't forget to add #include "foo.moc", preferably at the end
of the file.