Linux QT Creator C++: How to link with $$HOME? - c++

I start a programm which includes a library (IDA) in
/home/MYUSERNAME/EB/IDA/Earlybite/
The library IDA has two folders:
/home/MYUSERNAME/EB/IDA/IDA/Includes/ (for h-file)
/home/MYUSERNAME/EB/IDA/IDA/Libs/ (for so-files)
This is the linking which works:
LIBS += -L$$PWD/../IDA/Libs/ -Wl,-rpath=$$PWD/../IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$PWD/../IDA/Includes/
The problem is that PWD only shows the path in which Earlybite starts. In this case
/home/MYUSERNAME/EB/IDA/Earlybite/, but if the programm starts e.g. in /home/MYUSERNAME/EB/IDA/ ...the linking will not work.
So I tried to link with the HOME environment variable. E.g.
LIBS += -L$$HOME/EB/IDA/IDA/Libs/ -Wl,-rpath=$$HOME/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += $$HOME/EB/IDA/IDA/Includes/
But this do not work.
I also tried
LIBS += -L/home/$$USER/EB/IDA/IDA/Libs/ -Wl,-rpath=/home/$$USER/EB/IDA/IDA/Libs/ -lIDA -ldl -lpthread -lrt
INCLUDEPATH += /home/$$USER/EB/IDA/IDA/Includes/
But this do not work, too.
(I've also tried every try with a single $ and with two $ symbols...)

Edit: I just remembered that you can use $$(HOME) which will read environment variable during qmake execution, so you just need to add () around HOME. Using $_PRO_FILE_PWD_ is still a good practice, but the last options is a workaround rather than straightforward solution I think.
You can try and use $$_PRO_FILE_PWD_, this variable points to .pro file location, and create path relative to project file. Also check qmake Variables for additional references.
Or you can do:
HOME = $$system(echo $HOME)
message($$HOME)
LIBS += -L$$HOME ...
About $$system link

Related

Link dylib in macOs QT project

I tried a simple dylib in macOS and compiled with g++. I made a small sample to test the lib, it works perfect.
Now I made a simple QT app, linked the lib and added the header to the mainwindow.cpp and I always got a fail message Reason: image not found
I read the web and also some other cases here but all is basing on paths.I think it is is not a path problem. Because I made all changes I read in all the cases and also copied the lib in all needed folders, like project folder, build folder. Nothing helps.
I think that QT cannot work with the library and I missed some needed code, like for initialize or export. So that the problem is more inside the dylib and not in QT or Paths values.
Maybe someone can help me here out?
I have tried:
INCLUDEPATH += $$PWD/mylib
DEPENDPATH += $$PWD/mylib
macx: LIBS += -L$$PWD/mylib/ -lmylib
I tried to copy the lib to the build folder and also to the executable folder.
Also edited:
/⁨Users⁩/⁨ingoforster⁩/Documents⁩/Development⁩/Playground⁩/TestGround⁩/mylib:/Users/ingoforster/Qt/5.9.1/clang_64/lib
in Project settings DYLD_LIBRARY_PATH
Set also to DYLD_FRAMEWORK_PATH
Cpp
#include "mylib.hpp"
char *mMessage(void) {
return "Ein sonniger Tag";
}
header
#include <stdio.h>
#include <iostream>
using namespace std;
char *mMessage(void);
compiled with
g++ -std=c++0x --verbose -dynamiclib -o libmylib.dylib mylib.cpp
Sample
#include "mylib.hpp"
int main(void){
char* Ingo = mMessage();
std::cout << mMessage();
}
compiled with
g++ -std=c++0x test.cpp -L./ -lmylib
Actual result is that the sample runs perfect.
But in QT I got:
dyld: Library not loaded: libmylib.dylib
Referenced from: /Users/ingoforster/Documents/Development/Playground/build-TestGround-Desktop_Qt_5_9_1_clang_64bit-Debug/TestGround.app/Contents/MacOS/TestGround
Reason: image not found
10:09:05: The program has unexpectedly finished.
10:09:05: The process was ended forcefully.
/Users/ingoforster/Documents/Development/Playground/build-TestGround-Desktop_Qt_5_9_1_clang_64bit-Debug/TestGround.app/Contents/MacOS/TestGround appears to be compile-linked to your library code but perhaps has not been formally installed to the executable. macdeployqt usually deals with this issue.
You may also do this manually with otool and install_name_tool:
otool -L /path/to/executable
This will list the installed paths to the dylibs in use. You will see your dylib, that is the "/old/path/to/libmylib.dylib"
install_name_tool -change /old/path/to/libmylib.dylib /new/path/to/libmylib.dylib /path/to/executable
Usually, in an .app, the dylib is installed to a subdirectory within the .app folder.
Application.app/
Contents/
MacOS/
executable
Frameworks/
libmylib.dylib
With install_name_tool you can point to the dylib relative to the executable path to make the app portable.
install_name_tool -change /old/path/to/libmylib.dylib #executable_path/../Frameworks/libmylib.dylib /path/to/executable
After some hard investigation and running into the wrong direction with the given answers here I found out, that QTCreator will do all its own:
QTCreator Pro file have to contain:
macx: LIBS += -L$$PWD/mylib/ -lmylib
INCLUDEPATH += $$PWD/mylib
DEPENDPATH += $$PWD/mylib
MediaFiles.files += mylib/libmylib.dylib
MediaFiles.path = Contents/MacOS
QMAKE_BUNDLE_DATA += MediaFiles

ft2build.h: No such file or directory - Freetype 2.6

I am trying to build from Linux cause I decided to start using Jenkins, my personal work but, even with freetype installed: v.2.6.3, it doens't recognize this ft2build.h.
This is the QT Pro with freetype loaded. I'm now running on UBuntu 16.04:
linux {
message("Build for Linux")
DEFINES += LINUX
DEFINES += BOOST_LOG_DYN_LINK
LIBS += -lGLU
LIBS += -lfreetype
LIBS += -L/usr/lib/x86_64-linux-gnu/-libboost_timer.so -libboost_log.so -libboost_log_setup.so -libboost_system.so -libboost_thread.so -libboost_filesystem.so
}
So i tried to change also the lib pointer doing manually as:
LIBS *= -L/usr/local/lib/ -lfreetype
but still nothing going properly. The error I get is from lGLU
In file included from displays/display.cpp:3:0:
displays/./../oglft/oglft.h:50:22: fatal error: ft2build.h: No such file or directory
compilation terminated.
Makefile:1093: recipe for target 'display.o' failed
I do always run 'qmake make clean' then /usr/lib/x86_64-linux-gnu/qt5/bin/qmake && make && make check to be sure it starts properly. Can someone help me solving the bug?
I've checked where freetype lib is located by doing:
pkg-config --cflags --libs freetype2
and this is what I get
-I/usr/local/include/freetype2 -L/usr/local/lib -lfreetype
PROBLEM SOLVED!
I have had to set FREETYPE in the environment variables as BOOST too and everything is now working properly!
Here is how is mine:
export BOOST=/var/lib/jenkins/workspace/boost_1_59_0
export FREETYPE=/var/lib/jenkins/workspace/freetype-2.6.3
I preferred to move those two libs under Jenkins to avoid permissions problems.
;-)

