GCC 编译、连接初体验

11月 30, 2017 |

# cat liba.c

int liba_func(int i)
{
return i + 1;
}

# cat libb.c

int liba_func(int i);
int libb_func(int i)
{
return liba_func(i + 1);
}

# cat test.c

#include <stdio.h>
int libb_func(int i);
int main(int argc, char *argv[])
{
fprintf(stdout, "%d\n", libb_func(argc));
return 0;
}

# cat star.sh ,编译指令

#/bin/sh
rm -rf test ./a/* ./b/*
gcc -o ./a/liba.so -fPIC -shared liba.c
gcc -o ./b/libb.so -fPIC -shared libb.c
gcc -o test -Wl,-rpath=./a,-rpath=./b -L./a -L./b test.c -lb -la

-fPIC指定生成的共享库是位置无关的代码
-Wl,-rpath 指定运行时查找共享库的路径, -L编译时共享库的查找路径, -l指定库名

Posted in: Linux

Comments are closed.