简体   繁体   English

在C ++ for OS X中分发动态库

[英]distributing dynamic libraries in C++ for OS X

I have written a program that crucially depends on OpenCV. 我写了一个关键取决于OpenCV的程序。 I have compiled OpenCV from the latest stable version and would now like to distribute this program, to ensure that people do not need to compile OpenCV by themselves. 我已经从最新的稳定版本编译了OpenCV,现在想分发这个程序,以确保人们不需要自己编译OpenCV。 The program itself is compiled using g++. 程序本身使用g ++编译。

I tried a number of things to resolve this issue: 我尝试了很多方法来解决这个问题:

  1. compiling OpenCV as static libraries works, but I cannot statically link my program to these libraries as some libraries in OS X cannot be statically linked; 编译OpenCV作为静态库工作,但我不能静态地将我的程序链接到这些库,因为OS X中的某些库不能静态链接; I found this information here: Mixed static and dynamic link on Mac OS 我在这里找到了这些信息: Mac OS上的混合静态和动态链接

  2. I tried to move to XCode, where I used it as a command line project. 我试图转移到XCode,在那里我将它用作命令行项目。 I set the search path as well as the installation location to @rpath and added a build phase to copy the files to Executables. 我将搜索路径以及安装位置设置为@rpath,并添加了构建阶段以将文件复制到可执行文件。 I verified with otool whether the compiled file has the correct links but it still fails on a clean machine (one that does not have the OpenCV libraries) with a "Library not found @rpath/libopencv..." error. 我用otool验证了编译后的文件是否具有正确的链接,但是在一台干净的机器(没有OpenCV库的机器)上仍然出现故障,并且“找不到库@rpath / libopencv ...”错误。 (Yes, all macs use Lion, so @rpath should work). (是的,所有的mac都使用Lion,所以@rpath应该可以工作)。 This answer was found here: Xcode keeps searching dylib at wrong path 这个答案在这里找到: Xcode继续在错误的路径上搜索dylib

the result I get from runnig otool -L on both the compiled file and the library is @rpath/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) 我从runnig otool -L获得的编译文件和库的结果是@rpath/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

So ... what am I doing wrong? 那么......我做错了什么? And how can I distribute these libraries together with my program? 我如何将这些库与我的程序一起分发?

You should use Frameworks ( SO question on frameworks ). 您应该使用框架(框架上的SO问题 )。 Combine all the stuff your app uses in one folder and then link to the framework in your XCode project. 将您的应用程序使用的所有内容合并到一个文件夹中,然后链接到XCode项目中的框架。

I've had to do that recently to run JavaCV in an applet. 我最近必须这样做才能在applet中运行JavaCV。 This command should get the job done: 这个命令应该完成工作:

BADPATH=/opt/local/lib  # in the case of MacPorts, change as necessary
for f in libopencv*2.4.dylib; do install_name_tool $f -id @rpath/$f \
    -add_rpath /opt/local/lib/ -add_rpath /usr/local/lib/ -add_rpath @loader_path/. \
    -change $BADPATH/libopencv_core.2.4.dylib @rpath/libopencv_core.2.4.dylib \
    -change $BADPATH/libopencv_calib3d.2.4.dylib @rpath/libopencv_calib3d.2.4.dylib \
    -change $BADPATH/libopencv_features2d.2.4.dylib @rpath/libopencv_features2d.2.4.dylib \
    -change $BADPATH/libopencv_flann.2.4.dylib @rpath/libopencv_flann.2.4.dylib \
    -change $BADPATH/libopencv_gpu.2.4.dylib @rpath/libopencv_gpu.2.4.dylib \
    -change $BADPATH/libopencv_highgui.2.4.dylib @rpath/libopencv_highgui.2.4.dylib \
    -change $BADPATH/libopencv_imgproc.2.4.dylib @rpath/libopencv_imgproc.2.4.dylib \
    -change $BADPATH/libopencv_legacy.2.4.dylib @rpath/libopencv_legacy.2.4.dylib \
    -change $BADPATH/libopencv_ml.2.4.dylib @rpath/libopencv_ml.2.4.dylib \
    -change $BADPATH/libopencv_nonfree.2.4.dylib @rpath/libopencv_nonfree.2.4.dylib \
    -change $BADPATH/libopencv_objdetect.2.4.dylib @rpath/libopencv_objdetect.2.4.dylib \
    -change $BADPATH/libopencv_photo.2.4.dylib @rpath/libopencv_photo.2.4.dylib \
    -change $BADPATH/libopencv_video.2.4.dylib @rpath/libopencv_video.2.4.dylib; done

And relink your software with those and the desired "-rpath" option. 并使用这些和所需的“-rpath”选项重新链接您的软件。

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

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