Linux 如何使用 OpenSSL 编译 .c 文件包含?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3368683/
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
How to compile .c file with OpenSSL includes?
提问by jahmax
I am trying to compile a small .c file that has the following includes:
我正在尝试编译一个包含以下内容的小 .c 文件:
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/evp.h>
In the same folder where i have the .c file I have a /openssl with all those files (and more), also in synaptic package manager I see OpenSSL installed, I am trying to compile with this:
在我有 .c 文件的同一个文件夹中,我有一个 /openssl 包含所有这些文件(以及更多),也在突触包管理器中我看到安装了 OpenSSL,我试图用这个编译:
gcc -o Opentest Opentest.c -lcrypto
but I always get the errors:
但我总是收到错误:
error: openssl/ssl.h: No such file or directory
error: openssl/rsa.h: No such file or directory
error: openssl/x509.h: No such file or directory
error: openssl/evp.h: No such file or directory
The file I want to compile is only a .c file, doesnt have Makefile or ./configure.
我要编译的文件只是一个 .c 文件,没有 Makefile 或 ./configure。
I already tried:
我已经尝试过:
env CFLAGS=-I/path/to/openssl/
and tried to compile again but i get the same errors.
并尝试再次编译,但我遇到了相同的错误。
What should I do in order to compile with openssl includes?
为了使用 openssl 进行编译,我应该怎么做?
采纳答案by caf
Your include paths indicate that you should be compiling against the system'sOpenSSL installation. You shouldn't have the .h
files in your package directory - it should be picking them up from /usr/include/openssl
.
您的包含路径表明您应该针对系统的OpenSSL 安装进行编译。你.h
的包目录中不应该有这些文件——它应该从/usr/include/openssl
.
The plain OpenSSL package (libssl
) doesn't include the .h
files - you need to install the development package as well. This is named libssl-dev
on Debian, Ubuntu and similar distributions, and libssl-devel
on CentOS, Fedora, Red Hat and similar.
普通 OpenSSL 包 ( libssl
) 不包含这些.h
文件 - 您还需要安装开发包。这libssl-dev
在 Debian、Ubuntu 和类似发行版上,以及libssl-devel
CentOS、Fedora、Red Hat 和类似发行版上命名。
回答by Borealid
Use the -I
flag to gcc properly.
-I
正确使用该标志进行 gcc。
gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c
gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c
The -I
should point to the directory containing the openssl
folder.
本-I
应指向包含的目录openssl
文件夹。
回答by Jonathan Leffler
If the OpenSSL headers are in the openssl
sub-directory of the current directory, use:
如果 OpenSSL 标头位于openssl
当前目录的子目录中,请使用:
gcc -I. -o Opentest Opentest.c -lcrypto
The pre-processor looks to create a name such as "./openssl/ssl.h
" from the ".
" in the -I
option and the name specified in angle brackets. If you had specified the names in double quotes (#include "openssl/ssl.h"
), you might never have needed to ask the question; the compiler on Unix usually searches for headers enclosed in double quotes in the current directory automatically, but it does not do so for headers enclosed in angle brackets (#include <openssl/ssl.h>
). It is implementation defined behaviour.
预处理器希望./openssl/ssl.h
从选项中的“ .
”-I
和尖括号中指定的名称创建一个名称,例如“ ” 。如果您在双引号 ( #include "openssl/ssl.h"
) 中指定了名称,则您可能永远不需要问这个问题;Unix 上的编译器通常会自动搜索当前目录中双引号括起来的头文件,但不会搜索尖括号 ( #include <openssl/ssl.h>
) 中的头文件。它是实现定义的行为。
You don't say where the OpenSSL libraries are - you might need to add an appropriate option and argument to specify that, such as '-L /opt/openssl/lib
'.
您没有说明 OpenSSL 库在哪里 - 您可能需要添加适当的选项和参数来指定它,例如“ -L /opt/openssl/lib
”。
回答by Praveen S
From the openssl.pc file
从 openssl.pc 文件
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries and tools
Version: 0.9.8g
Requires:
Libs: -L${libdir} -lssl -lcrypto
Libs.private: -ldl -Wl,-Bsymbolic-functions -lz
Cflags: -I${includedir}
You can note the Include directory path and the Libs path from this. Now your prefix for the include files is /home/username/Programming
.
Hence your include file option should be -I//home/username/Programming
.
您可以从这里注意到 Include 目录路径和 Libs 路径。现在包含文件的前缀是/home/username/Programming
. 因此您的包含文件选项应该是-I//home/username/Programming
.
(Yes i got it from the comments above)
(是的,我是从上面的评论中得到的)
This is just to remove logs regarding the headers. You may as well provide -L<Lib path>
option for linking with the -lcrypto
library.
这只是为了删除有关标题的日志。您也可以提供-L<Lib path>
与-lcrypto
库链接的选项。
回答by user2317002
Use the snippet below as a solution for the cited challenge;
使用下面的片段作为所引用挑战的解决方案;
yum install openssl
yum install openssl-devel
Tested and proved effective on CentOS version 5.4 with keepalived version 1.2.7.
在 CentOS 5.4 版和 keepalived 1.2.7 版上测试并证明有效。
回答by gzh
For this gcc error, you should reference to to the gcc document about Search Path.
对于这个 gcc 错误,您应该参考关于 Search Path的gcc 文档。
In short:
简而言之:
1) If you use angle brackets(<>) with #include, gcc will search header file firstly from system path such as /usr/local/includeand /usr/include, etc.
1) 如果在#include 中使用尖括号(<>),gcc 将首先从系统路径中搜索头文件,例如/usr/local/include和/usr/include等。
2) The path specified by -Ldircommand-line option, will be searched before the default directories.
2) -L dir命令行选项指定的路径,将在默认目录之前搜索。
3)If you use quotation("") with #include as #include "file", the directory containing the current file will be searched firstly.
3)如果使用quote("")和#include as #include "file",将首先搜索包含当前文件的目录。
so, the answer to your question is as following:
所以,你的问题的答案如下:
1) If you want to use header files in your source code folder, replace <> with "" in #include directive.
1) 如果要在源代码文件夹中使用头文件,请将#include 指令中的<> 替换为""。
2) if you want to use -I command line option, add it to your compile command line.(if set CFLAGS in environment variables, It will not referenced automatically)
2) 如果你想使用 -I 命令行选项,将它添加到你的编译命令行。(如果在环境变量中设置了 CFLAGS,它不会自动引用)
3) About package configuration(openssl.pc), I do not think it will be referenced without explicitly declared in build configuration.
3)关于包配置(openssl.pc),我认为如果没有在构建配置中明确声明,我认为它不会被引用。
回答by Jeff Pal
You need to include the library path (-L/usr/local/lib/)
您需要包含库路径 (-L/usr/local/lib/)
gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto
gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto
It works for me.
这个对我有用。