Coder Perfect

How can I use OpenSSL included to compile a.c file?

Problem

I’m attempting to compile a small.c file with the following contents:

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

I have a /openssl folder with all those files (and more) in the same location as the.c file, and I see OpenSSL installed in synaptic package manager. I’m trying to build with this:

gcc -o Opentest Opentest.c -lcrypto

However, I constantly get the following 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 merely has a.c extension and no Makefile or./configure.

I already tried:

env CFLAGS=-I/path/to/openssl/

I tried compiling again, but I am getting the same issues.

What should I do to compile with OpenSSL include files?

Asked by jahmax

Solution #1

You should be compiling against the system’s OpenSSL installation, based on your inclusion paths. The.h files should not be in your package directory; instead, they should be picked up from /usr/include/openssl.

The.h files aren’t included in the basic OpenSSL installation (libssl), therefore you’ll need to install the development package as well. On Debian, Ubuntu, and similar distributions, this is called libssl-dev, whereas on CentOS, Fedora, Red Hat, and similar distributions, it’s called libssl-devel.

Answered by caf

Solution #2

To properly gcc, use the -I flag.

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

The -I option should point to the location where the openssl folder is located.

Answered by Borealid

Solution #3

As a solution to the cited task, use the snippet below;

yum install openssl
yum install openssl-devel

On CentOS 5.4, keepalived version 1.2.7, it was tested and found to be effective.

Answered by user2317002

Solution #4

The library path (-L/usr/local/lib/) must be included.

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

It seems to work for me.

Answered by Jeff Pal

Solution #5

If the OpenSSL headers are in the current directory’s openssl subdirectory, run:

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

From the “.” in the -I option and the name supplied in angle brackets, the pre-processor attempts to produce a name such as “./openssl/ssl.h.” You might not have needed to ask the question if you had specified the names in double quotes (#include “openssl/ssl.h”); the Unix compiler searches for headers enclosed in double quotes in the current directory automatically, but not for headers enclosed in angle brackets (#include “openssl/ssl.h>). It is behavior that is defined by the implementation.

You don’t specify the location of the OpenSSL libraries; you may need to add an appropriate option and argument, such as ‘-L /opt/openssl/lib’.

Answered by Jonathan Leffler

Post is based on https://stackoverflow.com/questions/3368683/how-to-compile-c-file-with-openssl-includes