gpt4 book ai didi

c++ - 将 OpenSSL 链接到动态库

转载 作者:行者123 更新时间:2023-11-30 05:43:36 24 4
gpt4 key购买 nike

我正在尝试将 OpenSSL 静态链接到我的程序中。

链接到可执行文件时它工作正常。我需要在共享库(so 或 dll)中使用 OpenSSL,我稍后会在进程执行时动态加载它。

尝试将 OpenSSL 静态链接到共享库会导致错误,因为 OpenSSL 未使用 -fPIC 进行编译。是否可以在不重新编译 openssl 的情况下执行此操作?

此外,有没有更好的方法来做到这一点?

最佳答案

I'm trying to static link OpenSSL into my program.

在这种情况下,它很简单:

gcc prog.c /usr/local/lib/libssl.a /usr/local/lib/libcrypto.a -o prog.exe -ldl

It works fine when linking into the executable.

魔鬼代言人...它与位置独立代码 (PIE) 一起工作是否正常?程序上的 PIE 等同于共享对象上的 PIC(有些人放弃了)。

gcc -fPIE prog.c /usr/local/lib/libssl.a /usr/local/lib/libcrypto.a -o prog.exe -ldl

根据 GCC 人员的说法,您可以使用 fPIC 进行编译, 然后用 -fPIC 构建一个共享对象或带有 -fPIE 的可重定位可执行文件.也就是用-fPIC就可以了对于两者。


Trying to statically link OpenSSL into the shared library causes errors due to OpenSSL not being compiled with -fPIC.

这很容易修复。您只需指定 shared在配置中:

./config shared no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl
make
sudo make install

认为您也可以(注意缺少 shared ):

export CFLAGS="-fPIC"
./config no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl
make
sudo make install

not being compiled with -fPIC. Is it possible to do this without recompiling openssl?

不,您必须使用 PIC 进行编译以确保 GCC 生成可重定位代码。


Also, is there a better way to do this?

通常你只需配置 shared .这会触发 -fPIC ,这会让您获得可重定位的代码。

您还可以做其他事情,但它们更具侵入性。比如可以修改Configure行(如 linux-x86_64 ),并添加 -fPIC在第二个领域。字段之间用冒号隔开,第二个字段是$cflags由 OpenSSL 构建系统使用。

可以看到修改Configure的例子在Build OpenSSL with RPATH?

关于c++ - 将 OpenSSL 链接到动态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129419/

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