Qt 5.3.1 - static building results in linker errors - c++

I just built the static libraries from Qt 5.3.1 for Windows x86 with MSVC 2013. The second step is obviously a static linked application that uses these libraries. So I created a small application (just a MainWindow with some controls, for testing purposes). This is my *.pro-file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = projectname
TEMPLATE = app
CONFIG += static
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS += -LC:\statics\lib\ -lQt5Core \
-LC:\statics\lib\ -lQt5Gui \
-LC:\statics\lib\ -lQt5Widgets \
-LC:\statics\lib\ -lqtmain \
-LC:\statics\lib\ -lQt5PlatformSupport \
INCLUDEPATH += C:\statics\include
Yeah. This is what I got from several tutorials/my memory from when I worked with 5.1. But when compiling I get those errors:
Qt5Core.lib(Qt5Core.dll):-1: error: LNK2005: "public: __thiscall QString::~QString(void)" (??1QString##QAE#XZ) allready defined in mainwindow.obj.
mainwindow.obj:-1: error: LNK2019: Unresolved extern symbol ""public: static struct QArrayData const * const QArrayData::shared_null" (?shared_null#QArrayData##2QBU1#B)" in Funktion ""public: __thiscall QString::QString(void)" (??0QString##QAE#XZ)".
moc_mainwindow.obj:-1: error: LNK2001: Unresolved extern symbol ""public: static struct QMetaObject const QMainWindow::staticMetaObject" (?staticMetaObject#QMainWindow##2UQMetaObject##B)".
Does somebody know how to handle these errors? Do I have to link against some other libraries not mentioned above, or include some files? I googled this topic and found out that many people experienced the same problem, but they mostly use older versions of Qt oder VS. I also tried to export the project to Visual Studio (working from Qt Creator), but the errors remain.
Thank you for your answers :)

QT += core gui
LIBS += -LC:\statics\lib\ -lQt5Core \
-LC:\statics\lib\ -lQt5Gui \
You are loading both Qt libraries twice: default and custom versions. By default, QT contains both core and gui libraries... you must delete them manually:
QT -= core gui

Just remove LIBS, INCLUDEPATH from project file and run qmake from static build on the project file to create makefile. If this will not help check your PATH

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.

How to link OpenGL to a QT application on Win32?

I have a sample QT application with the following raw OpenGL calls (not with QGLFunctions):
#define GL_GLEXT_PROTOTYPES
#include <QtOpenGL/QtOpenGL>
GLuint positionBuffer = 1;
glDeleteBuffers(1, &positionBuffer);
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
In Windows desktop configuration I get the following linker errors:
error LNK2019: unresolved external symbol _glDeleteBuffers#8 referenced in function "public: void __thi
scall OsgViewer::beforeRendering(void)" (?beforeRendering#OsgViewer##QAEXXZ) [D:\Repos\build\geoviewer\GeoViewer.vcxpro
j]
It is noteworthy that glClearColor and glClear are linked successfully. I tried both QMake and CMake with the same result.
My QMake file is:
QT += qml quick opengl
CONFIG += c++11
DEFINES += GL_GLEXT_PROTOTYPES
#see https://stackoverflow.com/questions/31688370/qt-5-5-with-qmake-linker-cannot-resolve-opengl-function-calls
#LIBS += -llibEGLd.lib -llibGLESv2d.lib
LIBS += opengl32.lib glu32.lib
VERSION = 1.0.11
DEFINES += VERSION_STRING=\\\"$$VERSION\\\"
SOURCES += main.cpp \
squircle.cpp
#Icon files are added in the same way as in quickcontrols2\gallery\ sample
RESOURCES += qml.qrc
HEADERS += \
squircle.h
It links fine (to ANGLE) with 'LIBS += -llibEGLd.lib -llibGLESv2d.lib' but with 'LIBS += opengl32.lib glu32.lib' it does not.
My CMake file contains:
find_package(OpenGL)
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})
it finds glu32 and opengl32 and corresponding DLLs exist in C:/Windows/System32, but I have an impression that opengl32.dll supports some old version of OpenGL, because it has glDeleteLists, but does not have glDeleteBuffers:
so if I have old OpenGL libraries, the question is where to get new libraries?
Qt5OpenGL.dll has QGLFunctions::glDeleteBuffers, but does not have raw glDeleteBuffers:
It is interesting that OpenGLES DLL has all the functions:

