简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM