linux中把.c的文件编译成.so

首先
gcc -c -fPIC libst.c
生成 libst.o

然后
gcc -shared -fPIC -o libst.so libst.o
生成 libst.so 动态链接库

把libst.so拷贝到系统默认库目录下,比如 /lib, /usr/lib 下
假定你有 test.c 要引用这个库
gcc -lst -o test test.c
然后就可以了

假如不能把libst.so拷贝到默认库目录下,比方说放在了
/home/aaa/lib 下
那么就用这样的语句来编译test.c
gcc -L/home/aaa/lib -lst -Wl,-rpath=/home/aaa/lib -o test test.c 

你可能感兴趣的:(linux)