How to resolve 'undefined references' in OpenGL C++ Qt5.5 - c++

I have downloaded Earth10 from GitHub and when i compile it in Qt5.3_MinGW with QGLWidget then it works but when i compile it in Qt5.5_MinGW with QOpenGLWidget then it pop up "undefined reference to `_imp__glPolygonMode#8'" etc errors. What can i change in source code to get rid of such errors?
Or if someone can point me to port from QGLWidget to QOpenGLWidget that would also be helpful.
One quick hack for removing all errors was:
CONFIG += c++11
LIBS += -lopengl32
LIBS += -lglu32
LIBS += -lglut32

Add opengl32.lib to the libraries linked by the linker stage.
Paraboloid87 showed the exact code which is required to add opengl32.lib:
Add LIBS += -lopengl32 to your *.pro file.

Thanks for the tip. I'm just "starting" my Qt OpenGL programming experience with QOpenGLWidgets and a call to change the points sizes : calling glPointSize(GLFloat size) caused an undefined reference.
By adding:
CONFIG += c++11
LIBS += -lopengl32
LIBS += -lglu32
LIBS += -lglut32
in the .pro file, the program went thru building and executed in debug mode...showing the vertex points + the colored triangle. Time for more learning....
Again, thanks for the tip.

Related

sf::Sound functions cause linker errors

I have started to use SFML recently. So I downloaded the 2.4.2 Win64 version of the library and started to play around the examples.
The only problem I have encountered so far is a linker error when I call play/pause/stop on a sf::Sound object. For example for play I get:
main.obj:-1: error: LNK2019: unresolved external symbol
"__declspec(dllimport) public: virtual void __cdecl
sf::Sound::play(void)" (__imp_?stop#Sound#sf##UEAAXXZ) referenced in
function main
Note that I am linking against sfml-audio.lib and can use other objects of the audio lib like AudioBuffers or even other functions of the sf::Sound like setBuffer which is wierd to me.
I am using Qt Creator as IDE so I link in the .pro file like this:
LIBS += -LC:/SFML/lib
CONFIG(release, debug|release): LIBS += -lsfml-audio -lsfml-graphics -lsfml-main -lsfml-network -lsfml-window -lsfml-system
CONFIG(debug, debug|release): LIBS += -lsfml-audio-d -lsfml-graphics-d -lsfml-main-d -lsfml-network-d -lsfml-window-d -lsfml-system-d
INCLUDEPATH += C:/SFML/include
DEPENDPATH += C:/SFML/include
Does anyone have a clue on whats happening?
Thanks in advance.
Well, finally I solved it just downloading the SFML Master and building the library with CMake + Visual Studio.
The libs/dlls generated that way work flawlessly.

Linux QT Creator C++: How to link with $$HOME?

