QtCreator - Can't link against static library (ld error) - c++

I'm having a problem while using QtCreator 3.0.1 with Qt 5.2.1 on Linux. I'm trying to compile my project using QtCreator's built in 'build' function, which I imagine runs qmake project.pro and then make. My project uses QScintilla, which I have compiled into a static library (.a), and included in my project's directory in qscintilla/Qt4Qt5.
After compilation is done, the compile output pane shows the following:
/usr/bin/ld: cannot find -lqscintilla2
collect2: error: ld returned 1 exit status
My .pro file contains:
LIBS += -Lqscintilla/Qt4Qt5 -lqscintilla2
However, running qmake and then make on the project's root directory via a terminal emulator works perfectly, and the project is compiled and linked successfully. I have double checked that the qscintilla/Qt4Qt5 directory contains the file libqscintilla2.a.

Since you seem to be using Linux, it would be better to leave the package with your distribution. Getting packages installed bringing static libraries into the system would be more "vanilla".
However, if you wish to stick to the in-project build principle, I would suggest not to disable the shadow build and create other workarounds having their own limitations.
You could for instance use the $$OUT_PWD variable for this purpose, but in general, you would need to specify the target dependency as well between the components.
So, you would be writing something like this then:
LIBS += -L$$OUT_PWD/qscintilla/Qt4Qt5 -lqscintilla2
Please adjust this based on your directory layout.

You should also add the line:
PRE_TARGETDEPS += qscintilla/Qt4Qt5/libqscintilla2.a
to your .pro file in order to link the library statically.

Managed to make it work: on QtCreator, click on "Projects" on the left pane, which will open a tab which allows you to edit the build/run/style options. On the Build/General section, disable "Shadow build". This will build the project on the original project directory.
Edit: see #LaszloPapp's answer for a better solution.

Related

Qt 3rd Party Library Static Linking (QtSerialPort)

Turns out static linking was working, but only for Qt libraries. My 3rd party library QtSerialPort is not linking statically. After some reasearch, I've found that I either have to build this library statically or I have to link directly to a .pri file in my .pro file.
I'm not sure how to do either since it seems QtSerialPort has not been designed for static linking.
The .pri method I really don't understand and has been vaguely described in these two links:
http://qt-project.org/forums/viewthread/15223
http://www.qtcentre.org/archive/index.php/t-54505.html
Does anyone have any adivce on how to get either of these methods to work? Or possibly another method?
Also, MSVCP100.dll is not linking statically if anyone could give me any advice on that.
==================================================================================
I am trying to get Qt to statically link libraries so that I can make a standalone application. I have followed various tutorials on how to build Qt statically then building a static application but I am not having much luck. I believe I have succesfully built Qt with static linking because the application has grown in size from 79KB to 7+MB but I am still getting errors saying QtCore4.dll and QtSerialPort.dll are missing. Also, another issue I'm having when using this static configuration, which isn't too serious, is that when I close my program Windows thinks it has crashed and gives me a window saying MyProgram.exe has stopped working...
I am on a Windows machine using MSVC 2010 with Qt 4.8.5 and am using the third party library QtSerialPort.
What I've done accoring to the guides I've been reading is:
Download and extract qt-everywhere-opensource-src-4.8.5.zip
Open /mkspec/mwin32-msvc2010/qmake.conf and change the follwing lines to
CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target
and
QMAKE_CLFAGS_RELEASE = -O2 -MT
I then open the MSVC2010 command prompt and cd to this . I then enter the commands
configure -static -release -platform win32-msvc2010
nmake sub-src
After this is done I open my project and add
CONFIG += static
to the .pro file. In QtCreator I then go into Projects, Manage Kits then to Qt Versions and browse to the qMake I just generated. I add a new Kit with this version of qMake. I then clean all and switch to this new kit and run qmake from QtCreator. I then use msvc2010 command prompt to go to the directory where the files are generated and then
nmake release
This generates a rather large .exe but like I said, it's still depending on a couple .dll's.
For static linking of external library one have a couple options, both have their pros and cons.
I. Compile the library for static linking yourself. Link to it.
Look for possible existing configuration switches for static linking. There can be something like QTSERIALPORT_STATIC = no, etc. in the library's .pro/.pri files. Just say yes for the library to compile for static linking and go to the step 4!
In .pro/.pri file replace CONFIG += dll with CONFIG += static.
Remove export declarations from the library. Typically Qt library symbols are declared with some definition like QTSERIALPORT_EXPORT which expands to Q_DECL_EXPORT/Q_DECL_IMPORT in shared library build / its header files usage when linking. You'll need to find where this QTSERIALPORT_EXPORT is defined and replace it with empty definition:
#define QTSERIALPORT_EXPORT // in source file
or
DEFINES += QTSERIALPORT_EXPORT # in .pro/.pri file
Build the library.
Link to the library .lib/.a file, use the library header files for symbol declarations in your project.
II. Include the library source files into your project and compile them within it (no linking at all).
Include all the source files of the library into your project (add to SOURCES in qmake project file)
Determine all the stuff the library depends on (other libraries, Qt options, etc.) and include it also into your .pro file.
OR
Include the proper .pri file into your .pro if the library author provides it for in-project compilation (i.e. include(QtSerialPort.pri) or something.)
Remove export/import declarations from the library source code — as described in the item 3 of part I.
Build your project.

