How to compile .c file with OpenSSL includes?

192,309

Solution 1

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 libssl-devel on CentOS, Fedora, Red Hat and similar.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
192,309
jahmax
Author by

jahmax

Updated on July 05, 2022

Comments

  • jahmax
    jahmax almost 2 years

    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?

  • jahmax
    jahmax almost 14 years
    gcc -I/home/username/Programming/openssl/ -o Opentest -lcrypto Opentest.c it gives me the same errors :(
  • Earlz
    Earlz almost 14 years
    To elaborate on your answer if the openssl folder is /path/to/openssl/ then the option needs to be -I/path/to/ @jahmax. so you want /home/username/Programming/
  • Borealid
    Borealid almost 14 years
    @Earlz : Thanks, I tried to say that with the last explicit line but it must have gotten missed.
  • jahmax
    jahmax almost 14 years
    Thanks, that worked, but now I get errors in the includes inside openssl/ssl.h, that include files that are inside /openssl/subfolders, how can I make gcc to find those?
  • Earlz
    Earlz almost 14 years
    @jah if you are being "bad" and your own project's include path(openssl/*) doesn't match OpenSSL's (possible *) then you could have this problem. The best solution is to change your project to use ssl.h instead of openssl/ssl.h etc. The quick fix is to set include paths for both /path/to/ and /path/to/openssl/
  • jahmax
    jahmax almost 14 years
    I still get errors in all the includes used by ssl.h, why gcc cant find those?
  • jahmax
    jahmax almost 14 years
    i tried -L/usr/lib but I still get errors in all the includes from ssl.h, why gcc cant find them?
  • caf
    caf almost 14 years
    @jahmax: No worries. You will find that most library packages in Debian-based distros have a *-dev package that you will need to compile against the library (and often a *-dbg package containing debugging symbols for the library).
  • karmakaze
    karmakaze about 11 years
    Right on. libssl-dev did the trick for building osslsigncode-1.5.2 on Ubuntu.
  • Wafeeq
    Wafeeq about 7 years
    -ldir is wrong. with -l, it is always lib instead of dir.
  • gzh
    gzh about 7 years
    @GulluButt It should be -Ldir. I have revised my answer.
  • Wafeeq
    Wafeeq about 7 years
    I am having similar problem which is mentioned in this question. I am trying to compile an example from link below but it is not working. I am not sure if my installation if ok or not. When I say -lssl, it looks for ssl.dll in installation directory of openssl ? simplestcodings.blogspot.de/2010/08/…
  • Wafeeq
    Wafeeq about 7 years
    How to install libssl-dev in wondows7?
  • gzh
    gzh about 7 years
    @GulluButt, For Linux, -lssl option will make gcc search libssl.so or libssl.a for symbols needed.
  • Wafeeq
    Wafeeq about 7 years
    on windows not on Linux.
  • emc
    emc about 5 years
    @Wafeeq just copy your comment and paste it into a search engine.
  • Reeshabh Ranjan
    Reeshabh Ranjan over 4 years
    Apparently params.h is missing in /usr/include/openssl/ after following these steps
  • Craig S. Anderson
    Craig S. Anderson over 3 years
    From a lower voted answer below: On CentOS, do: yum install openssl-devel