iphone dylib program

本文通过一个小例子详细介绍了如何在MacOSX上创建并使用动态链接库(dylib)。首先,创建了一个名为libmylib.dylib的动态库,并将其链接到调用模块callmymod中。接着,通过设置环境变量DYLD_LIBRARY_PATH和DYLD_PRINT_LIBRARIES来控制动态链接器dyld的行为,以确保正确加载所需的库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dynamic linking on Mac OS X, a tiny example
Keywords: macosx, dylib, dyld, dynamic-linker

Steps:
1) create a library libmylib.dylib containing mymod.o
2) compile and link a "callmymod" whihc calls it
3) call mymod from callmymod, using DYLD_LIBRARY_PATH and DYLD_PRINT_LIBRARIES

Problem: you "just" want to create a library for other modules to use. However there's a daunting pile of programs -- gcc, ld, macosx libtool, dyld -- with zillions of options, some well-rotted compost, and differences between MacOSX and Linux. There are tons of man pages (I count 7679 + 1358 + 228 + 226 lines in 10.4.11 ppc) but not much in the way of examples, or programs with a "tell me what you're doing" mode.

(The most important thing in understanding is to make a simplified OVERVIEW for yourself: draw some pictures, run some small examples, explain it to someone else).

 

 


 

Step 1, create libmylib.dylib --

mymod.c:
   
#include <stdio.h>
   
void mymod( int x )
   
{
        printf
( "mymod: %d/n", x );
   
}
gcc
-c mymod.c  # -> mymod.o
gcc
-dynamiclib -current_version 1.0  mymod.o  -o libmylib.dylib
   
# calls libtool with many options -- see man libtool
   
# -compatibility_version is used by dyld, see also cmpdylib

file libmylib
.dylib  # Mach-O dynamically linked shared library ppc
otool
-L libmylib.dylib  # versions, refs /usr/lib/libgcc_s.1.dylib

 


 

Step 2, compile and link callmymod --

callmymod.c:
   
extern void mymod( int x );
   
int main( int argc, char** argv )
   
{
        mymod
( 42 );
   
}
gcc
-c callmymod.c
gcc
-v callmymod.o ./libmylib.dylib -o callmymod
    # == gcc callmymod.o -dynamic -L. -lmylib
otool -L callmymod  # refs libmylib.dylib
nm -gpv callmymod  # U undef _mymod: just a reference, not mymod itself

 


 

Step 3, run callmymod linking to libmylib.dylib --

export DYLD_PRINT_LIBRARIES=1  # see what dyld does, for ALL programs
callmymod
    dyld
: loaded: libmylib.dylib ...
    mymod
: 42

mv libmylib
.dylib /tmp
export DYLD_LIBRARY_PATH=/tmp  # dir:dir:...
callmymod
    dyld: loaded: /
tmp/libmylib.dylib ...
    mymod
: 42

unset DYLD_PRINT_LIBRARIES
unset DYLD_LIBRARY_PATH
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值