PDA

View Full Version : Using 3rd part static library with QT Creator in Win7



daynen
9th November 2014, 22:11
I am new to QT and i have encountered the problem for which i cant find the solution.. so i want to ask for Your advices ;)

So, currently i am writing the application for android with QT creator in win7. When it is simple (aka 'hello world') it compiles and runs on the connected android tablet successfully. The problem begins when i try to use 'libusb' library - qt creator shows lots of linking errors and fails to build (more details ahead).



I have downloaded latest libusb-1.0.19.7z (from here (http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.19/)) with .a and .h files. I have just copy/paste Header (libusb.h) and static lib (libusb-1.0.a) files (for MinGW32) to my project directory and added to project using qt creator.


after adding library, .pro file is like this (skipping basic code lines)...

...
unix:!macx: LIBS += -L$$PWD/ -lusb-1.0

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/

unix:!macx: PRE_TARGETDEPS += $$PWD/libusb-1.0.a


When i try to build project, it shows this:


23:55:37: Running steps for project TestAndroid...
23:55:37: Configuration unchanged, skipping qmake step.
23:55:37: Starting: "C:\Qt\Tools\mingw482_32\bin\mingw32-make.exe"
C:\Qt\5.3\android_armv7\bin\qmake.exe -spec android-g++ CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ..\TestAndroid\TestAndroid.pro
C:\android\ndk\android-ndk-r10b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ --sysroot=C:\android\ndk\android-ndk-r10b/platforms/android-9/arch-arm/ -Wl,-soname,libTestAndroid.so -Wl,--no-undefined -Wl,-z,noexecstack -shared -o libTestAndroid.so main.obj testclassAndroid.obj hid.obj qrc_qml.obj moc_testclass.obj -LC:\android\ndk\android-ndk-r10b/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a -LC:\android\ndk\android-ndk-r10b/platforms/android-9/arch-arm//usr/lib -LC:/Users/dainius/Desktop/qt/projects/TestAndroid/ -lusb-1.0 -LC:/Qt/5.3/android_armv7/lib -lQt5Quick -Lc:\Utils\android\ndk/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a -Lc:\Utils\android\ndk/platforms/android-9/arch-arm//usr/lib -LC:\Utils\icu32_51_1_mingw482\lib -LC:\utils\postgresql\pgsql\lib -LC:\utils\mysql\mysql\lib -LC:\Utils\pgsql\lib -LC:\temp\opensll-android-master\openssl-android-master\lib -LC:\Qt\5.3\android_armv7/lib -lQt5Qml -lQt5Widgets -lQt5Network -lQt5Gui -lQt5Core -lGLESv2 -lgnustl_shared -llog -lz -lm -ldl -lc -lgcc
..\TestAndroid/libusb.h:1792: error: undefined reference to 'libusb_control_transfer'
..\TestAndroid/hid.c:484: error: undefined reference to 'libusb_get_bus_number'
..\TestAndroid/hid.c:485: error: undefined reference to 'libusb_get_device_address'
..\TestAndroid/hid.c:499: error: undefined reference to 'libusb_init'
..\TestAndroid/hid.c:514: error: undefined reference to 'libusb_exit'

and more undefined references...



btw, readme.txt file from libusb-1.0.19.7z tells this:

MinGW/cygwin
- Copy libusb.h, from include/libusb-1.0/ to your default include directory,
and copy the MinGW32/ or MinGW64/ .a files to your default library directory.
Or, if you don't want to use the default locations, make sure that you feed
the relevant -I and -L options to the compiler.
- Add the '-lusb-1.0' linker option when compiling.



So what i am doing wrong here or am i missing something??
Thanks in advance.


More info:
I am using QT Creator 3.2.2, QT 5.3.2 (msvc 2010, 32bit). I have tried on WIN7 32bit and 64bit

Infinity
9th November 2014, 22:46
I am using QT Creator 3.2.2, QT 5.3.2 (msvc 2010, 32bit)
I don't think that you are using MSVC when compiling your C++ application to run on Android. Normally you use the appropriate compiler for your architecture which comes with the Android NDK to compile your C++ application as well as the libraries you need to use.


I have just copy/paste Header (libusb.h) and static lib (libusb-1.0.a) files (for MinGW32) to my project directory and added to project using qt creator.
You can not link against libraries compiled with MinGW (for AMD64/x86 Windows platform!). I would compile libusb from the source using the appropriate compiler from the Android NDK, see: https://github.com/libusb/libusb/tree/master/android

daynen
11th November 2014, 10:24
thanks for the answer.

i used compiler from Android NDK and built 'libusb' for Android (i have downloaded the source files from the github). In result i got libs and obj folders with .so files in it.

So, i updated my .pro file accordingly...



unix:!macx: LIBS += -L$$PWD/../../libusb-master/android/libs/armeabi-v7a/ -lusb1.0

INCLUDEPATH += $$PWD/../../libusb-master/android/libs/armeabi-v7a
DEPENDPATH += $$PWD/../../libusb-master/android/libs/armeabi-v7a

...and it finally compiled successfully.

Unfortunately, my program fails to start on Android tablet and QT creator shows the following:


Starting remote process.

Unable to start "org.qtproject.example.HidManager".

Checked the logcat and found:


E/dalvikvm(13795): dlopen("/data/app-lib/org.qtproject.example.HidManager-1/libH
idManager.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635):
could not load library "libusb1.0.so" needed by "libHidManager.so"; caused by lo
ad_library(linker.cpp:745): library "libusb1.0.so" not found


What could be the suggestions what i am doing wrong here (again)?

BTW, when removing the usage of libusb library from the project, application on android runs successfully.

Infinity
11th November 2014, 17:32
Since you're linking dynamically against libusb you need to deploy the library with your application. You can use ANDROID_EXTRA_LIBS to achieve that.
Have a look at the documentation: http://qt-project.org/doc/qt-5/deployment-android.html