gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:50:30 30 4
gpt4 key购买 nike

我正在尝试构建一个 C 程序的静态可执行文件,它需要 Postgresql libecpg-dev 包中的以下库:libecpg、libpq 和 libpgtypes。静态库 libecpg.a、libpq.a、libpgtypes.a 都位于与其动态版本/usr/lib 相同的位置。无论如何,我已经尝试添加 -L 选项作为它们的路径,但我得到了相同的结果。

我的可执行文件需要是静态的,以便二进制文件可以移植,这样用户就不必将库安装到他们的系统上就可以使用它。当我对动态库使用以下命令时,程序构建良好:(对于我正在使用的某些 Postgresql 函数,必须包含 -I 目录)

 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 

然而,编译器给我一长串“ undefined reference ”错误作为输出。有很多,但这里只是一小部分:

/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 选项(以及所有其他选项),但似乎没有任何效果。

最佳答案

我认为您不能再在现代 Linux 发行版中创建完全静态的非平凡可执行文件。但是you can statically include some specific libraries .

关于postgresql - 链接到静态库以构建静态可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17329796/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com