将 OpenSSL 库链接到程序
我已经从源代码构建了 OpenSSL(故意使用旧版本;使用 ./config && make && make test
构建),并且更愿意使用我构建的内容而不执行 make install
链接到我的程序。
失败的命令是:
gcc -Wall -Wextra -Werror -static -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto
-Iopenssl/openssl-0.9.8k/include -o myApp source1.o source2.o common.o`
我收到一系列类似于以下内容的错误:
common.c:(.text+0x1ea): undefined reference to `SSL_write'
这让我觉得我的 OpenSSL 有点奇怪。如果我从命令中省略 -Lopenssl/openssl-0.9.8k/
,错误将更改为无法:
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
我是否错误地编译了 OpenSSL?或者我应该如何最好地解决这个问题?
I have built OpenSSL from source (an intentionally old version; built with ./config && make && make test
) and would prefer to use what I have built without doing make install
to link against my program.
The command that's failing is:
gcc -Wall -Wextra -Werror -static -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto
-Iopenssl/openssl-0.9.8k/include -o myApp source1.o source2.o common.o`
And I receive a series of errors similar to:
common.c:(.text+0x1ea): undefined reference to `SSL_write'
This makes me think there's something funky with my OpenSSL. If I omit -Lopenssl/openssl-0.9.8k/
from my command, the error changes to being unable to:
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
Am I compiling OpenSSL incorrectly? Or how should I best resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
愚蠢的“Linux主义”再次袭来!显然,我需要更改我的命令,以便
-L
和-l
内容在末尾(尽管man gcc
似乎表明):gcc -Wall -Wextra -Werror -static -o myApp source1.o source2.o common.o -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto -Iopenssl/ openssl-0.9.8k/include
Silly "Linux-isms" strike again! Apparently, I need to change my command such that the
-L
and-l
stuff is at the end like (despite whatman gcc
seems to indicate):gcc -Wall -Wextra -Werror -static -o myApp source1.o source2.o common.o -Lopenssl/openssl-0.9.8k/ -lssl -lcrypto -Iopenssl/openssl-0.9.8k/include
为什么不想使用
make install
?如果您之前将生成的二进制文件传递给./configure --prefix $HOME/target_library_install_directory
如果您对构建和安装的每个库都使用了此技巧,则它可以将生成的二进制文件复制到您想要的目录中,然后您可以添加将目标目录添加到
LIBRARY_PATH
环境变量中,并避免使用 -L 选项。Why don't you want to use
make install
? It can copy generated binaries in the directory you want if you previously passed it to./configure --prefix $HOME/target_library_install_directory
If you used this trick with every library you build and install, you could then add the target directory to the
LIBRARY_PATH
environment variable and avoid using -L option.如果您使用 Autotools,或者正在构建像
cURL
这样的 Autools 项目,那么您应该能够使用pkg-config
。这个想法是 Autotools 包将读取 OpenSSL 的包配置,并且一切都会为您“正常工作”。OpenSSL 包配置库名称为
openssl
。您可以像在基于 makefile 的项目中一样使用它。
另请参阅如何在 Make 中使用 pkg-config 和 如何使用 pkg-config 静态链接库。
If you use Autotools, or you are building an Autools project like
cURL
, then you should be able to usepkg-config
. The idea is the Autotools package will read OpenSSL's package configuration and things will "just work" for you.The OpenSSL package configuration library name is
openssl
.You would use it like so in a makefile based project.
Also see How to use pkg-config in Make and How to use pkg-config to link a library statically.
另一种方法是使用 pkg-config 来保持兼容性。这是需要链接
OpenSSL
时的makefile
示例。Another approach is to use
pkg-config
to preserve compatibility. This is an example ofmakefile
when needs to linkOpenSSL
.