How to include external libraries in Qt - c++

I want some basic explanation about how external libraries are added to a Qt C++ project in a Linux environment
I've installed a library called podofo in two ways :
- Installed it with apt-get install libpodofo
- Installed the source code and build it
Afterwards i added in the .pro file of my Qt Project : LIBS += -lpodofo
How do i choose whether he works with the first or the second one ?
How am i supposed to add an external library with a lot of .jar files in my project ?
I figured it might be something in the run environment but i still don't get it. I tried changing it but it seemed better to just copy libraries manually in the file Qt is installed in.

Like most libraries on Linux, podofo installs a pkg-config file. The file is named libpodofo-0.pc. qmake has built-in support for that, so all you need to do is add this to your project file:
PKGCONFIG += libpodofo-0
(Note that for this to work, you might need to add link_pkgconfig to your CONFIG line.)
Do not add anything to LIBS. qmake will call pkg-config and add the needed compiler and linker flags automatically. Specifically, it will add the correct -l flags for linking, and the correct -I flag for compiling (usually -I/usr/include/podofo). So when you include the podofo headers in your code, don't use #include <podofo/podofo.h>. Use #include <podofo.h> to keep your code portable when building on systems that might be using a different directory name to install the podofo headers.
Make sure that the pkg-config tool is actually installed on your system though. pkg-config --version should print something like 0.29.1. If instead the command is not found, then install the pkg-config package of your Linux distribution.

Related

Compiling QT OPCUA on Windows

I have a problem when I try to compile Qt OPCUA on windows with open62541.
I am following this tutorial (Building On Windows - Mingw32)
I cannot seem to specify the path to the header open62541.h and the lib open62541.a even if I did
set QTOPCUA_OPEN62541_INCLUDE_PATH=c:\path\to\open62541\build
set QTOPCUA_OPEN62541_LIB_PATH=c:\path\to\open62541\build\bin
Note that open62541 is compiled and that I do have the files open62541.h and libopen62541.a (in version 0.3) where they should be.
The error I get is a linker problem : cannot find -lopen62541
I added manually the open62541.h to the project files since it was not working either.
What should I do to specify correctly the path to open62541 please ? Is there an up-to-date documentation ?
You need to add the path where libopen62541.a is to the library path directory.
If you are using QMake, you need to specify add it to the LIBS variable prepending the path with a -L as follows:
LIBS += -L$${PATH_WHERE_LIB_FILE_IS}
Take a look on how is done in the QUaServer project.
If you are using CMake, then take a look at this answer on how to add to the library path directory.
What you are doing with those commands is telling the linker where to find the missing libraries.

wxWidgets jpeg library build issue

I'm trying to build wxWidgets library into a custom path on a Fedora 27 operative system.
I achieved the wx-config file path recognition and works with the cmake execution. Also, I load libraries and include dirs based on modified wxWidgets finder cmake file that sets thewx-config custom path successfully.
But cmake does not load my wxWidgets configuration. I mean, wx_gtk2u_jpeg-3.1 builded lib could not be founded (suposed to be /usr/lib/libwx_gtk2u_jpeg-3.1.so). I need jpeg dependency from wxWidgets for my project.
I'm sure that problem is not about cmake files. However, the problem is wxWidgets compilation because cmake can found the other builded dependencies into /usr/lib/
I actually installed the libjpeg-turbo-devel package that includes the libjpeg.h needed for wxWidgets building without success of libwx_gtk2u_jpeg-3.1.so creation.
The weirdest part is that $ wx-config --libs shows the wx_gtk2u_jpeg-3.1 lib to be linked and the hint paths that it should be founded.
wxWidgets commands for building:
$ ./configure --with-libjpeg=builtin --with-libpng=builtin --with-libtiff=builtin --with-zlib=builtin --with-expat=builtin --enable-webviewwebkit=no --prefix=/opt/cpp_dependencies/2018Q1/usr'
$ make -j 4
$ make install
You can check out my cmake files, the cmake output and wxWidgets building output in order to reproducing it: https://gist.github.com/jjalvarezl/b70accae269ef56c56010bedf157c27f
You can see line 1543 of wxWidgets building output file that jpeg library is buildin, and, 1564 of same file, the make install command that installs all libwx_<lib_name>.so libraries into final /usr/lib path. Anyway, no one contains the needed library.
Please show the exact error message, as it's not clear what the actual problem is. What I can say, is that the different built-in versions of 3rd party libraries, such as libjpeg, are always static libraries, even when wxWidgets themselves are shared. I.e. you're never going to have libwx_gtk2u_jpeg-3.1.so, only .a.
I'd also strongly recommend using system versions of the 3rd party libraries under Unix systems. This means that your wxWidgets applications will get security updates from your OS vendor and you don't risk running into any incompatibilities due to using 2 different versions of the same library in your application.

Some doubts about libraries