Qt qmake and DISTFILES

So I am learning to program in OpenGL 3.3 and I am using qtcreator as my IDE with qmake as the compiler. Everything is fine except I have to read 2 files (fragmentshader.frag and vertexshader.vert) using ifstream.
I have included those 2 files in the ".pro" like this:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp \
shaders.cpp
LIBS += -lGLEW -lglfw -lGL -lX11 -lpthread -lXrandr -lXi
DISTFILES += \
vertexshader.vert \
fragmentshader.frag
HEADERS += \
shaders.h
and in the code I try to directly read "vertexshader.vert" and "fragmentshader.frag".
My question is: How do I include these files in my application without having to specify an absolute path?
I had the same problem, it comes from the fact Qt is running files from another directory (by default from the build one).
The Fix
So you have two options:
change the Working directory in Projects->Build & Run->Run to %{sourceDir}.
change the Build directory in Projects->Build & Run->Build to . or leave it blank.
Explanations
The first option run your program (.exe on Windows) from the sourceDir, where your project sits, so path will remain the same relatively.
The second option place all your build files (ie: Makefile, *.o and ".exe") in your project directory (they won't appear in the project view unless your Add them), I prefer this way because now your project directory contains everything related to it.
Choose the one you prefer the most, hope it helps.

Adding libusb library to a Qt project in osx

I've been attempting for the past 16 hours to attach the libusb library to a Qt project without much success. I would appreciate any input on the matter, it's getting frustrating.
The .pro file is this:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
Source code:
#include <iostream>
#include <libusb.h>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Compiler output:
13:01:50: Running steps for project lallala...
13:01:50: Configuration unchanged, skipping qmake step.
13:01:50: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
/Users/MAXIMUS/Qt5.0.0/5.0.0/clang_64/bin/qmake -spec macx-g++42 CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../lallala/lallala.pro
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h>
/bin/sh: -c: line 0: syntax error near unexpected token `newline'
/bin/sh: -c: line 0: `g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h> '
make: *** [lallala] Error 2
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
13:01:50: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project lallala (kit: Desktop Qt 5.0.0 clang 64bit (SDK))
When executing step 'Make'
Forming my comment into a proper answer; this is not the correct syntax to use:
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
The proper one would be this:
LIBS += -L/usr/local/lib -lusb-1.0
or
LIBS += -l/full/path/to/libusb-1.0.a
You can drop the second LIBS line in your initial attempt because you have already specified the path in the former, and putting an "include" statement in there would not be reasonable anyhow. So, this is what you could write for your complete .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -lusb-1.0
This is not Qt specific, just generic linkage issue: -lfoo extends to $(prefix)foo$(suffix), where the prefix and suffix are figured out automatically based on the platform. That is, the prefix would be lib in your case, and suffix would be either .a or .so on Unix, probably .dylib on Mac, etc.
You may wish to look into pkg-config support if it is possible to establish. In that case, you would write something like this what we did in QtSerialPort:
CONFIG += link_pkgconfig
PKGCONFIG += libudev
Yet another option is to add the GUI through the QtCreator IDE or similar IDE that you may be using. There is an option usually in the "Linker" section to add a library. Here are two screenshots from my QtCreator:
Click on the project name on the left in the project source tree navigator, and select Add Library. Then the first screenshot will come up, and you can select the external option, and then you can see the second.
It is needless to say that you would need to run qmake after these changes to generate the corresponding Makefile on your desired platform.
Syntax is the following:
-L%LIBRARY_PATH% to make a specific path visible and
-l%LIBRARY_NAME% to link a specific library that is located in a visible path
so I guess this should work (I don't think you need the .a extension):
LIBS += -L/usr/local/lib -llibusb-1.0
and I have no idea what this would do:
LIBS += -L
so I guess I'd remove it.
Once fixed run qmake then build...
Hope it helps...

Linking freeglut with Qt Creator in Linux

I currently run Arch Linux on my laptop and was hoping to know why Qt Creator isn't finding my glut library (which exists on my system).
My setup qmake file looks as follows:
...
/*sources and headers above*/
QT += opengl
LIBS += -lfreeglut
INCLUDEPATH += -L/usr/lib/
And when I run a locate glut, I get the following:
/usr/include/kwinglutils.h
/usr/include/kwinglutils_funcs.h
/usr/include/GL/freeglut.h
/usr/include/GL/freeglut_ext.h
/usr/include/GL/freeglut_std.h
/usr/include/GL/glut.h
/usr/lib/libglut.a
/usr/lib/libglut.so
/usr/lib/libglut.so.3
/usr/lib/libglut.so.3.9.0
/usr/share/avogadro/fragments/amino_acids/D-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/D-glutamine.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamic_acid.cml
/usr/share/avogadro/fragments/amino_acids/L-glutamine.cml
/usr/share/licenses/freeglut
/usr/share/licenses/freeglut/LICENSE
/var/lib/pacman/local/freeglut-2.6.0-1
/var/lib/pacman/local/freeglut-2.6.0-1/changelog
/var/lib/pacman/local/freeglut-2.6.0-1/desc
/var/lib/pacman/local/freeglut-2.6.0-1/files
Note that I have tried -lglut32 in my qmake file as well.
What could I be missing here?
When you specify -lfoobar in your .pro file (or with gcc in general), you're directing the compiler to search for the library libfoobar.a. Judging from your locate output it looks like you want:
LIBS += -lglut
Oh, silly me. I just realized that the correct lib to add was just -lglut and not -lglut32. This is because there exists libglut.so, and not libglut32.so.