How configure Qt Creator to compile qupzilla?

Is there any simple way to configure Qt Creator to compile qupzilla in my folder (not in /usr/bin and /usr/lib)?
By default i get "error: ../../bin/libQupZilla.so". If i set the path to my app folder with ldconfig - app is compiled but without plugins. They return errors for example (build/qrc_testplugin.cpp Error 1 or something with locale folder).
OS: Arch Linux 64 bit
QT: Qt5
Also i successfully compile qupzilla-git from AUR with qt5. This means that my problem somewhere in incorrect settings or paths...
Qupzilla: http://www.qupzilla.com/
Solution:
1) Add path to programm "path_to_app/bin" to ldconfig (/etc/ld.so.conf)
2) Check off in Projects "Shadow build" checkbox.
3) Change qmake to qmake-qt5. If I understand correctly, it is not necessary to do. Since in Arch Linux qmake and qmake-qt5 same.
4) In build environment add
KDE true
USE_WEBGL true
If you have libQupZilla (or any library) somewhere else than system library directories, easiest is to edit the .pro file, add line like:
LIBS += -L$(PWD)/..relative-path-to-library-location../
...or use absolute path to the library if you want. You probably already have -lQupZilla there, so no need to add that the 2nd time.
In general, with problems like this in Qt Creator, look at the Compile Output tab (at the bottom of the screen). Find the link command (note: if project uses cmake instead of qmake, you may need to add some switches or something for that to make the command visible), which is the one with all the -L/path and -lfoobar switches, and -o programname outputting the final target. Then look earlier to see in what directory it is being run in. Then look if everything matches, and possibly try to copy-paste and run the command from command line yourself, in the same directory where it is run when building under Qt Creator.

Compile a Qt-Project and Include Qt Libraries

I have a C++ project using Qt and I have it working as expected. However, the problem is when I copy
the executable to a standard installation of a OS (e.g. Windows or a Linux) upon execution I receive the QtWidgets or some other Qt libraries missing error.
I tried referencing the Qt Documentation but I am unable to find a solution or an example of what I am after. It has something to do with Static and Dynamic Building... but could not locate a good example or tutorial.
I looked at the tutorial http://www.qtcentre.org/wiki/index.php?title=Deploying_Qt_Applications but it is not quite the most efficient solution.
Basically I want the system to include the required Qt libraries along with the final compiled file. How can I pull it off?
What you can do is making a script which copies your DLLs (or .so* files) in the executable folder. Once you make it add an extra Makefile target in your project file :
theproject.pro:
OTHER_FILES += copy.script # Assuming your copy script is called "copy.script"
deploylibs.target = deploylibs
deploylibs.command = ./copy.script # Launching the script
QMAKE_EXTRA_TARGETS += deploylibs
Now you can deploy your libraries by using the (n)make deploylibs command.
NB: there is a very useful tool called "Dependency Walker" to find the DLLs you need. It is only for Windows. As for Linux and its *.so* files you can use the ldd command :
>$: ldd theexecutable
alib.so => /path/to/library/on/your/computer.so
...

Qt app finds libraries when launched from QtCreator, but not from command line

