简体   繁体   English

在MacOS QT项目中链接Dylib

[英]Link dylib in macOs QT project

I tried a simple dylib in macOS and compiled with g++. 我在macOS中尝试了一个简单的dylib,并使用g ++进行了编译。 I made a small sample to test the lib, it works perfect. 我做了一个小样本来测试lib,它可以完美工作。 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 现在,我制作了一个简单的QT应用程序,链接了lib,并将标头添加到mainwindow.cpp中,但总是收到失败消息。原因:找不到图像

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. 因为我进行了所有更改,所以在所有情况下我都阅读了该文件,并且还在所有需要的文件夹中复制了lib,例如项目文件夹,build文件夹。 Nothing helps. 没有任何帮助。

I think that QT cannot work with the library and I missed some needed code, like for initialize or export. 我认为QT无法使用该库,并且我错过了一些所需的代码,例如用于初始化或导出。 So that the problem is more inside the dylib and not in QT or Paths values. 因此问题出在dylib内部,而不是QT或Paths值。

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. 我试图将lib复制到build文件夹,也复制到可执行文件夹。

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 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 使用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 用g ++ -std = c ++ 0x test.cpp -L./ -lmylib编译

Actual result is that the sample runs perfect. 实际结果是样本运行完美。 But in QT I got: 但是在QT中,我得到了:

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. dyld:未加载库:libmylib.dylib引用自:/Users/ingoforster/Documents/Development/Playground/build-TestGround-Desktop_Qt_5_9_1_clang_64bit-Debug/TestGround.app/Contents/MacOS/TestGround原因:找不到图像10:09:05 :程序意外完成。 10:09:05: The process was ended forcefully. 10:09:05:该过程被强制结束。

/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. /Users/ingoforster/Documents/Development/Playground/build-TestGround-Desktop_Qt_5_9_1_clang_64bit-Debug/TestGround.app/Contents/MacOS/TestGround似乎已编译链接到您的库代码,但可能尚未正式安装到可执行文件。 macdeployqt usually deals with this issue. macdeployqt通常处理此问题。 You may also do this manually with otool and install_name_tool : 您也可以使用otoolinstall_name_tool手动执行此操作:

otool -L /path/to/executable

This will list the installed paths to the dylibs in use. 这将列出正在使用的dylib的安装路径。 You will see your dylib, that is the "/old/path/to/libmylib.dylib" 您将看到您的dylib,即“ /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. 通常,在.app ,dylib安装在.app文件夹内的子目录中。

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您可以指向相对于可执行路径的dylib,以使应用程序具有可移植性。 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会自己做:

QTCreator Pro file have to contain: QTCreator Pro文件必须包含:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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