Qt libao undefined reference - c++

I'm trying to get the libao library working in Qt. Here's what I have so far.
#include <ao/ao.h>
...
static int audio_driver;
static ao_device *audio_device;
static ao_sample_format audio_format;
...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ao_initialize();
audio_driver = ao_default_driver_id();
MainWindow w;
w.show();
return a.exec();
}
It says that every reference to anything in the ao library is an undefined reference.
error: undefined reference to `ao_initialize'
error: undefined reference to `ao_default_driver_id'
And so on all the way through the code.
For what it's worth, every function in ao/ao.h is in an extern "C".
Any idea what's causing this?
Many thanks.

You doesn't link against ao dynamic library.
If you use qmake add following lines in .pro file
LIBS += -lao
If library in non-standard location, add these lines too
INCLUDEPATH += path/to/headers
LIBPATH += path/to/library

If you're on Linux, or anywhere else where pkg-config is available, then the way this should be done is by adding "link_pkgconfig" to the CONFIG variable, and then add the package name to the PKGCONFIG variable. For example, if you're using libao and libvorbisfile:
CONFIG += link_pkgconfig
PKGCONFIG += ao vorbisfile
This will make sure that not only the correct link flags will be used, but also the correct CFLAGS/CXXFLAGS, which is also important.

Related

qt5 undefined reference to 'QApplication::QApplication(int&, char**, int)'

I'm trying to get a simple hello world example running, and already needed some time to figure out what includes to use
Now I verified the include paths, the QApplication should actually be there, but it throws the above error. For clarity my code:
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *button = new QPushButton("Hello world!");
button->show();
return app.exec();
}
I tried compiling using first qmake -project, then qmake and finally make and then got the following errors:
qt_hello_world.o: In function 'main':
undefined reference to QApplication::QApplication(int&, char**, int)
qt_hello_world.cpp: undefined reference to QPushButton::QPushButton(QString const&, QWidget*)
qt_hello_world.cpp: undefined reference to QWidget::show()
qt_hello_world.cpp: undefined reference to QApplication::exec()
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()
qt_hello_world.cpp: undefined reference to QApplication::~QApplication()
The Makefile created by qmake contains the correct include path to the qt5 directory which contains the QtWidgets/QApplication, the QApplication file just includes the qapplication.h header that contains the actual class QApplication.
The tutorial https://wiki.qt.io/Qt_for_Beginners is fully updated so you must modify it. Change to:
TEMPLATE = app
TARGET = callboot-ui.exe
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS +=
SOURCES += main.cpp
TL; DR;
In the case of #jww it has an error in the following line of the .pro:
greaterThan(QT_MAJOR_VERSION, 5): QT += core gui widgets
The error is caused because greaterThan(QT_MAJOR_VERSION, 5) verifies that the major version of Qt is greater than 5 to add sub-modules, but the latest version of Qt is 5.13.2 is not greater than 5, so it is not linked the modules causing the error shown.
In the tutorial greaterThan(QT_MAJOR_VERSION, 4): QT += widgets is used to support .pro so that it can be compiled for Qt4 and Qt5 since in the latter the widgets moved to a new sub-module called widgets.

How to include jxcore c++ library "jx.h" in qt applications?

This is the sample code I am trying execute in Qt Creator. I have included all the JX libraries in Qt. The JX c++ code works outside Qt well but I want to make it work inside Qt. Please review the code and suggest me a way to achieve this.
Currently I am getting errors such as "undefined reference to `v8::Locker::Locker(v8::Isolate*)'" and 1000 similar errors. I gues its because qt is not recognizing the JX libraries.
Thanks in advance.
#include "mainwindow.h"
#include <QApplication>
#include "jx.h"
#if defined(_MSC_VER)
// Sleep time for Windows is 1 ms while it's 1 ns for POSIX
// Beware using this for your app. This is just to give a
// basic idea on usage
#include <windows.h>
#else
#include <unistd.h>
#define Sleep(x) usleep(x)
#endif
void callback(JXValue *results, int argc) {
// do nothing
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
const char *contents =
"process.requireGlobal = require;";
JX_Initialize(argv[0], callback);
JX_InitializeNewEngine();
JX_DefineMainFile(contents);
JX_StartEngine();
JXValue ret_val;
JX_Evaluate("process.requireGlobal('./dummy.js').data",
"eval",&ret_val);
while (JX_LoopOnce() != 0) usleep(1);
JX_Free(&ret_val);
JX_StopEngine();
return a.exec();
}
Yes, the 'undefined reference to' is a linker error.
This is due to the fact that the linker can't find the library (libraries) in the paths it already has, so you need to specified it (them) and you've to do it in the .pro file of your Qt project.
.pro file works similarly to makefile: here you have to specify "extra" paths where source and header files are located, as well as libraries.
For your specific question, you need to add all the library requesting to make jxcore working, adding them to the keyword "LIBS": -L introduce path(s) at which libraries are stored, while -l introduce libraries themselves you want to use. I.e.
LIBS += -L/path/to/your/libraries \
-lname_of_library_without_lib
You can find more detailed description at this link http://doc.qt.io/qt-4.8/qmake-project-files.html
Go directly to last section if you're just interested in how add libraries
Hope it helps

Cmake link issue: undefined reference to QPushButton

I just started using Qt. I have a problem with compiling the first example.
main.cpp:
#include <QCoreApplication>
#include <QPushButton>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QPushButton button ("Hello world !");
return app.exec();
}
CMake.txt:
cmake_minimum_required(VERSION 2.6)
project(new)
find_package(Qt4 REQUIRED)
enable_testing()
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
set(source_SRCS main.cpp)
qt4_automoc(${source_SRCS})
add_executable(new ${source_SRCS})
target_link_libraries(new${QT_QTCORE_LIBRARY})
add_subdirectory(tests)
install(TARGETS new RUNTIME DESTINATION .)
The error I get upon building is:
undefined reference to `QPushButton::QPushButton(QString const&,QWidget*)'
It is a linking problem, but how can I solve it?
Here is what I think you are missing:
find_package(Qt4 REQUIRED QtGui)
looking at your cmake you probably want to change the target_link_libraries for the following:
target_link_libraries(new ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
You have three problems:
You're not linking with the Gui module (Widgets module in Qt 5). This is covered in the other answer.
You you must use QApplication in widgets-based applications. Since QPushButton comes from the Gui module (Widgets in Qt5), you can't merely use QCoreApplication nor QGuiApplication: your program will crash as soon as you attempt to instantiate a QWidget.
You're not showing the button, so when your program starts you'll see nothing once you fix the above.
Your main.cpp should look like:
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
You're also going to need to link against the QtGui and QtWidgets library. Within qmake it handles which of the many libraries make up Qt, you'll have to do this by hand in cmake.
If you look at the documentation for QPushButton (http://doc.qt.io/qt-5/qpushbutton.html), the "qmake" line shows what library you need.
Consider using qmake instead of cmake with Qt.
Anyway, QCoreApplication (see docs) is the console version of main application class and will not work in GUI application. QPushButton is a widget class and can exist alone and will make a window (although you must show() it explicitely for that) but only with QApplication.
When using qmake in your *.pro file you need to include widgets like so:
CONFIG += widgets
and make sure you don't have
CONFIG -= gui
If you insist on using cmake then see here.
this answer solves my same problem :)
ok,
i had can solve it by myself.
For all they have they problem too:
The error is sourced from the use of "Q_OBJECT".
To solve the error, right-cklick on the Project and choose "Run qmake" and after >this: "Rebuild".
Then the error should be disappeared ;-)
-casisto
https://forum.qt.io/topic/52439/get-undefined-reference-error-but-don-t-know-why/2

Qt creator - `multiple definition of qMain` error

I started to work for my homework using QT creator to make the GUI, but I gt this error and I can't manage to find the reason for it, nor can I understand what it means. I suppose it sees my main function twice but I do not know why... please assist me in fixing this error:
error:
Makefile.Debug:155: warning: overriding commands for target `debug/main.o'
Makefile.Debug:142: warning: ignoring old commands for target `debug/main.o'
debug/main.o: In function `Z5qMainiPPc':
D:\c++\Labs\GUI_r/../../../info/qt/Desktop/Qt/4.8.1/mingw/include/QtGui/qwidget.h:494: multiple definition of `qMain(int, char**)'
debug/main.o:D:\c++\Labs\GUI_r/main.cpp:7: first defined here
collect2: ld returned 1 exit status
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "controller.h"
#include "StudentRepository.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StudentRepository *stre = new StudentRepository();
Controller *c = new Controller(stre);
MainWindow w(c);
w.show();
return a.exec();
}
edit: long code removed - not the reason for the error. Check the answere it is useful.
The reason for that linking error is because of awkawrd behaivior behalf QT creator. I had in the projectName.pro -
QT += core gui
TARGET = GUI_r
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
main.cpp \ /////// Double call of main.cpp
StudentRepository.cpp \
controller.cpp
HEADERS += mainwindow.h \
controller.h \
StudentRepository.h \
Student.h \
ui_mainwindow.h \ /////Double call of ui_mainwindow.h
ui_mainwindow.h
FORMS += mainwindow.ui
Thank you, i hope this post will be usefull to other new users of QTcreator.
Maybe your project contains another source file with a main. Somewhere files duplicated. Check "SOURCES =" and main.cpp in your .pro file.
You can only have one QApplication per program!
Review your classes (Controller, StudentRepository, MainWindow) and make sure that they are not declaring QApplication as well.
It does see two definitions of qMain , not your main.
You have probably taken a sample program and modified it by adding your code. Recreate those steps and see when it stopped working.
When writing a code, do a compilation as often as possible, to find such errors right after you've introduced them.

How to use the TBB in Qt Creator

I am trying to use the TBB in the Qt Creator. I am using the Qt 4.7 and TBB 3.0, below is my setting in .pro.
INCLUDEPATH += C:\tbb30\include
LIBS += C:\tbb30\lib\ia32\vc10\*.lib
My sample code is very simple:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
tbb::concurrent_vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::cout<<v.size();
return a.exec();
}
But the program exit with the code -1073741515
Anyone use the TBB in the Qt Creator before?
Best Regards,
in LIBS, I don't think you can use joker characters (*, ?). You need to use the actual name of the lib. Note that you can specify a search path for libs, then the lib name in "short format". If you aim at cross-compilation, it can be useful.
Here is an example to link Open Ssl (might be useful for you)
LIBS += -L$${OPEN_SSL}/lib
LIBS += -llibeay32
LIBS += -lssleay32
Concerning your error code, what compiler are you using (mingwin or visual?). In any case, you can check the generated command to try to understand why it goes wrong (the linker command). Hope this helps.