Dynamic Libraries in Xcode

I am trying to create a mac application in Xcode that has some implementation in a dynamic library.

I added a new target (dynamic library) to my Xcode cocoa project and after a battle battle with frameworks and precompiled headers, compile dynlib and successfully execute the debugger.

When run offline, however, it is obvious that dynlib is in the wrong place. "Library not loaded: /usr/local/lib/testlib.dynlib." On Windows - my more ordinary platform - Dll can be placed in the same folder as exe, and anywhere on the path to the system.

I would prefer my application to look for its dynlib somewhere in its application package (and some documents seem to confirm this as the right approach), but I don't know how to do it.

Is it possible to configure the project in Xcode so that dylib is actually copied to the application package in the place where the application will look for it?


It seems I don't need to use otool to reset the search path. If I edit the Target Info -> Build -> Deployment -> Installation directory, I can change, from within Xcode, the path that dyld will look for for dylib. By default, for the new dynamic library project, the path was set to /usr/local/lib. Which I changed to ./- otool confirms that. / Is now where dyld will look for this particular dynamic module. Unfortunately. / does not seem to reference any directory in my application :(

, ( ): 1. Target Info → ... → Installation Directory 2. XCode dylib ?

+5
3

. . XCode.

, Installed Directory : : @executable_path/../Frameworks

"Copy Files" , Frameworks.

Simple. .

+6

, .

, , . otool ( script).

: Xcode.

.

+1

, , openCV dylib , " :....".

Dylibs , , , (macports /opt/local/lib/ ). , . , dylib "libopencv_core.2.4.8.dylib", dylib, :

otool -L libopencv_core.2.4.8.dylib

:

libopencv_core.2.4.8.dylib:
/opt/local/lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.8)
/opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

1- : , .

2- : dylib. (macports /opt/local/lib/ )

3- : dylib, , OSX. (macports /opt/local/lib/, , , , , " " )

4 5 - 4 5: dylib macOSX. .

, :

1- dylib ( dylib, ), dylib "Frameworks", mac , dylibs "frameworks" : , , :

@executable_path/../Frameworks/nameOFTheDylib.dylib

( !)

2- xCode dylib, dylib /opt/local/lib, , : ( , , 500 ), , , , ( 4 5) dylib, 4 5 .

Y, , :

a) :

1- /opt/local/lib .

2- lib, .dylib.

3- script lib ( dylib), :

( textEdit theNameYouPrefer.sh)

#!/bin/bash
echo empieza el script

EXECFILE=${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}
OLDLIBPATH="/opt/local/lib/"
NEWLIBPATH="@executable_path/../Frameworks/"
ACTUALPATH=`pwd`
# space separated list of libraries
for TARGET in ${ACTUALPATH}/* ; do
TARGET=$(basename $TARGET)
    if [ ${TARGET: -6} == ".dylib" ]
    then
        echo otool -DX ${TARGET}
        TARGETID=`otool -DX ${TARGET}`
        echo ${TARGETID}

        echo install_name_tool -id ${NEWLIBPATH}${TARGET} ${TARGET}
        echo `install_name_tool -id ${NEWLIBPATH}${TARGET} ${TARGET}`

        for DYLIB in ${ACTUALPATH}/* ; do
        DYLIB=$(basename $DYLIB)
            if [ ${DYLIB: -6} == ".dylib" ]
            then
                echo install_name_tool -change ${TARGETID} ${NEWLIBPATH}${TARGET} ${DYLIB}
                echo `install_name_tool -change ${TARGETID} ${NEWLIBPATH}${TARGET} ${DYLIB}`
            else
                if [ ${DYLIB: -3} == ".sh" ]
                then
                    echo soyYo
                else
                    echo mkdir ${ACTUALPATH}/eliminadas/
                    echo `mkdir ${ACTUALPATH}/eliminadas/`
                    echo mv ${DYLIB} ${ACTUALPATH}/eliminadas
                    echo `mv ${TARGET} ${ACTUALPATH}/eliminadas`
                fi
            fi
        done
    else
if [ ${TARGET: -3} == ".sh" ] || ![ -h ${TARGET} ]
        then
            echo noMeElimino
        else
            echo mkdir ${ACTUALPATH}/eliminadas/
            echo `mkdir ${ACTUALPATH}/../eliminadas/`
            echo mv ${TARGET} ${ACTUALPATH}/eliminadas/${TARGET}
            echo `mv ${TARGET} ${ACTUALPATH}/../eliminadas`
        fi
    fi
done

echo finaliza el script

4- :

cd Desktop/lib
sudo ./theNameYouPrefer.sh

sudo, dylib .

dylib. dylib :

otool -L theDylibYouPrefer.dylib

b) dylib, lib:

1- lib lib2: lib ===== > lib2

2- ( , , exampleFolder) , NEXT TO lib2.

3- exampleFolder dylib, , , , : libopencv_core.2.4.8.dylib, libopencv_highgui.2.4.8.dylib, libopencv_imgproc.2.4.8.dylib, libopencv_objdetect.2.4.8.dylib

4- Script: exampleFolder, dylib. , :

#!/bin/bash
echo empieza el script

EXECFILE=${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}
OLDLIBPATH="/opt/local/lib/"
NEWLIBPATH="@executable_path/../Frameworks/"
ACTUALPATH=`pwd`
# space separated list of libraries
for TARGET in ${ACTUALPATH}/* ; do
TARGET=$(basename $TARGET)
    if [ ${TARGET: -6} == ".dylib" ]
    then
        echo otool -DX ${TARGET}
        TARGETID=`otool -L ${TARGET}`
echo ${TARGETID}

echo :::::::::::::::::::::::::::

ELIM=`echo ${TARGETID} | sed 's/([^)]*)/UUU/g'`
echo $ELIM
#echo ${TARGETID} | sed 's/([^)]*)/UUU/g'
ELIM=`echo $(awk '{gsub("@executable_path/../Frameworks/", "");print}' <<< ${ELIM})`
echo $ELIM

ELIM=`echo $(awk '{gsub(" UUU", "");print}' <<< ${ELIM})`
echo $ELIM

ELIM=`echo $(awk '{gsub(":", "");print}' <<< ${ELIM})`
echo $ELIM

IFS=' ' read -a ARRAY <<< $ELIM

for element in ${ARRAY[@]}
do
    echo $element
    if [[ ${element} == */* ]]
    then
        echo "NO FRAMEWORK";
    else
        echo `mkdir ${ACTUALPATH}/../copy`
        echo `cp ${ACTUALPATH}/../lib2/${element} ${ACTUALPATH}/${element}`
    fi
done


    else
if [ ${TARGET: -3} == ".sh" ] || ![ -h ${TARGET} ]
        then
            echo noMeElimino
        else
            echo mkdir ${ACTUALPATH}/eliminadas/
            echo `mkdir ${ACTUALPATH}/../eliminadas/`
            echo mv ${TARGET} ${ACTUALPATH}/eliminadas/${TARGET}
            echo `mv ${TARGET} ${ACTUALPATH}/../eliminadas`
        fi
    fi
done

echo finaliza el script

5 script , , exampleFolder .

6- , , - Folder! 61 . (, , 4 dylib)

7- , , "Frameworks" xCode. , " " " " : "editor" = > " " = > " " " " PHASE CLICK IN PLUS , , "". , , Frameworks.

, .

0

All Articles