Qt5 and MacOS X - qt

I've installed Qt5 on my Mac OS X Yosemite from MacPorts.
If I compile this simple file
#include <QtGui>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QLabel lbl("Hello World!");
lbl.show();
return app.exec();
}
I'll get error
Undefined symbols for architecture x86_64:
"QApplication::exec()", referenced from:
_main in main.o
"QApplication::QApplication(int&, char**, int)", referenced from:
_main in main.o
"QApplication::~QApplication()", referenced from:
_main in main.o
"QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)", referenced from:
_main in main.o
"QLabel::~QLabel()", referenced from:
_main in main.o
"QWidget::show()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [123.app/Contents/MacOS/123] Error 1
I've change QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6
to QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
or QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.10
in file /opt/local/share/qt5/mkspecs/macx-clang/qmake.conf
but it has no result.

Hard to know exactly without more information, but check that you are adding in widgets in your .pro file:
QT += gui core
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
You may have to run qmake explicitly if you aren't using qtcreator. Also, wouldn't you be showing a main window instead of a QLabel? I haven't checked if that's legit doable code, but maybe you should start showing a main window and verify that works first. QTCreator has a project template for a new Qt Widgets Application. Also check to see that macports is installing the latest qt5 version in the event it's a bad path or config on their part.

Related

How to avoid std::filesystem linker errors with Qt?

I would like to use the std::filesystem with Qt 5.12.0 with the g++ version Ubuntu 8.2.0-7ubuntu1, but getting linker errors:
g++ -lstdc++fs -Wl,-rpath,/home/user/Qt/5.12.0/gcc_64/lib -o qf_filesystem_test main.o -L/home/user/Qt/5.12.0/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
/usr/bin/ld: main.o: in function `std::filesystem::exists(std::filesystem::__cxx11::path const&)':
/usr/include/c++/8/bits/fs_ops.h:121: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
/usr/bin/ld: main.o: in function `std::filesystem::__cxx11::path::path<char*, std::filesystem::__cxx11::path>(char* const&, std::filesystem::__cxx11::path::format)':
/usr/include/c++/8/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:257: qf_filesystem_test] Error 1
22:12:16: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project qf_filesystem_test (kit: Desktop Qt 5.12.0 GCC 64bit)
When executing step "Make"
After some googling, I found that I need to use the linker flag -lstdc++fs. My code builds perfectly with the command g++ main.cpp -std=c++17 -lstdc++fs, but I can't seem to make it work inside Qt Creator. My simple test code is the following:
#include <iostream>
#include <filesystem>
int main(int argc, char *argv[])
{
if(1 < argc)
{
std::cout << argv[1] << " does ";
if(!std::filesystem::exists(std::filesystem::path(argv[1]))) std::cout << "not ";
std::cout << "exist!" << std::endl;
}
return 0;
}
My .pro file looks like this:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qf_filesystem_test
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++17
QMAKE_LFLAGS += -lstdc++fs
SOURCES += main.cpp
After some tests with g++ It seems to me, that the problem is caused by the order of the g++ flags, because Qt puts the -lstdc++fs to the front.
Why do I need to still use this flag? I thought that g++8 already supports C++17 and this flag is only needed if I want to use the std::experimental::filesystem.
How could I make my code build in Qt Creator?
<filesystem> is a separate library for GCC 8 (see this question). Your issue, as you suspected, is in the order of the flags. Poking about a bit in the docs hints that QMAKE_LFLAGS is more for linker flags than library loads, which is why it gets passed early (e.g. -O3).
Using LIBS += -lstdc++fs should work instead.
Credit to this reddit response for this solution.

Linker error caused by Qt qjpeg4?

I cannot link my project because of some issues with the qjpeg4 library.
Its DLL is located here:
PROJECT_ROOT/../Release/obj/imageformats/qjpeg4.dll
I get this linker error:
Linking CXX executable test.exe
CMakeFiles\test.dir/objects.a(entrypoint.cpp.obj):entrypoint.cpp:(.text.startup+0x2692): undefined reference to `qt_plugin_instance_qjpeg()'
collect2: ld returned 1 exit status
In the file containing main() I have: Q_IMPORT_PLUGIN(qjpeg)
I tried adding these lines, with no improvement:
QApplication app(argc, argv);
QString sDir = QCoreApplication::applicationDirPath();
app.addLibraryPath(sDir + "/plugins");
The previous error should even not depend on the fact that the DLL has not been found, since it is still in the link phase...
If in the file containing main() I remove Q_IMPORT_PLUGIN(qjpeg), the linker is successful, but the executable does absolutely nothing.
The only Qt headers I include are <QApplication> and <QtPlugin>; adding <QtGui> has been useless.
From this link, it seems to be a bug in Qt:
https://bugreports.qt-project.org/browse/QTBUG-24177

QtSql program doesn't work with shared lib configuration

I wrote a sample program using QSqlDatabase object two years ago with a Qt configuration that depended on static libraries. It compiled ran as expected. Some time last year, I rebuilt the configuration using shared libraries and now I am getting the following errors.
Here is the error:
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) publ
ic: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase##QAE#XZ
) referenced in function _main
...
debug\qtsql.exe : fatal error LNK1120: 12 unresolved externals
Here are my includes and the instantiation of the object in main.cpp:
#include <Qt>
#include <QtDebug>
#include <QtSql\QSqlDatabase>
#include <QFile>
#include <QtSql\QSqlQuery>
#include <QString>
#include <QVariant>
#include <QDate>
int main()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
...
Also, here is my .pro file:
TEMPLATE = app
TARGET =
DEPENDPATH += . versions
INCLUDEPATH += .
# Input
SOURCES += main.cpp
# Libraries
QMAKE_LIB_DIR += C:\\Qt\\4.7.0\\lib
I thought that last line in the .pro file would give me a link to any library I might need in Qt. I'm pretty ignorant of what it takes to link to the Qt .dll's.
What am I missing to get the program to see the QSqlDatabase library?
Also, must I specify the library even though I have given it the header file and library directory?
CONFIG += qt
QT += sql
might be missing here (link to qmake docs)
Here is a link to the Qt sql examples. Perhaps you find inspiration/guidance there.

Cannot get a QT "hello world" to work!

#include <QtCore/QCoreApplication>
#include <QtGui/QLabel>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLabel label("Hello world");
label.show();
return a.exec();
}
I am using QTCreator, I installed QT 4.7 on windows.
Now every time I compile I get this error:
:: error: collect2: ld returned 1 exit status
With mingw32 giving me the following
tmp/obj/debug_shared/main.o: In function `main':
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:6: undefined reference to `_imp___ZN6QLabelC1ERK7QStringP7QWidget6QFlagsIN2Qt10WindowTypeEE'
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev'
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\QTHello.exe] Error 1
mingw32-make: *** [debug-all] Error 2
The process "E:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project QTHello (target: Desktop)
When executing build step 'Make'
Whats the problem??
Create a Qt GUI project and olny then write what you have written.
Your project file is probably not including the required stuff. Do you have either
TEMPLATE = app
or
QT += gui
in your .pro file?
Are you linking with QtGui? If you don't, do it.
Also, for UI applications you must use QApplication. QCoreApplication is for CLI-only tools.
You want to create a Hello World app with QT? Really simple:
Create a new GUI app into QtCreator
Open the ui file and add a label using the QtDesigner embedded into QtCreator
Compile and launch
You have your Hello World application done with Qt
Then if you want to know how to write a QTGui application, just open files in project to see what is written in them.

qt add path for 3rd party header and libraries

I am using qt and developing a desktop app that will run under win xp/vista.
I have a 3rd party library UserAgentLib (static, and shared). But I am not sure how to link in qt creator.
I have opened the *.pro file and added my library and header path.
The library is called UserAgentLib and the header file is called UserAgentLib.h
TARGET = Dialer
TEMPLATE = app
LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib
INCLUDEPATH += D:\Projects\qtDialer\tools\inc
SOURCES += main.cpp\
catdialer.cpp
HEADERS += catdialer.h
FORMS += catdialer.ui
I think it does find the header file, as I get about 100 errors for declarations in the UserAgentLib.h file. However, I don't think it is linking with the library.
Many thanks for any suggestions,
======================
I have create a very simple library in VS C++ 2008. Here is the code for the header and source file.
Header:
// mathslibrary.hpp
int add_numbers(const int a, const int b);
Source:
// mathslibrary.cpp
#include "mathslibrary.hpp"
int add_numbers(const int a, const int b)
{
return a + b;
}
I have compiled this into a library. And tested by linking with a WIN32 console application in VS 2008. The library worked as expected.
Now when I try and link with qt.
#include <QtCore/QCoreApplication>
#include <iostream>
#include "mathslibrary.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "add numbers 40 + 60 = " << add_numbers(40, 60) << std::endl;
return a.exec();
}
This is my qmake file:
QT -= gui
TARGET = testlibrary
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS = D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib
INCLUDEPATH = D:\Projects\TestLibrary\mathsLibrary\
SOURCES += main.cpp
These are the errors I get when I try and build:
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c::-1: error: undefined reference to `WinMain#16'
:-1: error: collect2: ld returned 1 exit status
And these are the compile issues:
Running build steps for project testlibrary...
Creating gdb macros library...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/mingw/bin/mingw32-make.exe debug -w
mingw32-make: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
C:/Qt/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\testlibrary.exe -L"c:\Qt\qt\lib"
D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib -lQtCored4
mingw32-make[1]: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
mingw32-make: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:
(.text+0x104): undefined reference to `WinMain#16'
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\testlibrary.exe] Error 1
mingw32-make: * [debug] Error 2
Exited with code 2.
Error while building project testlibrary
When executing build step 'Make'
Many thanks for any advice,
Don't know if this changes anything, but maybe you have to define it like this:
LIBS += -LD:/Projects/qtDialer/tools/lib -lUserAgentLib
If you are getting compiler errors then your UserAgentLib.h probably didn't get included. You can test it with:
!exists( UserAgentLib.h ) {
error( "No UserAgentLib.h file found" )
}
You put the above in one of the .pro file and not the constructor.See this.
If the library didn't get linked (which is after your application has compiled well) -- then you need to tinker with your LIBS += ... line, though which appears fine on first glance.
Try this with the simple library first and then try it with the library you are actually trying to get working.
LIBS += D:\Projects\qtDialer\tools\lib\mathsLibrary.lib
In your .hpp file, add extern "C" before your function declarations:
// mathslibrary.hpp
extern "C" int add_numbers(const int a, const int b);
Rebuild the library from Visual Studio.
Now you should be able to compile your test app with Qt Creater. Then copy the corresponding dll into the directory with your new executable and give it a run.
As far as i understood, you generated a dll using MSVC and now you are trying to link it in Qt using mingw. right?
Object files and static libraries created with different compilers, or
even with significantly different releases of the same compiler, often
cannot be linked together. This issue is not specific to MinGW: many
other compilers are mutually incompatible. Build everything from
source with the same version of the same compiler if you can
chech this out : http://chadaustin.me/cppinterface.html

Resources