How to include a library in a Qt project - c++

I'm trying to create a project that uses the TagLib library. I'm not really sure of how exactly to go about it.
I have downloaded TagLib 1.11.1.
I built it as follows:
Build zlib, by first having CMake create a Visual Studio solution file, then building this solution with Visual Studio:
mkdir build && cd build
cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed"
msbuild /P:Configuration=Debug INSTALL.vcxproj
msbuild /P:Configuration=Release INSTALL.vcxproj
Build TagLib much in the same way:
cd ....\taglib-1.11.1
mkdir build && cd build
cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" -DZLIB_INCLUDE_DIR="e:\workspace\lib\installed\include" -DZLIB_LIBRARY="e:\workspace\lib\installed\lib\zlib.lib" -DWITH_ASF=on -DWITH_MP4=on -DBUILD_EXAMPLES=on
msbuild /P:Configuration=Release INSTALL.vcxproj
I create a simple Qt Console Application:
I then add tag.lib from the TagLib Build above in E:\workspace\lib\installed\lib using Qt
Qt -> Add Library -> Library Type (External Library) -> .......
main.cpp :
#include <QCoreApplication>
#include <QDebug>
#include <taglib/fileref.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TagLib::FileRef f("D:/Dire Straits - Sultans of Swing.mp3");
return a.exec();
}
taglibtest.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = taglibtest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag
INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag
INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds
HEADERS += \
taglib/aifffile.h \
taglib/aiffproperties.h \
taglib/apefile.h \
taglib/apefooter.h \
taglib/apeitem.h \
taglib/apeproperties.h \
taglib/apetag.h \
taglib/asfattribute.h \
taglib/asffile.h \
taglib/asfpicture.h \
taglib/asfproperties.h \
etc....
etc....
I get the following errors whenever I try Building the project in Qt:
F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib8FileNameC1EPKc'
F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefC1ENS_8FileNameEbNS_15AudioProperties9ReadStyleE'
F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
:-1: error: release/main.o: bad reloc address 0x0 in section `.ctors'
:-1: error: final link failed: Invalid operation
collect2.exe:-1: error: error: ld returned 1 exit status
What should I do to fix this and get working with TagLib?
taglibtest.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = taglibtest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag
INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag
INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds
HEADERS += \
taglib/aifffile.h \
taglib/aiffproperties.h \
taglib/apefile.h \
taglib/apefooter.h \
taglib/apeitem.h \
taglib/apeproperties.h \
taglib/apetag.h \
taglib/asfattribute.h \
taglib/asffile.h \
taglib/asfpicture.h \
taglib/asfproperties.h \
etc....
etc....

You have compiled TagLib and zlib with Visual Studio and you compile your project with mingw (at least that's what I can guess from error messages). This won't work. You need to compile all your libraries and apps with the very same compiler.
Visual Studio and gcc have different ABI's so gcc cannot see Visual's symbols.

Related

C++ HDF5 cannot find -lhdf5d

Situation: I want to create a program to read something from a .hdf5 file.
What i did: Nothing, but adding the hdf5.lib to the project.
Problem:
I get two Errors
when i try to run the program.
cannot find -lhdf5d
error: ld returned 1 exit status
My Code:
HDF5_Test.pro:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
win32:CONFIG(release, debug|release): LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5
else:win32:CONFIG(debug, debug|release): LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5d
else:unix: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
main.cpp:
#define H5_SIZEOF_SSIZE_T H5_SIZEOF_LONG_LONG //this is needed to avoid redefinition conflict of ssize_t (Qt vs HDF5)
#include <hdf5.h>
#include <H5Cpp.h> //Tried with and without this include
int main()
{
return 0;
}
Using:
C++
Qt 5.10.1
MinGW 32bit
HDF5 1.10.2
Windows 7
In another project for whatever reason these Errors doesn't occur, but these.
.
I linux, using apt-get install libhdf5-dev we get 8 libs installed but none is lihdf5 in its "nature". What i mean by that is that my include would be -lhdf5_openmpi and not-lhdf5. as you didn't provide the download source, try to check if you have multiple libs in your /lib file
The Solution was for me to delete the final "d" in this line:
else:win32:CONFIG(debug, debug|release): LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5d
Meaning i changed -lhdf5d to -lhdf5.
This problem was fixed this way but lead to another problem.

How to get the target directory in PRO file

I am building an application in Debug mode, how to get the target directory in pro file.
I didnt explicitly mentioned the target directory using "DESTDIR"
CORE_API_PATH = $$PWD/../Bin
SEPARATOR = "/"
QT += core gui xml widgets printsupport svg
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Cinemas
TEMPLATE = app
RC_ICONS += cinemas-icon.ico
qtHaveModule(opengl) {
DEFINES += QT_OPENGL_SUPPORT
QT += opengl
}
LIBS += -lQt5Concurrent -lglu32 -lopengl32 -lglut32 -LC:\Qt\Qt5.3.2\Tools\mingw482_32\i686-w64-mingw32\lib\glut -LC:\Qt\Qt5.3.2\Tools\mingw482_32\i686-w64-mingw32\lib\glu32
win32:CONFIG(release, debug|release): LIBS += "$$quote($${CORE_API_PATH}/Release/CoreApi.dll)"
else:win32:CONFIG(debug, debug|release): LIBS += "$$quote($${CORE_API_PATH}/Debug/CoreApiD.dll)"
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/SSH/lib/ -lssh2
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/SSH/lib/ -lssh2
else:unix: LIBS += -L$$PWD/SSH/lib/ -lssh2
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/3rdparty/qwt/lib/ -lqwt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/3rdparty/qwt/lib/ -lqwtd
else:unix: LIBS += -L$$PWD/qwt/lib/ -libqwt
QT += concurrent network
CONFIG += c++11
RESOURCES += \
cinemasresource.qrc
FORMS += \
I want to copy the app.exe from the current folder to some other folder
On Windows you can use DLLDESTDIR variable which specifies where to copy the target dll or exe. Just add this to your .pro :
CONFIG(release, debug|release): DLLDESTDIR += $$PWD/../exec
On Linux you can use QMAKE_POST_LINK variable which contains the command to execute after linking the TARGET together. So it is like:
CONFIG(release, debug|release): QMAKE_POST_LINK += $$quote(cp project $$PWD/../exec)
Here project is the name of the target file which you provide by TARGET = project
These will copy the executable binary to a directory named exec one level upper than the program working directory. You can have your arbitrary path.

Adding jsoncpp external library to my qt project : (symbol(s) not found for architecture x86_64)

I'm trying to add jasoncpp library to my C++ qt project.
I included the header, and linked (I think) the library but I get these errors:
"symbol(s) not found for architecture x86_64" and "linker command failed with exit code 1 (use -v to see invocation)".
The symbols not found are obviously the ones concerning jsoncpp library.
This is my makefile:
QT += core gui webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = JsonTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/release/ -ljson
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/debug/ -ljson
else:unix: LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/ -ljson
INCLUDEPATH += $$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/include
DEPENDPATH += $$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/include
I really don't know how to proceed so thanks in advance.
If you installed it using cmake, try this commands:
mkdir -p build/debug
cd build/debug
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..
make
change
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/release/ -ljson
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/debug/ -ljson
else:unix: LIBS += -L$$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/lib/ -ljson
INCLUDEPATH += $$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/include
DEPENDPATH += $$PWD/../../../../../../../usr/local/Cellar/jsoncpp/0.5.0/include
for this:
unix|win32: LIBS += -ljsoncpp
and ready to use jsoncpp

"cannot find -lQt5Core -lQt5Gui etc" when using other not related libraries

ISSUE SOLVED (scroll down)
Im on windows 7 and compilling with MinGW.
I made a new Qt application in QtCreator 3.01 (Qt 5.2.1). I compile it, the empty application window pops up everything is awesome. But as soon as i use any other library (like boost or gtest) im starting getting these errors when compiling
cannot find -lqtmain
cannot find -lQt5Widgets
cannot find -lQt5Gui
cannot find -lQt5Core
ld returned 1 exit status
This is my .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2014-04-19T14:17:24
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Thermovision
TEMPLATE = app
include(Model/Model.pri)
include(Controller/Controller.pri)
include(View/View.pri)
include(Interface/Interface.pri)
SOURCES += \
main.cpp
HEADERS += \
logdebug.h
FORMS +=
OTHER_FILES += \
Model/Model.pri \
View/View.pri \
Controller/Controller.pri \
Interface/Interface.pri \
Globals/zGlobals.pri
INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
INCLUDEPATH += $$PWD/../../../../../../../MinGW/msys/1.0/local/include
DEPENDPATH += $$PWD/../../../../../../../MinGW/msys/1.0/local/include
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../Downloads/boost_1_54_0/boost_1_54_0/stage/lib/ -lboost_thread-mgw48-mt-1_54
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../Downloads/boost_1_54_0/boost_1_54_0/stage/lib/ -lboost_thread-mgw48-mt-1_54d
this is the main.cpp
#include <iostream>
#include <QApplication>
#include "View/form.h"
#include <boost/thread.hpp>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form w;
w.show();
boost::thread b;
return a.exec();
}
when i comment usage of boost thread, the include and the part in .pro file responsible for finding boost thread library everything compiles again. Same when im trying to use gtest.
The odd thing is that I havent found any information regarding this issue anywhere on the internet :<
Can someone help me?
=================================================================================
[SOLVED]
ok so it seems qt wanted me to explicitly add the libraries into project. I used the built-in tool which generated the following output
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/libQt5Core.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/libQt5Cored.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/Qt5Core.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/Qt5Cored.lib
but im sure one can thin it down (im gonna stay with this)
Based on our comments and outcome, it seems that a quick solution is to add the libraries explicitly that you wish to use. It will generate something like this in the background, so basically using static libraries as opposed to dynamic:
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/libQt5Core.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/libQt5Cored.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/Qt5Core.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/Qt5Cored.lib
Alternatively, which I would personally suggest more is to add the following line for now in your project file to get this working with dynamic libraries rather than static to make your application binary go up to the hill:
LIBS += -L$$PWD/../../../../../../../Qt/Qt5.2.1/5.2.1/mingw48_32/lib/

"No rule to make target" error when adding an external library in Qt Creator

I am new in the world of Qt. I would like to add an external library of wxMSW-2.8.12 in Qt Creator. In order to do that, I right-clicked on my .pro file and clicked on "Add Library", then I select "External Library". I finally choose the Library file, click on "Static" for "Linkage".
This is what I got automatically from Qt Creator:
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xml
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xmld
else:unix: LIBS += -L$$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/ -lwxbase28ud_xml
INCLUDEPATH += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib DEPENDPATH += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xml.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xmld.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/wxbase28ud_xml.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/wxbase28ud_xmld.lib
else:unix: PRE_TARGETDEPS += $$PWD/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xml.a
But, when running, I get this error:
:-1: erreur : No rule to make target 'C:/Qt/Qt5.2.0/Tools/QtCreator/bin/testConverter/../../../../../../wxMSW-2.8.12/lib/vc_lib/libwxbase28ud_xmld.a', needed by 'debug\testConverter.exe'. Stop.
I tried to clean and rerun qmake but with any success.
Any suggestions?