Mac OSX 10.10 - Include a QT Static Library *.a into a Eclipse CDT JNI Shared Library *jnilib (Download of the projekts available)



  • Dear QT Community,

    I am a QT beginner.
    I will create a static library *.a with QT creator and make it reuseable for a other projekt.
    This QT projekt ist downloadable as *.ZIP:

    Download: http://moozoom.de/share/QT_vs_Java_JNI_over_CDT.zip

    The *.pro file have following build information:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2015-06-13T16:02:21
    #
    #-------------------------------------------------

    QT -= gui

    TARGET = QTStaticLibrary
    TEMPLATE = lib
    CONFIG += staticlib

    @CONFIG += MAC_CONFIG
    MAC_CONFIG {
    QMAKE_CXXFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.10
    QMAKE_LFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.10
    }

    QMAKE_CFLAGS_RELEASE = -Os -momit-leaf-frame-pointer

    SOURCES += qtstaticlibrary.cpp

    HEADERS += qtstaticlibrary.h
    unix {
    target.path = /usr/lib
    INSTALLS += target
    }

    I develop on Mac OS X 10.10 and create the static library (.a) with a clang (LLVM) compiler.
    My main application based on JAVA because I need a special runtime environment - based on java.
    I used Eclipse LUNA CDT for developing this application based on java jni for including a C++ shared library (
    .jnilib) (cpp compiler) over a CDT Eclipse plugin. A clang compiler is not available for the eclipse mac osx version.

    Tutorial JNI:
    https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

    Include c++ libraries by eclipse cdt:
    http://stackoverflow.com/questions/8332460/how-do-i-include-a-statically-linked-library-in-my-eclipse-c-project

    The makefile for my helloJNI project:

    # Define a variable for classpath
    CLASS_PATH = ../bin

    # Define a virtual path for .class in the bin directory
    vpath %.class $(CLASS_PATH)

    # Modify the "makefile" as follows to generate the shared library "libHello.jnilib" for Mac.

    all : libHello.jnilib

    # $@ matches the target, $< matches the first dependancy
    # The -lstdc++ flag tells the linker to include the C++ Standard Library
    libHello.jnilib : HelloQT2JNI.o
    g++ -lstdc++ -v -Wl -shared -o $@ $< libQTStaticLibrary.a

    # $@ matches the target, $< matches the first dependancy
    # -c flag to tell the compiler to skip the link stage: "ld: symbol(s) not found for architecture x86_64"
    HelloQT2JNI.o : HelloQT2JNI.cpp HelloQT2JNI.h
    gcc -I"/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/include" -I"/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/include/darwin" -c $< -o $@

    # $* matches the target filename without the extension
    HelloQT2JNI.h : HelloQT2JNI.class
    javah -classpath $(CLASS_PATH) $*

    clean :
    rm HelloQT2JNI.h HelloQT2JNI.o libHello.jnilib

    Call the func hello() in my qt static library in my jni-class:

    #include <jni.h>
    #include <iostream>
    #include "HelloQT2JNI.h"

    #include "qtstaticlibrary.h"

    using namespace std;

    JNIEXPORT void JNICALL Java_HelloQT2JNI_sayHello__(JNIEnv *, jobject)
    {
    cout << "Hello World!" << endl;

    QTStaticLibrary hello = QTStaticLibrary();
    hello.hello();

    return;
    }

    How I can include my static library *.a (clang compiled) into my shared library *.jnilib (cpp compiler) over my makefile? I will use my qt static library in my java project over java jni.

    I received following CDT Build Console error message:

    make all
    javah -classpath ../bin HelloQT2JNI
    g++ -lstdc++ -v -Wl -shared -o libHello.jnilib HelloQT2JNI.o libQTStaticLibrary.a
    Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
    Target: x86_64-apple-darwin14.3.0
    Thread model: posix
    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -dylib -arch x86_64 -macosx_version_min 10.10.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -o libHello.jnilib -lc++ HelloQT2JNI.o libQTStaticLibrary.a -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/lib/darwin/libclang_rt.osx.a
    Undefined symbols for architecture x86_64:
    "std::basic_ios<char, std::char_traits<char> >::widen(char) const", referenced from:
    QTStaticLibrary::hello() const in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::ostream::put(char)", referenced from:
    QTStaticLibrary::hello() const in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::ostream::flush()", referenced from:
    QTStaticLibrary::hello() const in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::ios_base::Init::Init()", referenced from:
    __GLOBAL__sub_I_qtstaticlibrary.cpp in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::ios_base::Init::~Init()", referenced from:
    __GLOBAL__sub_I_qtstaticlibrary.cpp in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)", referenced from:
    QTStaticLibrary::hello() const in libQTStaticLibrary.a(qtstaticlibrary.o)
    "std::cout", referenced from:
    QTStaticLibrary::hello() const in libQTStaticLibrary.a(qtstaticlibrary.o)
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [libHello.jnilib] Error 1

    The qt project and the clipse LUNA cdt project is downloadable as *.ZIP:

    Download: http://moozoom.de/share/QT_vs_Java_JNI_over_CDT.zip

    Actually the jni project is runable. The qt files can u find in the dir „jni/<qtstaticlibrary.h, libQTStaticLibrary.a>“.

    Please help my. I am very frustrated. QT is a very interesting framework for using in java over jni.

    Thank u in advanced :-)



  • @moozoom
    I run in the same paradigm when I try to create a static linked library with Qt targeting Android and latter to use it with JINI on Android Studio.
    Reading you post I see you have almost the same problem like me with one difference.
    To me are working as far I use only std library
    My problem appears when I use QString for example in the library.
    Looks the static build does not statically includes the Qt framework dependencies.
    Finally did you find a solution? Me I run with this issue past over one month.
    Thanks



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.