文件fun.c,fun.h,hello.c,hello.h,main.c动态库函数都在fun.c和hello.c里面 int add(int a, int b) #ifndef _FUN_H_11 hello.c: #i nclude <stdio.h> #ifndef HELLO_H_111 main.c: #i nclude <stdio.h> 使用libtools创建和使用安装动态库步骤: (1) (2) (3) (4) 然后就可以运行/usr/local/bin/test了,当然路径可以任意设置,这是手动过程,写成Makefile文件为: OBJS = fun.o hello.o all : test install : libhello.la libhello.la : $(OBJS) main.o : main.c fun.h hello.h fun.o : fun.c fun.h hello.o : hello.c hello.h clean :
fun.c:
{
return a+b;
}
fun.h:
#define _FUN_H_11
int add(int a, int b);
#endif
----------------------------
void output(char *ss)
{
printf("HELLO %s/n", ss);
}
hello.h
#define HELLO_H_111
void output(char *ss);
#endif
----------------------------
#i nclude "hello.h"
#i nclude "fun.h"
void
main()
{
output("world");
printf("Test Value:%d/n", add(1, 2));
}
libtool --mode=compile gcc -g -O -c hello.c
libtool --mode=compile gcc -g -O -c fun.c
libtool --mode=compile gcc -g -O -c main.c
#生成各自的o文件
libtool --mode=link gcc -g -O -o libhello.la hello.lo fun.lo -rpath /usr/local/lib -lm
#连接成动态库文件
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
#连接生成可执行文件test
libtool --mode=install cp libhello.la /usr/local/lib/libhello.la
libtool --mode=install install -c libhello.la /usr/local/lib/libhello.la
libtool -n --mode=finish /usr/local/lib
libtool install -c test /usr/local/bin/test
#安装动态库
LO_OBJS = main.lo fun.lo hello.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
test : libhello.la main.o
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
libtool gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -lm -version-info ${PACKAGE_VERSION}
libtool --mode=compile gcc -g -O -c main.c
libtool --mode=compile gcc -g -O -c fun.c
libtool --mode=compile gcc -g -O -c hello.c
@rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la
@rm -rf .libs
Linux 动态连接库
最新推荐文章于 2024-12-30 09:14:29 发布