Libtool创建和使用linux下动态库
2006-07-05 17:56
351 查看
文件fun.c,fun.h,hello.c,hello.h,main.c动态库函数都在fun.c和hello.c里面
fun.c:
int add(int a, int b)
{
return a+b;
}
fun.h:
#ifndef _FUN_H_11
#define _FUN_H_11
int add(int a, int b);
#endif
----------------------------
hello.c:
#i nclude <stdio.h>
void output(char *ss)
{
printf("HELLO %s/n", ss);
}
hello.h
#ifndef HELLO_H_111
#define HELLO_H_111
void output(char *ss);
#endif
----------------------------
main.c:
#i nclude <stdio.h>
#i nclude "hello.h"
#i nclude "fun.h"
void
main()
{
output("world");
printf("Test Value:%d/n", add(1, 2));
}
使用libtools创建和使用安装动态库步骤:
(1)
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文件
(2)
libtool --mode=link gcc -g -O -o libhello.la hello.lo fun.lo -rpath /usr/local/lib -lm
#连接成动态库文件
(3)
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
#连接生成可执行文件test
(4)
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
#安装动态库
然后就可以运行/usr/local/bin/test了,当然路径可以任意设置,这是手动过程,写成Makefile文件为:
OBJS = fun.o hello.o
LO_OBJS = main.lo fun.lo hello.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
all : test
install : libhello.la
test : libhello.la main.o
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
libhello.la : $(OBJS)
libtool gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -lm -version-info ${PACKAGE_VERSION}
main.o : main.c fun.h hello.h
libtool --mode=compile gcc -g -O -c main.c
fun.o : fun.c fun.h
libtool --mode=compile gcc -g -O -c fun.c
hello.o : hello.c hello.h
libtool --mode=compile gcc -g -O -c hello.c
clean :
@rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la
@rm -rf .libs
fun.c:
int add(int a, int b)
{
return a+b;
}
fun.h:
#ifndef _FUN_H_11
#define _FUN_H_11
int add(int a, int b);
#endif
----------------------------
hello.c:
#i nclude <stdio.h>
void output(char *ss)
{
printf("HELLO %s/n", ss);
}
hello.h
#ifndef HELLO_H_111
#define HELLO_H_111
void output(char *ss);
#endif
----------------------------
main.c:
#i nclude <stdio.h>
#i nclude "hello.h"
#i nclude "fun.h"
void
main()
{
output("world");
printf("Test Value:%d/n", add(1, 2));
}
使用libtools创建和使用安装动态库步骤:
(1)
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文件
(2)
libtool --mode=link gcc -g -O -o libhello.la hello.lo fun.lo -rpath /usr/local/lib -lm
#连接成动态库文件
(3)
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
#连接生成可执行文件test
(4)
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
#安装动态库
然后就可以运行/usr/local/bin/test了,当然路径可以任意设置,这是手动过程,写成Makefile文件为:
OBJS = fun.o hello.o
LO_OBJS = main.lo fun.lo hello.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
all : test
install : libhello.la
test : libhello.la main.o
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
libhello.la : $(OBJS)
libtool gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -lm -version-info ${PACKAGE_VERSION}
main.o : main.c fun.h hello.h
libtool --mode=compile gcc -g -O -c main.c
fun.o : fun.c fun.h
libtool --mode=compile gcc -g -O -c fun.c
hello.o : hello.c hello.h
libtool --mode=compile gcc -g -O -c hello.c
clean :
@rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la
@rm -rf .libs
相关文章推荐
- Libtool创建和使用linux下动态库
- Libtool创建和使用linux下动态库
- Linux下静态库和动态库的创建和使用
- linux下静态库和动态库的创建和使用
- Linux中创建和使用静态库&动态库
- linux中静态库和动态库的创建和使用
- Linux 动态库的创建和使用简单实例
- linux下创建,使用动态库
- Linux下静态库和动态库的创建和使用( 转载 )
- [转载] linux下创建和使用静态和动态库
- linux---静态库与动态库的创建、生成、使用
- linux下静态库、动态库的创建与使用
- 【C/C++开发】C++静态库与动态库以及在Linux和Windows上的创建使用
- Linux下静态库和动态库的创建和使用
- 问题集合 ---- linux 静态库和动态库创建、检查、使用建议
- 在linux下,创建和使用C/C++的静态与动态库
- linux 动态库的创建和使用
- linux下静态库和动态库的创建和使用
- VisualGDB系列7:使用VS创建Linux静态库和动态库
- 在Linux中静态库.a和动态库.so 的创建和使用