Linux 将 OpenSSL 库链接到程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4352573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linking OpenSSL libraries to a program
提问by mrduclaw
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.
我已经从源代码(一个故意的旧版本;使用 构建./config && make && make test
)构建了 OpenSSL,并且更愿意使用我构建的内容,而无需make install
链接到我的程序。
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:
这让我觉得我的 OpenSSL 有点奇怪。如果我-Lopenssl/openssl-0.9.8k/
从我的命令中省略,错误将更改为无法:
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
Am I compiling OpenSSL incorrectly? Or how should I best resolve this?
我是否错误地编译了 OpenSSL?或者我应该如何最好地解决这个问题?
采纳答案by mrduclaw
Silly "Linux-isms" strike again! Apparently, I need to change my command such that the -L
and -l
stuff is at the endlike (despite what man gcc
seems to indicate):
愚蠢的“Linux 主义”再次罢工!显然,我需要更改我的命令,使-L
and 的-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
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
回答by jopasserat
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
你为什么不想使用make install
?如果您之前将它传递给它,它可以将生成的二进制文件复制到您想要的目录中./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.
如果您在构建和安装的每个库中都使用了这个技巧,那么您可以将目标目录添加到LIBRARY_PATH
环境变量中并避免使用 -L 选项。
回答by jww
If you use Autotools, or you are building an Autools project like cURL
, then you should be able to use pkg-config
. The idea is the Autotools package will read OpenSSL's package configuration and things will "just work" for you.
如果您使用 Autotools,或者您正在构建一个 Autools 项目,例如cURL
,那么您应该能够使用pkg-config
. 这个想法是 Autotools 包将读取 OpenSSL 的包配置,事情会为你“正常工作”。
The OpenSSL package configuration library name is openssl
.
OpenSSL 包配置库名称是openssl
.
You would use it like so in a makefile based project.
您可以在基于 makefile 的项目中像这样使用它。
%.o: %.c
$(CC) -o $@ -c `pkg-config --cflags openssl` $^
target: foo.o bar.o baz.o
$(CC) -o $@ `pkg-config --libs openssl` $^
Also see How to use pkg-config in Makeand How to use pkg-config to link a library statically.