Unresolved symbols: Qt and OpenCV

I have been trying to use OpenCV in my Qt-based application. However, I am running into linking errors for some reason.
I am using Qt 5.5.0, MSVC 12.0, OpenCV 3.0, Windows 7. My OpenCV is unpacked in C:\opencv folder. I am not trying to compile the library myself, just using what came in the package. As shown in the code below, I am linking to libraries located in vc12 folder.
I have removed all my code and made it really basic for troubleshooting purposes, so I have a default starter QtWidget project (with an empty screen), to which I added a single line:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat M;
}
MainWindow::~MainWindow()
{
delete ui;
}
If I comment out the only openCV command (cv::Mat M;) the code compiles and runs. With this line present I am getting two unresolved symbol errors:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree#cv##YAXPEAX#Z) referenced in function "public: __cdecl cv::Mat::~Mat(void)" (??1Mat#cv##QEAA#XZ)
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl cv::Mat::deallocate(void)" (?deallocate#Mat#cv##QEAAXXZ) referenced in function "public: void __cdecl cv::Mat::release(void)" (?release#Mat#cv##QEAAXXZ)
I have tried various ways to statically link openCV libraries, but I keep getting the same errors (I always clean, re-run QMake, and then re-build the project for every attempt). Here is what my .pro file looks like right now (I ended up adding all static libs hoping that would help - it did not):
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x86/vc12/staticlib \
-lopencv_core300 \
-lopencv_highgui300 \
-lopencv_imgproc300 \
-lIlmImf \
-lippicvmt \
-llibjasper \
-llibjpeg \
-llibpng \
-llibtiff \
-llibwebp \
-lopencv_calib3d300 \
-lopencv_features2d300 \
-lopencv_flann300 \
-lopencv_hal300 \
-lopencv_imgcodecs300 \
-lopencv_ml300 \
-lopencv_objdetect300 \
-lopencv_photo300 \
-lopencv_shape300 \
-lopencv_stitching300 \
-lopencv_superres300 \
-lopencv_ts300 \
-lopencv_video300 \
-lopencv_videoio300 \
-lopencv_videostab300 \
-lzlib
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x86/vc12/staticlib \
-lopencv_core300d \
-lopencv_highgui300d \
-lopencv_imgproc300d \
-lIlmImfd \
-lippicvmt \
-llibjasperd \
-llibjpegd \
-llibpngd \
-llibtiffd \
-llibwebpd \
-lopencv_calib3d300d \
-lopencv_features2d300d \
-lopencv_flann300d \
-lopencv_hal300d \
-lopencv_imgcodecs300d \
-lopencv_ml300d \
-lopencv_objdetect300d \
-lopencv_photo300d \
-lopencv_shape300d \
-lopencv_stitching300d \
-lopencv_superres300d \
-lopencv_ts300d \
-lopencv_video300d \
-lopencv_videoio300d \
-lopencv_videostab300d \
-lzlibd
INCLUDEPATH += C:/opencv/build/include/
DEPENDPATH += C:/opencv/build/include/
Is there something in particular I am missing about the configuration? I am pretty sure the basic syntax (like slashes, etc) is ok since I was able to link another library in Qt Creator in another project, but I will be more than happy to try any suggestions at this point.
So after searching around and multiple attempts I found out what was wrong. I was linking to x86 library instead of x64. If I changed to x64 libs, I was getting mismatch between Static and Dynamic linker directives. Linking OpenCV statically would not work since OpenCV libraries themselves link to libcmt.lib, whereas Qt (pre-built) links dynamically to the counter-part of libcmt.lib, i.e. to msvcrt.lib. And since these two MS libraries are one and the same (just one is for static linking, the other for dynamic), I had two options: either rebuild Qt or OpenCV (which I don't want to do - I am very new to this kind of issues), or link OpenCV dynamically.
I opted for the second option. All you have to do is to remove all references to /staticlib folder and libs that it contains, and replace it with the following:
win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x64/vc12/lib -lopencv_world300
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x64/vc12/lib -lopencv_world300d
Note that one has to distribute corresponding DLLs with the application (located in build\x64\vc12\bin\ folder).

Using RegOpenKey and RegEnumKey in dynamic QT with MSVC2010

I want to use 2 winapi functions for registry access, as stated.
I have QT 5.3.2 with QT Creator 3.2.0, MSVC2010 build, Windows SDK v7.1 and debugging tools installed.
windows.h is included in my project and both functions are seen, but linker fails with
usb_part.obj:-1: error: LNK2019: unresolved external symbol __imp__RegEnumKeyW#16 referenced in function "bool __cdecl GetUsbName(class QString &)" (?GetUsbName##YA_NAAVQString###Z)
usb_part.obj:-1: error: LNK2019: unresolved external symbol __imp__RegOpenKeyW#12 referenced in function "bool __cdecl GetUsbName(class QString &)" (?GetUsbName##YA_NAAVQString###Z)
I suppose it tries to link those functions dynamically (hense the "__imp"), how can I set it to link against a static .lib file? Or, if that is not possible with dynamically built QT, how do I link those to .dll?
UPD:
Tried these:
LIBS += -ladvapi32
LIBS += AdvAPI32.Lib
LIBS += "c:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\AdvAPI32.Lib"
to no avail
UPD2:
Problem was in QT not recreating makefiles after changes to .pro. After I deleted makefiles, LIBS += -ladvapi32 worked fine. Thought that "Clean project" does that, but it turns out that it doesn't.
You should add to your .pro file
LIBS += -lAdvapi32
or
LIBS += Advapi32.lib
After that run qmake and build again.
[Update] It may be needed to remove generated makefiles manually and recreated them with qmake run again.

qt creator qt5.1 vs2010 linker error when using static library

i have a problem when i try to use a static library which was compiled with vs2010 in qt creater with qt 5.1.
I'm using qt5.1. which was compiled with/for the vs2010 compiler.
The source for my simple library look as follows:
Lib_Test.h
#pragma once
#include <iostream>
class Lib_Test
{
public:
Lib_Test(void);
~Lib_Test(void);
void HelloTest();
};
Lib_Test.cpp
#include "Lib_Test.h"
Lib_Test::Lib_Test(void)
{
}
Lib_Test::~Lib_Test(void)
{
}
void Lib_Test::HelloTest()
{
std::cout << "Hello World!";
}
This two files are compiled into my "Lib_Test.lib". I copied the lib and the header file to "C:/Qt/" to simplify the library calls.
My qt project file (for a c++ console application):
QT += core
QT -= gui
TARGET = Lib_Test_Qt
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:/Qt
DEPENDPATH += C:/Qt
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -lLIB_Test
And finally the main.cpp
#include <QCoreApplication>
#include <Lib_Test.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Lib_Test *lb = new Lib_Test();
lb->HelloTest();
return a.exec();
}
When i try to build the project in Qt Creator i get the following error message
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: void __thiscall Lib_Test::HelloTest(void)" (?HelloTest#Lib_Test##QAEXXZ) referenced in function _main
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: __thiscall Lib_Test::Lib_Test(void)" (??0Lib_Test##QAE#XZ) referenced in function _main
debug\Lib_Test_Qt.exe:-1: Fehler:LNK1120: 2 unresolved externals
When i declare the HelloTest method as a static method and try to call it without creating an instance of Lib_Test i get a similar error Message
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: static void __cdecl Lib_Test::HelloTest(void)" (?HelloTest#Lib_Test##SAXXZ) referenced in function _main
debug\Lib_Test_Qt.exe:-1: Fehler:LNK1120: 1 unresolved externals
What am i missing? Can someboody help? It's really frustrating right now :/.
Edit:
I tried DUMPBIN /SYMBOLS Lib_Test.lib in the msvs2010 console and all i get is:
Microsoft (R) COSS/PE Dumper Version 10.00.40210.01
Copytight (C) Microsoft Corporation. All right reserved
Dump of file Lib_Test.lib
File Type: LIBRARY
Does that mean that my library is somehow empty?? :/
In your qmake files, change
win32:CONFIG(release, debug|release): LIBS += -C:/Qt/ -lLib_Test
win32:CONFIG(release, debug|release): PRE_TARGETDEPS += C:/Qt/Lib_Test.lib
To
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -lLib_Test
Or
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -l_Test
I think one of them should work. And don't forget to go to Build menu, then Run qmake.