如何使用 OpenSSL 编译 .c 文件?

发布于 2024-09-11 22:25:08 字数 753 浏览 4 评论 0原文

我正在尝试编译一个小的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

影子是时光的心 2024-09-18 22:25:08

您的包含路径表明您应该针对系统的 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 named libssl-dev on Debian, Ubuntu and similar distributions, and openssl-devel on CentOS, Fedora, Red Hat and similar.

无声静候 2024-09-18 22:25:08

正确使用 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 the openssl folder.

剪不断理还乱 2024-09-18 22:25:08

使用下面的代码片段作为所引用挑战的解决方案;

yum install openssl
yum install openssl-devel

在 CentOS 5.4 版和 keepalived 1.2.7 版上测试并证明有效。

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.

久而酒知 2024-09-18 22:25:08

您需要包含库路径(-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.

魂归处 2024-09-18 22:25:08

如果 OpenSSL 标头位于当前目录的 openssl 子目录中,请使用:

gcc -I. -o Opentest Opentest.c -lcrypto

预处理器会创建一个名称,例如“./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:

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.

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'.

小女人ら 2024-09-18 22:25:08

从 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}

您可以从中记下 Include 目录路径和 Libs 路径。现在,包含文件的前缀是 /home/username/Programming
因此,您的包含文件选项应该是 -I//home/username/Programming

(是的,我从上面的评论中得到它)

这只是为了删除有关标题的日志。您还可以提供 -L 选项来链接 -lcrypto 库。

From the openssl.pc file

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.

(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.

说不完的你爱 2024-09-18 22:25:08

对于这个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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文