链接静态库以构建静态可执行文件

3
我正在尝试构建一个静态可执行C程序,该程序需要来自Postgresql libecpg-dev软件包的以下库:libecpg、libpq和libpgtypes。静态库libecpg.a、libpq.a、libpgtypes.a与它们的动态版本位于同一位置,即/usr/lib。我已经尝试将-L选项添加为它们的路径,但结果相同。
我的可执行文件需要是静态的,以便二进制文件可以是可移植的,这样用户就不必安装库到他们的系统上才能使用它。当我使用以下命令和动态库构建程序时,程序构建得很好:(必须包括-I目录,因为我使用了一些Postgresql函数)
 gcc main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name

当我尝试链接静态库时,我使用以下命令:

gcc -static main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name 

然而,编译器输出一长串“未定义引用”的错误信息。错误数目众多,以下是其中的一小部分:

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o):
In function `get_descriptors':
(.text+0xc3): undefined reference to `pthread_once'
 /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGdeallocate_desc':
(.text+0x2c6): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGallocate_desc':
(.text+0x376): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `get_descriptors':
(.text+0xd2): undefined reference to `pthread_getspecific'

当我按照以下方式更改订单时:
gcc -I/usr/include/postgresql -static -lecpg -lpq -lpgtypes -o program_name main.o

编译器的输出看起来像下面这样(再次强调,只是少量示例):
main.c:(.text+0x5c6): undefined reference to `ECPGconnect'
main.c:(.text+0x83e): undefined reference to `ECPGdo'
main.c:(.text+0x843): undefined reference to `ECPGget_sqlca'
main.c:(.text+0x85c): undefined reference to `ECPGdisconnect'
main.c:(.text+0x87c): undefined reference to `ECPGget_sqlca'

我尝试在所有选项中使用-I选项(以及其他所有选项),但似乎都不起作用。

2个回答

1

0

试着这样做:

 gcc main.o /usr/include/postgresql/libecpg.a /usr/include/postgresql/libecpg.a /usr/include/postgresql/libpq.a /usr/include/postgresql/libpgtypes.a -o program_name

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接