Say, I want to write a program in C++ in Linux and I need some specific libraries. There are 2 ways of getting these libraries:
Using the command line--> apt-get install library
Downloading and extracting a .zip or .tar file from their website.
Now my questions are:
For the first method I have seen libraries being downloaded with apt-get install library and apt-get install library-dev. I know dev means development or developer, but what is the difference between installing the dev and not installing the dev? What does dev do, exactly?
For the second method, do I need to build the libraries using a compiler? Because I have seen tutorials doing it but the OS used was usually Windows, do I only have to build them on Windows and not Linux?
Also, say I can only use the 2nd method for a certain library and not the first one. After extracting, what am I supposed to do? Is there any default way of installing a library manually or is each library different?
Finally, when I use the first method where is the library installed to? Is it /usr/local/lib, /usr/lib or /usr/include? Because when I have to link to these libraries in the Linker's settings I only write their name, not the path so I assume there is already a default path for libraries to be in.
One last question: Is there any default way of installing and using libraries in general or does that depend on what I want to do, programming language, etc...?
The second method is very broad because it depends entirely on the how the project is designed including the build system used etc. Things get a little more conformant when you use a distribution's managed packages.
If you want to develop a program that uses the library you need the library-dev package that usually contains the C/C++/etc.. header files.
Many development package conform to a standard tool that helps your build system find the libraries header and binary files.
For example libcurl uses the pkg-config system so its compiler components can be found from the command line like this:
pkg-config libcurl --libs # print the library link flags
You can then add that to your Makefile (or whatever build system you use):
program:
g++ -o program program.cpp $(shell pkg-config libcurl --libs)
The $(shell pkg-config libcurl --libs) part adds the correct compiler flags to link with the library.
Not all dev packaged use pkg-config. Some come with their own tools (like mysql_config) while others let you guess and try to figure it all out for yourself (looking at you libclang).

exploiting poppler in Qt creator - help

I am trying to compile a Qt project that uses poppler library for pdf (linux).
I put #include in the mainwindow.cpp
I put poppler folder in my project folder besides mainwindow.cpp
poppler-qt4.h is in poppler-0.16.7/qt4/src
I do not need to recompile poppler, I just need to link the library through its headers but I do not know how to do. If compilation is necessary I can do it, but I tried ./configure and it said "./configure not found".
I searched for other similar threads but they were not enough useful to me.
I know LIBS is involved but makefile is overwritten by Qt-creator?
I'd prefer to learn how to "officially" inform Qt-creator that I want to add the library.
Please can help?
You have a good example, completed with source code, here: Poppler: Displaying PDF files with Qt
Along with including the header files where apropriate, you need to link to the poppler library.
To do so, you need to edit your .pro file and include something like:
INCLUDEPATH += /usr/include/poppler/qt4
LIBS += -L/usr/lib -lpoppler-qt4
These are the "default" paths, you may need to change them accordingly to your particular install location.
EDIT:
From your comments you seem to be trying to build poppler lib from source. The problem is that you're executing ./configure ( make and make install ) in the wrong directory. You "need" to position yourself in the directory where the file configure is located(*). Then execute the traditional commands:
./configure
make
make install
You run configure (you usually have to type ./configure as most
people don't have the current directory in their search path). This
builds a new Makefile.
Type make This builds the program. That is, make would be
executed, it would look for the first target in Makefile and do what
the instructions said. The expected end result would be to build an
executable program.
Now, as root, type make install. This again invokes make, make
finds the target install in Makefile and files the directions to
install the program.
I extracted this quote from http://tldp.org/LDP/LG/current/smith.html. But there are lots of places where you can find more information about these commands. Just google it! :D
(*) You don't really need to be in the same directory as the configure file. But it's easier than writing the full path.

How to use SOCI C++ Database library?

I'm trying to implement soci in my program but I don't know how. I'm using C++ on Linux, on a project using netbeans. I have followed the steps in: http://soci.sourceforge.net/doc/structure.html to install it, and I tried to copy the files soci.h from /src/core and soci-mysql.h from /src/backends/mysql in my project but it gives a compilation error (these files include other soci files, but it's illogical to copy all files into the directory...). I have read the guide several time but I don't understand what I'm doing wrong. The examples only include these files.
Thanks.
Edit: I have given more information in a comment below the answer. I don't know what steps I have to follow to implement soci.
The relevant bit on that page is
When the configure script is run without parameters, the remaining part of the process will use /usr/local/include/soci as a default destination for SOCI header files and /usr/local/lib as a default destination for library files
Now /usr/local/include ought to be in your default include path (e.g. try something like gcc -c -v -x c++ /dev/null -o /dev/null to see the list your install uses) and so you can include these using
#include <soci/soci.h>
#include <soci/soci-mysql.h>
You then need to add the libraries to your link step. It looks like you'll have both static and shared versions of the libraries. You'll need to add -lsoci_core -lsoci_mysql to your link step; however if that doesn't work then you'll also need to specify /usr/local/lib as a search directory i.e. -L/usr/local/lib -lsoci_core -lsoci_mysql. (Again it's probably there already but you can see using gcc -print-search-dirs.) However, the issue then is that if you're using the shared version and /usr/local/lib isn't in your distributions library search path (see /etc/ld.so.conf and/or /etc/ld.so.conf.d/*) then it won't be able to find the shared library at runtime. You'll need to either hard-code in the path to the library with the linker switch -rpath or add /usr/local/lib to the system-wide search path as before or in your environment (variable LD_LIBRARY_PATH). I'm not sure what the best way to do this is - I'd suggest -rpath to avoid modifying the system in general, although if you're building a lot of libraries into /usr/local/lib it might make sense to add it.
I got the same doesn't load backend error on my C++ program when I execute session sql("mysql://db=...)
I found a solution (at least on my Ubuntu 11.04). Just do:
sudo -i ln -s /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so /usr/lib/libsoci_mysql.so
It seem that the SOCI library search for the file /usr/lib/libsoci_mysql.so that is not in the system, buy if you make a link to the library /usr/lib/libsoci_mysql-gcc-3_0-3.0.0.so that it's in the system it works (I think debian/ubuntu makes a file name change from the original name, but it have side effects because the SOCI library search inside for the original name).
I found the error using the bash environment variable LD_DEBUG=files and running my C++ binary.
Hope it helps.