将 OpenSSL 链接到动态库
Linking OpenSSL into a dynamic library
我正在尝试将 link OpenSSL 静态化到我的程序中。
在 link 进入可执行文件时工作正常。我需要在共享库(so 或 dll)中使用 OpenSSL,我稍后会在进程执行时动态加载它。
尝试将 OpenSSL 静态 link 到共享库中会导致错误,因为 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
。字段之间用冒号分隔,第二个字段是OpenSSL构建系统使用的$cflags
。
您可以在 Build OpenSSL with RPATH?
查看修改 Configure
的示例
我正在尝试将 link OpenSSL 静态化到我的程序中。
在 link 进入可执行文件时工作正常。我需要在共享库(so 或 dll)中使用 OpenSSL,我稍后会在进程执行时动态加载它。
尝试将 OpenSSL 静态 link 到共享库中会导致错误,因为 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
。字段之间用冒号分隔,第二个字段是OpenSSL构建系统使用的$cflags
。
您可以在 Build OpenSSL with RPATH?
查看修改Configure
的示例