79

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?

rici
  • 234,347
  • 28
  • 237
  • 341
jahmax
  • 8,181
  • 7
  • 26
  • 25
  • Also see [How to compile openssl with relative rpath](https://stackoverflow.com/q/9399677/608639) and [Build OpenSSL with RPATH?](https://stackoverflow.com/q/29858870/608639) It ensures you get the expected libraries at runtime. – jww Sep 14 '18 at 20:14

7 Answers7

119

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.

caf
  • 233,326
  • 40
  • 323
  • 462
12

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.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 2
    gcc -I/home/username/Programming/openssl/ -o Opentest -lcrypto Opentest.c it gives me the same errors :( – jahmax Jul 30 '10 at 04:29
  • 2
    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/` – Earlz Jul 30 '10 at 04:36
  • @Earlz : Thanks, I tried to say that with the last explicit line but it must have gotten missed. – Borealid Jul 30 '10 at 04:42
  • 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? – jahmax Jul 30 '10 at 04:51
  • @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/` – Earlz Jul 30 '10 at 04:58
11

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.

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
user2317002
  • 111
  • 1
  • 2
9

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.

Jeff Pal
  • 1,519
  • 1
  • 17
  • 28
4

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • i tried -L/usr/lib but I still get errors in all the includes from ssl.h, why gcc cant find them? – jahmax Jul 30 '10 at 14:50
4

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.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
1

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.

gzh
  • 3,507
  • 2
  • 19
  • 23
  • `-ldir` is wrong. with `-l`, it is always `lib` instead of `dir`. – Wafeeq May 19 '17 at 11:49
  • 1
    @GulluButt It should be -Ldir. I have revised my answer. – gzh May 19 '17 at 12:16
  • 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` ? http://simplestcodings.blogspot.de/2010/08/secure-server-client-using-openssl-in-c.html#!/2010/08/secure-server-client-using-openssl-in-c.html – Wafeeq May 19 '17 at 13:20
  • @GulluButt, For Linux, -lssl option will make gcc search libssl.so or libssl.a for symbols needed. – gzh May 22 '17 at 00:56
  • on windows not on Linux. – Wafeeq May 22 '17 at 08:39