代码之家  ›  专栏  ›  技术社区  ›  jahmax

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

  •  61
  • jahmax  · 技术社区  · 14 年前

    #include <openssl/ssl.h>
    #include <openssl/rsa.h>
    #include <openssl/x509.h>
    #include <openssl/evp.h>
    

    在我有.c文件的同一个文件夹中,我有一个带有所有这些文件(以及更多文件)的/openssl,同样在synaptic package manager中,我看到安装了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/
    

    并试图再次编译,但我得到同样的错误。

    7 回复  |  直到 4 年前
        1
  •  112
  •   caf    7 年前

    include路径指示您应该根据 系统的 OpenSSL安装。你不应该有 .h 包目录中的文件-它应该从 /usr/include/openssl .

    纯OpenSSL包( libssl )不包括 h 文件-您还需要安装开发包。这个叫 libssl-dev 在Debian、Ubuntu和类似的发行版上,以及 libssl-devel 在CentOS,软呢帽,红帽子和类似的。

        2
  •  12
  •   Borealid    14 年前

    -I 正确标记gcc。

    gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c

    这个 -我 openssl 文件夹。

        3
  •  10
  •   nyedidikeke Dexter0015    5 年前

    yum install openssl
    yum install openssl-devel
    

        4
  •  7
  •   Jeff Pal    7 年前

    您需要包括库路径(-L/usr/local/lib/)

    gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto

    这对我很有用。

        5
  •  4
  •   Jonathan Leffler    14 年前

    openssl 当前目录的子目录,使用:

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

    ./openssl/ssl.h “来自” . “在 -I #include "openssl/ssl.h" ),你可能永远不需要问这个问题;Unix上的编译器通常会自动搜索当前目录中用双引号括起来的头,但不会搜索用尖括号括起来的头( #include <openssl/ssl.h> ). 它是实现定义的行为。

    -L /opt/openssl/lib '.

        6
  •  4
  •   Praveen S    14 年前

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

    /home/username/Programming . 因此,include file选项应该是 -I//home/username/Programming .

    这只是为了删除有关头的日志。你不妨提供 -L<Lib path> 链接到的选项 -lcrypto

        7
  •  1
  •   gzh    8 年前

    gcc document about Search Path .

    1) 如果使用尖括号(<>)使用#include,gcc将首先从系统路径搜索头文件,例如 /usr/本地/包括 等等。

    2) 由-L指定的路径 目录 命令行选项,将在默认目录之前搜索。

    3) 如果将引号(“”)与#include一起用作#include“file”,则将首先搜索包含当前文件的目录。

    所以,你的问题的答案如下:

    1) 如果要使用源代码文件夹中的头文件,请替换<&燃气轮机;包含指令中的“”。

    2) 如果要使用-I命令行选项,请将其添加到编译命令行中。(如果在环境变量中设置CFLAGS,则不会自动引用它)

    3) 关于包配置(openssl.pc),我认为在构建配置中没有显式声明的情况下不会引用它。