I start a programm which includes a library (IDA) in
/home/MYUSERNAME/EB/IDA/Earlybite/
The library IDA has two folders:
/home/MYUSERNAME/EB/IDA/IDA/Includes/ (for h-file)
/home/MYUSERNAME/EB/IDA/IDA/Libs/ (for so-files)
This is the linking which works:
LIBS += -L$$PWD/../IDA/Libs/ -Wl,-rpath=$$PWD/../IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$PWD/../IDA/Includes/
The problem is that PWD only shows the path in which Earlybite starts. In this case
/home/MYUSERNAME/EB/IDA/Earlybite/, but if the programm starts e.g. in /home/MYUSERNAME/EB/IDA/ ...the linking will not work.
So I tried to link with the HOME environment variable. E.g.
LIBS += -L$$HOME/EB/IDA/IDA/Libs/ -Wl,-rpath=$$HOME/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$HOME/EB/IDA/IDA/Includes/
But this do not work.
I also tried
LIBS += -L/home/$$USER/EB/IDA/IDA/Libs/ -Wl,-rpath=/home/$$USER/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += /home/$$USER/EB/IDA/IDA/Includes/
But this do not work, too.
(I've also tried every try with a single $ and with two $ symbols...)
Edit: I just remembered that you can use $$(HOME) which will read environment variable during qmake execution, so you just need to add () around HOME. Using $_PRO_FILE_PWD_ is still a good practice, but the last options is a workaround rather than straightforward solution I think.
You can try and use $$_PRO_FILE_PWD_, this variable points to .pro file location, and create path relative to project file. Also check qmake Variables for additional references.
Or you can do:
HOME = $$system(echo $HOME)
message($$HOME)
LIBS += -L$$HOME ...
About $$system link

shared library in Qt Creator with mingw gives undefined reference error

Resolved. When I add my library as a normal internal library everything is fine. What I had failed to do was use the MY_LIBRARY_EXPORT macro on some free functions defined in a namespace. Somehow I had manipulated the library import in such a way to get those functions working, but it broke in other ways as described here.
I have not been able to use a shared library using the Qt Creator wizard. I have tried with dynamic linkage, and have failed, and cannot use as a static lib either. In both cases I get an 'undefined reference to `IMP_*' error.
Here are my pro files. They are both in the same SUB_DIR parent project and the paths are correct.
Relevant parts of the library pro file:
TARGET = Prospec
TEMPLATE = lib
CONFIG += staticlib
QMAKE_CXXFLAGS += -std=c++11
DEFINES += PROSPEC_LIBRARY
The entirety of the user .pro file. (ProspecTest is a unit test project for Prospec).
#-------------------------------------------------
#
# Project created by QtCreator 2013-12-12T15:04:30
#
#-------------------------------------------------
QT += testlib
QT -= gui
TARGET = prospectest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++11
SOURCES += prospectest.cpp \
mltest.cpp \
convertertest.cpp \
numericitemtest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
HEADERS += \
utilities.h \
mltest.h \
convertertest.h \
numericitemtest.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../Prospec/release/ -lProspec
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../Prospec/debug/ -lProspec
INCLUDEPATH += $$PWD/../Prospec/debug
DEPENDPATH += $$PWD/../Prospec/debug
win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../Prospec/release/libProspec.a
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../Prospec/debug/libProspec.a
# Boost
INCLUDEPATH += C:/boost/boost_1_55_0/
LIBS += "-LC:/boost/boost_1_55_0/stage/lib/"
EDIT: the undefined reference errors happen only where I invoke functionality in a source file. For instance, if I construct a library object I get the error, but if I move that objects constructor into a header file, then compile is OK. Right now I'm not sure if the problem is due to the Qt environment, or me not understanding shared libraries in general.
The error you describe points to a linker error. It happens when a necessary object/lib file is not specified. The most likely scenario is this: you include a header and use a function from that header, but don't specify in which library that function is defined.
Small example:
//test.c
#include <winsock.h>
int main() { gethostbyname("localhost"); }
'undefined reference' linker error:
>gcc test.c
cczICEqq.o:test.c:(.text+0x1e): undefined refer ence to `gethostbyname#4'
collect2.exe: error: ld returned 1 exit status
In this case you would have to specify libwsock32.a like this:
gcc test.c -lwsock32
Also, note that a libsomething.a is not the only place where a reference can be resolved. You might have missed an object file.
Unfortunately without more details I am not able to tell you the exact problem.
You probably forgot to export your symbols, as described here on MSDN and in various questions on this website. It's a pain in general, and there are a few ways to handle this, none much better than the other.
This makes it so that the DLL and import library actually contain a reference to the symbols you compiled into it. You can compare it partially to GCC's symbol visibility, although that only works in Linux, and GCC also supports the stuff described on MSDN for Windows.
Had the same problem just now. Build was working fine on macOS/OSX but not on Windows with the same codebase. Thanks to rubenvb, who pointed me in the right direction. Here the full solution, how to do it for Qt:
Use Q_DECL_EXPORT on functions when building the lib. And use Q_DECL_IMPORT when using the functions from the lib.
Example how to do this here: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application#Creating_a_shared_library
Solved the problem in my case, hope it helps!

Linking freeglut with Qt Creator in Linux

I currently run Arch Linux on my laptop and was hoping to know why Qt Creator isn't finding my glut library (which exists on my system).
My setup qmake file looks as follows:
...
/*sources and headers above*/
QT += opengl
LIBS += -lfreeglut
INCLUDEPATH += -L/usr/lib/
And when I run a locate glut, I get the following:
/usr/include/kwinglutils.h
/usr/include/kwinglutils_funcs.h
/usr/include/GL/freeglut.h
/usr/include/GL/freeglut_ext.h
/usr/include/GL/freeglut_std.h
/usr/include/GL/glut.h
/usr/lib/libglut.a
/usr/lib/libglut.so
/usr/lib/libglut.so.3
/usr/lib/libglut.so.3.9.0
/usr/share/avogadro/fragments/amino_acids/D-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/D-glutamine.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamine.cml
/usr/share/licenses/freeglut
/usr/share/licenses/freeglut/LICENSE
/var/lib/pacman/local/freeglut-2.6.0-1
/var/lib/pacman/local/freeglut-2.6.0-1/changelog
/var/lib/pacman/local/freeglut-2.6.0-1/desc
/var/lib/pacman/local/freeglut-2.6.0-1/files
Note that I have tried -lglut32 in my qmake file as well.
What could I be missing here?
When you specify -lfoobar in your .pro file (or with gcc in general), you're directing the compiler to search for the library libfoobar.a. Judging from your locate output it looks like you want:
LIBS += -lglut
Oh, silly me. I just realized that the correct lib to add was just -lglut and not -lglut32. This is because there exists libglut.so, and not libglut32.so.

QMake and wxWidgets (External Libraries)

I'm trying to compile a GUI program based on the wxWidgets libraries. I get a lot of undefined references to "something". I tried to add a few libraries manually on the LIBS variable of QMake without success. How can i add all the wxWidgets libraries to QMake without hard coding each library? Below is my .pro file.
# simple.pro
TARGET = sample
HEADERS += main.h simple.h
SOURCES += main.cpp simple.cpp
LIBS += -LC:/SourceCode/Libraries/wxWidgets2.8/lib/gcc_dll/wxmsw28_core_gcc.dll \
-LC:/SourceCode/Libraries/wxWidgets2.8/lib/gcc_dll/wxmsw28_gcc.dll \
-LC:/SourceCode/Libraries/wxWidgets2.8/lib/gcc_dll/wxmsw28_aui_gcc.dll
INCLUDEPATH += C:/SourceCode/Libraries/wxWidgets2.8/include
CONFIG += release
The errors are of the form:
release/simple.o:simple.cpp:(.rdata$_ZTV6Simple[vtable for
Simple]+0x320): undefined reference to
`wxFrameBase::SetStatusBar(wxStatusBar*)'
First, you need to use the .a files to add to the linker.
Then you need to define WXUSINGDLL if you link against the shared libraries.
Additionally, you forgot
wxbase29u.a
Hope that helps.