如何使用 OpenSSL 编译 .c 文件?
我正在尝试编译一个小的 .c 文件,其中包含以下内容:
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/evp.h>
在包含 .c 文件的同一文件夹中,我有一个包含所有这些文件(以及更多文件)的 /openssl,也在突触包管理器中我看到安装了 OpenSSL ,我试图用这个进行编译:
gcc -o Opentest Opentest.c -lcrypto
但我总是收到错误:
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
我想要编译的文件只是一个 .c 文件,没有 Makefile 或 ./configure。
我已经尝试过:
env CFLAGS=-I/path/to/openssl/
并尝试再次编译,但出现相同的错误。
我应该怎么做才能使用 OpenSSL 进行编译?
I am trying to compile a small .c file that has the following includes:
#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:
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, doesn't have Makefile or ./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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的包含路径表明您应该针对系统的 OpenSSL 安装进行编译。您的包目录中不应该有
.h
文件 - 它应该从/usr/include/openssl
中获取它们。普通 OpenSSL 包 (
libssl
) 不包含.h
文件 - 您还需要安装开发包。在 Debian、Ubuntu 和类似发行版上,其名称为libssl-dev
;在 CentOS、Fedora、Red Hat 及类似发行版上,名称为openssl-devel
。Your include paths indicate that you should be compiling against the system's OpenSSL installation. You shouldn't have the
.h
files in your package directory - it should be picking them up from/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 namedlibssl-dev
on Debian, Ubuntu and similar distributions, andopenssl-devel
on CentOS, Fedora, Red Hat and similar.正确使用 gcc 的
-I
标志。gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c
-I
应指向包含openssl
的目录文件夹。Use the
-I
flag to gcc properly.gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c
The
-I
should point to the directory containing theopenssl
folder.使用下面的代码片段作为所引用挑战的解决方案;
在 CentOS 5.4 版和 keepalived 1.2.7 版上测试并证明有效。
Use the snippet below as a solution for the cited challenge;
Tested and proved effective on CentOS version 5.4 with keepalived version 1.2.7.
您需要包含库路径(-L/usr/local/lib/)
gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto
它对我有用。
You need to include the library path (-L/usr/local/lib/)
gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto
It works for me.
如果 OpenSSL 标头位于当前目录的
openssl
子目录中,请使用:预处理器会创建一个名称,例如“
./openssl/ssl.h”来自
-I
选项中的“.
”以及尖括号中指定的名称。如果您在双引号中指定了名称 (#include "openssl/ssl.h"
),您可能永远不需要提出这个问题; Unix 上的编译器通常会自动在当前目录中搜索用双引号括起来的标头,但它不会搜索用尖括号括起来的标头 (#include
)。这是实现定义的行为。您没有说明 OpenSSL 库的位置 - 您可能需要添加适当的选项和参数来指定它,例如“
-L /opt/openssl/lib
”。If the OpenSSL headers are in the
openssl
sub-directory of the current directory, use: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.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.pc 文件中
您可以从中记下 Include 目录路径和 Libs 路径。现在,包含文件的前缀是
/home/username/Programming
。因此,您的包含文件选项应该是
-I//home/username/Programming
。(是的,我从上面的评论中得到它)
这只是为了删除有关标题的日志。您还可以提供
-L
选项来链接-lcrypto
库。From the openssl.pc file
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
.(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.对于这个gcc错误,您应该参考关于搜索路径的gcc文档。
简而言之:
1)如果你在#include中使用尖括号(<>),gcc将首先从系统路径中搜索头文件,例如/usr/local/include和/usr /include 等
2) -Ldir 命令行选项指定的路径,将在默认目录之前搜索。
3)如果使用引号("")和#include作为#include“文件”,则首先搜索包含当前文件的目录。
所以,你的问题的答案如下:
1)如果你想使用源代码文件夹中的头文件,请将 <> 替换为#include 指令中带有“”。
2)如果你想使用-I命令行选项,请将其添加到你的编译命令行中。(如果在环境变量中设置CFLAGS,它将不会自动引用)
3)关于包配置(openssl.pc),我不认为它将被引用而无需在构建配置中显式声明。
For this gcc error, you should reference to to the gcc document about Search Path.
In short:
1) If you use angle brackets(<>) with #include, gcc will search header file firstly from system path such as /usr/local/include and /usr/include, etc.
2) The path specified by -Ldir command-line option, will be searched before the default directories.
3)If you use quotation("") with #include as #include "file", the directory containing the current file will be searched firstly.
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.
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)
3) About package configuration(openssl.pc), I do not think it will be referenced without explicitly declared in build configuration.