I need to ship a product with shared libraries (Ubuntu) in a folder I've created ('shared') within the project directory--i.e., shared libraries that are not in /usr/lib, /usr/local/lib, etc. To this end I've done this in the .pro file, i.e.
LIBS += -Lshared/gsl -lgsl -lgsl_cblas
NB the executable is in the same directory asy the 'shared' folder. Everything works when I launch the program in debug mode from QtCreator. In addition the program fails to launch if I remove or rename the libs in 'shared', and also I've verified that the app is not finding these libraries anywhere else. But when I launch from the command line, I get
error while loading shared libraries: libgslcblas.so.0: cannot open
shared object file: No such file or directory
What's going on?
EDIT: The solution is a variation on lionbest's below, as developed in the comments. For some reason, it turns out that on my platform qmake/QMAKE_FLAGS only allows a specific format for linker options, and not the one you find googling around for other instances where people have worked with QMAKE_FLAGS. Namely,
QMAKE_FLAGS += "-Wl,-rpath,\'\$$ORIGIN/shared/gsl\'"
QMAKE_LFLAGS_RPATH=
Specifically, the problem was that the parser (or linker) would not accept -rpath=..., despite the fact that this seems to work for everyone else. Ubuntu 12.04, gcc 4.6.3, Qt 4.8.0, QtCreator 2.4.1 "based on Qt 4.7.4". Hope this experience can save someone else the same frustration.
Start a program with environmental variable LD_LIBRARY_PATH=shared/gsl or add a RPATH to linker settings:
LIBS += -Wl,-rpath=shared/gsl
If you want to make shortcut for your application, most distribution (based on Gnome, KDE, LXDE and XFCE) use a .desktop file. QtCreator probably generated one for you. You need to edit it or generate it, and put to $HOME/.local/applications or /usr/share/applications/ during installation. In desktop file you could add envirometal varible and starting directory as follow:
Exec=/usr/bin/env LD_LIBRARY_PATH=/<path_to_libs>/ /<path>/app -extra_options
If you need to run your application in specific directory add line:
Path=/<path_to_your_application_working_dir>
You can use ${} inside path.

How to get qmake to generate "project dependencies" in a Visual Studio .sln project

I have a qmake build of a few libraries and an app which depends on them. Using the subdirs template I'm able to get qmake to output a .sln file which works almost to my liking in VC2008. Though I've specified the dependencies between the targets in every way I've seen described, I end up with no "project dependencies" in the .sln file, and I have to add these in manually.
So far I've tried
CONFIG += ordered
with correct ordering to no avail.
And similarly the more arcane syntax:
client.depends = core common
Which also doesn't work. No dependencies whatsoever show up when I load the sln.
Both CONFIG += ordered and target.depends = are not supported by the qmake's MSVC backend (solution generator). Back in 2010 with Qt 4.7 around, the docs didn't mention that, but in Qt 4.8 the developers have updated the docs accordingly (see the Target section remarks):
.depends This subproject depends on specified subproject. Available only on platforms that use makefiles.
The ordered option is not supported for Visual Studio.
But they had provided a workaround (which is discussed in that cryptic post), and it's still valid and even documented in the same target section. Too bad I had to rebuild qmake and use a debugger to verify that:
a) There is a Lib/DLL project of which TARGET (the .lib is used and not the .dll) is used on the link line of another project in your solution (you can modify the link line with LIBS).
b) There is an Exe project of which TARGET is used in a custom build-step of another project in your solution.
You don't use paths in the TARGET variable (use DESTDIR/DLLDESTDIR for that), e.g, TARGET=$(SOME_VARIABLE)/myLib, won't work.
If you have a special location for your libs, you specify the -Lmy/library/path and LIBS += mylib, instead of just using LIBS += my/library/path/mylib
The leaf projects are created before you generate the solution file. (You can use the recursive flag for qmake to do this, like "qmake -tp vc -r [yourproject.pro]"
Basically, qmake will generate dependency when your lib's target name (yourlib.lib) is equal to the one of the import libraries of the final app (that has LIBS += yourlib.lib).
(See qmake's source where the import libraries are added as dependencies, and a little further where they're compared with the project target names)
Here is the minimal setup that generates dependencies in the solution:
solution.pro
TEMPLATE = vcsubdirs
SUBDIRS = main app
app/app.pro
LIBS += main.lib
main/main.pro
TARGET = main
TEMPLATE = vclib
With those, if you run qmake -r -tp vc, you'll get the explicit dependency in the generated .sln:
GlobalSection(ProjectDependencies) = postSolution
{E634D0EB-B004-3246-AADA-E383A376158F}.0 = {1BD6E999-63E6-36F5-99EE-1A650332198C}
EndGlobalSection
From an old mailing list entry:
http://lists.trolltech.com/qt-interest/2006-07/thread00238-0.html
It appears that it tries to figure out which things are dependent for you. Are you able to build from the sln without entering the project dependencies manually?
I am not a wiz in makefiles but if I were you, I would try to recreate that dependency in with QtCreator by editing the .pro file, running qmake then looking at the auto-generated result in the MAKLEFILE. If you want to know how qmake works then look at the qt documentation.