动态连接库
时间:2010-12-28 来源:netrangering
摘自:http://hi.baidu.com/zengzhaonong/blog/item/4d54ac1ce16a738d87d6b697.html
mylib.c
----------------------------------
#include <stdio.h>
int add(int a, int b)
{
int c = a + b;
printf("c = %d\n", c);
return c;
}
mylib.h
----------------------------------
#ifndef MYLIB_H
#define MYLIB_H
int add(int a, int b);
#endif
Makefile
----------------------------------
CC = gcc -Wall
LD = ld
MY_OBJECTS = mylib.o
all: $(MY_OBJECTS)
@$(CC) -shared -o libmylib.so $(MY_OBJECTS)
clean:
rm -fr *.so *.o
testlib.c
----------------------------------
#include "mylib.h"
int main()
{
add(10, 20);
}
readme.sh
----------------------------------
make
gcc testlib.c -L./ -lmylib
export LD_LIBRARY_PATH=./
./a.out
/***********************执行过程************************/ sh readme.sh su cp libmylib.so /usr/local/lib/ ./a.out c=30
----------------------------------
#include <stdio.h>
int add(int a, int b)
{
int c = a + b;
printf("c = %d\n", c);
return c;
}
mylib.h
----------------------------------
#ifndef MYLIB_H
#define MYLIB_H
int add(int a, int b);
#endif
Makefile
----------------------------------
CC = gcc -Wall
LD = ld
MY_OBJECTS = mylib.o
all: $(MY_OBJECTS)
@$(CC) -shared -o libmylib.so $(MY_OBJECTS)
clean:
rm -fr *.so *.o
testlib.c
----------------------------------
#include "mylib.h"
int main()
{
add(10, 20);
}
readme.sh
----------------------------------
make
gcc testlib.c -L./ -lmylib
export LD_LIBRARY_PATH=./
./a.out
/***********************执行过程************************/ sh readme.sh su cp libmylib.so /usr/local/lib/ ./a.out c=30
相关阅读 更多 +