Adding libusb library to a Qt project in osx - c++

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...

Related

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.

LLVM Error : External function could not be resolved

I am reading the LLVM's Kaleidoscope tutorial (http://llvm.org/docs/tutorial/index.html).
I wanted to compile and test the language. After some compiler's errors (EngineBuilder and Module's constructor, linking libs...), the example program was built. Then, I tried the language. I got a few problems with InitializeNativeTargets, DataLayoutPass... But I managed to correct them.
Howewer, I don't manage to resolve one error. When I write extern printd(x); printd(5);, the program doesn't work : "LLVM ERROR : Program used external function 'printd' which could not be resolved".
I looked for the solution on the net. I read a lot of webpages, but nothing worked.
How can I resolve this problem ? Why LLVM can't find the external function 'printd', which is included in the program ? Thanks in advance for your answer.
The used code : https://docs.google.com/document/d/1Qb-zUGaUUIF354uFCXv1iuq8n_rjya6IHDW4WCPWN_4/edit?usp=sharing
The .pro file (Qt Creator) :
QT += core
QT -= gui
TARGET = Kaleidoscope
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -rdynamic -std=c++11 -O3
I use GCC 4.8.2, LLVM 3.5 and Qt 5.3.1 on Ubuntu 14.04 32bits.
Finally, I found a great link on the net : http://koichitamura.blogspot.fr/2011/01/since-i-went-to-held-several-weeks-ago.html.
The error came from the wrong place of the -rdynamic argument in the .pro file (-rdynamic must be after the linking options). You need this argument because (GCC man page) :
This instructs the linker to add all symbols, not only used ones, to the dynamic symbol table.
I changed the .pro file :
QT += core
QT -= gui
TARGET = Test01
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
INCLUDEPATH += /usr/include/i386-linux-gnu/c++/4.8 \
/usr/include/c++/4.8
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed -rdynamic
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -g -std=c++11 -O3
In this way, the program works with no error ! (The tutorial's example runs (http://llvm.org/docs/tutorial/LangImpl6.html#kicking-the-tires))

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

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

Qt/mingw32 undefined reference errors... unable to link a .lib

I am new to Qt and have one error I am unable to fix.
I have a bunch of windows (VS2005) static library file (.lib). And I am testing if they work well with Qt. So I took the most simple library that I have. (Called MessageBuffer).
So I added MessageBuffer.h to the main.cpp, and added the location of those file in the INCLUDEPATH of the .pro.
Until then everything seem fine, I can use the class and Qt IDE show all method and everything. So to me it look like it found the .h file.
Now I added the MessageBuffer.lib (VS2005/Debug build) in the .pro like this:
LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
I have also tried the following:
win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
LIBS += -LE:/SharedLibrary/lib -lMessageBufferd
win32:LIBS += -LE:/SharedLibrary/lib -lMessageBufferd
Here is the content of my .pro file:
QT += opengl
TARGET = SilverEye
TEMPLATE = app
INCLUDEPATH += E:/SharedLibrary/MessageBuffer
SOURCES += main.cpp \
silvereye.cpp
HEADERS += silvereye.h
FORMS += silvereye.ui
OTHER_FILES +=
win32:LIBS += E:/SharedLibrary/lib/MessageBufferd.lib
They all give me the same errors: (and I get the same even if I don't include the .lib)
Running build steps for project SilverEye...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/2009.03/mingw/bin/mingw32-make.exe -w
mingw32-make: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
C:/Qt/2009.03/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\SilverEye.exe debug/main.o debug/silvereye.o debug/moc_silvereye.o -L"c:\Qt\2009.03\qt\lib" -lopengl32 -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind E:/SharedLibrary/lib/MessageBufferd.lib -lQtOpenGLd4 -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
mingw32-make: Leaving directory `C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye'
debug/main.o: In function `Z5qMainiPPc':
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:12: undefined reference to `MessageBuffer::MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:13: undefined reference to `MessageBuffer::Append(char*, int)'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
C:/Documents and Settings/JP/My Documents/QTProjects/SilverEye/main.cpp:17: undefined reference to `MessageBuffer::~MessageBuffer()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\SilverEye.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project SilverEye
When executing build step 'Make'
Can anyone help please?
Based on the question Use libraries compiled with visual studio in an application compiled by g++ (mingw) and the MSDN forum post I can't mix VC & GCC it does not appear you can link a gcc application with visual c++ compiled libraries.
The solution would be to recompile everything with the same compiler.
The MinGW FAQ discusses this problem and offers a solution:
Create a definition file using reimp (for lib files) or pexports (for dll files).
Remove the underscore prefixes from the stdcall functions.
Use dlltool to convert the MSVC library into a MinGW library with the new definition.
That didn’t work. We finally removed the ordinals from the function names, which caused it to compile. But the program wouldn’t run because it couldn’t find the linked functions in the DLL. Finally, after consulting the MSDN documentation for definition files, we changed the build instructions:
Create a definition file using reimp.
For each stdcall function (formatted as _name#ordinal) add a line name = _name#ordinal, allowing MinGW to map its stdcall naming convention to that of MSVC.
Use dlltool to convert the MSVC library into a MinGW library with the new definition.
It worked! To compile the project you must simply:
Download and install the Qt/Windows package, which includes MinGW.
Download reimp and drop it into the MinGW/bin folder.
Download the development packages for the third-party libraries and point an environment variable to that location.
Build the project with the usual qmake/make commands.
Taken from:
http://blog.outofhanwell.com/2006/05/01/linking-msvc-libraries-with-mingw-projects/
I assume that you have used the MessageBuffer library in another application with problems. The error looks like it either cannot find the library or the MessageBuffer class is not being exported.
Have you tried putting -l on front of the library in the pro file?
win32:LIBS += -lE:/SharedLibrary/lib/MessageBufferd.lib
See my other answer. I added the other answer because I didn't want to make this answer any more messy than it already was.
Tried so far:
Not a typo, d is appended to the library
Using the lib extension is correct as seen in the output