How to compile .c file with OpenSSL includes?

前端 未结 7 851
醉梦人生
醉梦人生 2020-11-27 03:40

I am trying to compile a small .c file that has the following includes:

#include 
#include 
#include 

        
相关标签:
7条回答
  • 2020-11-27 03:58

    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.

    0 讨论(0)
  • 2020-11-27 04:05

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

    0 讨论(0)
  • 2020-11-27 04:06

    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.

    0 讨论(0)
  • 2020-11-27 04:10

    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.

    0 讨论(0)
  • 2020-11-27 04:11

    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.

    0 讨论(0)
  • 2020-11-27 04:11

    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.

    0 讨论(0)
提交回复
热议问题