Compiling C programs using libssl on OS X El Capitan?

gogo_gorilla picture gogo_gorilla · Nov 4, 2015 · Viewed 15k times · Source

I have a simple C program using libssl.

On Linux, I installed the openssl-dev package and compiled the program with the following line:

gcc test_libssl.c -o test_libssl -lcrypto -lssl

Now I would like to do the same on my Mac. The same line resulted in:

fatal error: 'openssl/conf.h' file not found

I tried installing openssl (openssl-dev did not work) with home-brew via brew install openssl

This gave me:

...
==> Installing openssl
==> Downloading https://www.openssl.org/source/openssl-1.0.2a.tar.gz curl: (22) The requested URL returned error: 404 Not Found

I found a related SO question with no answer.

I also tried

brew info openssl

and got informed that

This formula is keg-only. Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

What do I have to do / install in order to be able to compile libssl-using C programs programs on OS X?

Or, is it a bad idea the first place (given the warning above)?




UPDATE:

I got the openssl installed using brew. I am not sure if this was the problem but I updated brew. Taking brew's advice

You should probably change the ownership and permissions of /usr/local back to your user account. sudo chown -R $(whoami):admin /usr/local

and this issue into account.

Then, following @Alex Reynolds's advice, I compiled it successfully with

gcc test_libssl.c -o test_libssl -lssl -lcrypto -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include

Answer

Alex Reynolds picture Alex Reynolds · Nov 4, 2015

I have Homebrew installed on El Capitan (10.11.1) and have installed a current version of OpenSSL with no apparent ill effects:

$ uname -a
Darwin hostname.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64

$ brew info openssl
openssl: stable 1.0.2d (bottled)
OpenSSL SSL/TLS cryptography library
https://openssl.org/

This formula is keg-only.
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

/usr/local/Cellar/openssl/1.0.2d_1 (464 files, 17M)
  Built from source
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/openssl.rb
==> Dependencies
Build: makedepend ✔
==> Options
--universal
    Build a universal binary
--without-check
    Skip build-time tests (not recommended)
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

Have you tried adding the flags that it suggests to your app's build statement? You might edit your app's makefile or other build statement and add those entries, after a brew install openssl. This may help your compiler find and link the library and header files it needs.

Looks like everything is there. Here are headers:

$ ls -al /usr/local/opt/openssl/include/openssl/
total 3688
drwxr-xr-x  77 alexpreynolds  admin    2618 Aug 24 13:46 .
drwxr-xr-x   3 alexpreynolds  admin     102 Aug 24 13:46 ..
-rw-r--r--   1 alexpreynolds  admin    6182 Aug 24 13:46 aes.h
-rw-r--r--   1 alexpreynolds  admin   63142 Aug 24 13:46 asn1.h
-rw-r--r--   1 alexpreynolds  admin   24435 Aug 24 13:46 asn1_mac.h
-rw-r--r--   1 alexpreynolds  admin   34475 Aug 24 13:46 asn1t.h
-rw-r--r--   1 alexpreynolds  admin   38566 Aug 24 13:46 bio.h
-rw-r--r--   1 alexpreynolds  admin    5351 Aug 24 13:46 blowfish.h
...

And static and dynamic libraries:

$ ls -al /usr/local/opt/openssl/lib
total 11664
drwxr-xr-x  10 alexpreynolds  admin      340 Aug 24 13:46 .
drwxr-xr-x  11 alexpreynolds  admin      374 Aug 24 13:46 ..
drwxr-xr-x  14 alexpreynolds  admin      476 Aug 24 13:46 engines
-r--r--r--   1 alexpreynolds  admin  1861780 Aug 24 13:46 libcrypto.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin  3206344 Aug 24 13:46 libcrypto.a
lrwxr-xr-x   1 alexpreynolds  admin       21 Aug 24 13:46 libcrypto.dylib -> libcrypto.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin   364144 Aug 24 13:46 libssl.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin   524424 Aug 24 13:46 libssl.a
lrwxr-xr-x   1 alexpreynolds  admin       18 Aug 24 13:46 libssl.dylib -> libssl.1.0.0.dylib
drwxr-xr-x   5 alexpreynolds  admin      170 Aug 24 13:46 pkgconfig