Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

objective c - XCode dylib looking in /usr/lib

I created a dylib in XCode. I set the installtion directory to @rpath. Then I created a test project in which I am using this dylib. When I run it, I get dyld error:library not loaded....Reason: no image found.

BUT....if I put that dylib in/usr/lib folder, then it works like a charm.

So my question is, is there a way when compiling this dylib, I can specify some settings so it doesn't look in /usr/lib and simply looks where it's located (if that makes any sense)? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit: I re-read your question after writing this answer. Since you're only creating the dylib, not an executable, you should only need to use the install_name_tool -id command. I don't believe you can tell it to look for itself whereever it's located. I think you have to give it a path. In this example, it looks in the application bundle's Frameworks path, which is a pretty reasonable place to look.


You have to change both the path where your application looks for the file and the path where the library expects to find itself. Here's an example where I'm using three OpenCV libraries.

These first lines tell the application to look for the libraries in the application bundle's Frameworks folder. ($BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH is the combination of environment variables that specify the executable file.)

#Fix references in executable
install_name_tool -change lib/libopencv_core.2.3.dylib @executable_path/../Frameworks/libopencv_core.2.3.1.dylib $BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH 
install_name_tool -change lib/libopencv_imgproc.2.3.dylib @executable_path/../Frameworks/libopencv_imgproc.2.3.1.dylib $BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH 
install_name_tool -change lib/libopencv_highgui.2.3.dylib @executable_path/../Frameworks/libopencv_highgui.2.3.1.dylib $BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH

I got the first parameter (lib/libopencv_core.2.3.dylib) by previously running otool -L.

$ otool -L libopencv_imgproc.2.3.1.dylib 
libopencv_imgproc.2.3.1.dylib:
    lib/libopencv_imgproc.2.3.dylib (compatibility version 2.3.0, current version 2.3.1)
    lib/libopencv_core.2.3.dylib (compatibility version 2.3.0, current version 2.3.1)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.3)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

These next lines tell the libraries to look for themselves in the Frameworks folder.

#Fix install location in libraries
install_name_tool -id @executable_path/../Frameworks/libopencv_core.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_core.2.3.1.dylib
install_name_tool -id @executable_path/../Frameworks/libopencv_imgproc.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_imgproc.2.3.1.dylib
install_name_tool -id @executable_path/../Frameworks/libopencv_highgui.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_highgui.2.3.1.dylib

And these lines tell the libraries to look for each other in the Frameworks folder.

#Fix references in libraries
install_name_tool -change lib/libopencv_core.2.3.dylib @executable_path/../Frameworks/libopencv_core.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_imgproc.2.3.1.dylib
install_name_tool -change lib/libopencv_core.2.3.dylib @executable_path/../Frameworks/libopencv_core.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_highgui.2.3.1.dylib 
install_name_tool -change lib/libopencv_imgproc.2.3.dylib @executable_path/../Frameworks/libopencv_imgproc.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libopencv_highgui.2.3.1.dylib

This all came from my target's Run Script build phase in my Xcode project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...