11

I am using the following cmake commands

# Search OpenSSL
find_package(PkgConfig REQUIRED)
pkg_search_module(OPENSSL REQUIRED openssl)

if( OPENSSL_FOUND )

    include_directories(${OPENSSL_INCLUDE_DIRS})
    message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
else()
    # Error; with REQUIRED, pkg_search_module() will throw an error by it's own
endif()

it works on Linux and on Mac, but on Mac it uses the osx-shipped libssl - which throws a a lot of deprecation warnings e.g. 'SSL_library_init' is deprecated: first deprecated in OS X 10.7"

using brew I already installed a newer - openssl-offical - libssl - how can I tell the pkg_search_module in cmake to find and use the brew version?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Helmut Januschka
  • 1,578
  • 3
  • 16
  • 34
  • You could use `FIND_PACKAGE(OpenSSL)` and specify the location of your OpenSSL installation like it is described here: http://stackoverflow.com/a/16249265/678093 – m.s. Apr 22 '15 at 08:42

6 Answers6

18

As of late 2016 this works for me:

In CMakeLists.txt:

find_package(openssl REQUIRED)

Run cmake like this:

cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl .
Kornel
  • 97,764
  • 37
  • 219
  • 309
Jonatan
  • 3,752
  • 4
  • 36
  • 47
  • 3
    To be version independent the following works: `-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl` – zcourts Mar 27 '17 at 18:56
  • Just doing `find_package(openssl REQUIRED)` works on cmake 3.10. Had issues on 3.16, so switched back to 3.10. – sb32134 May 21 '20 at 19:49
8

ok got it working :)

brew upgrade openssl
brew link --force openssl
pkg-config --modversion openssl
#1.0.2

removed the cmake build folder and rerun the cmake .. and the above macro now finds the 1.0.2 libssl :)

Helmut Januschka
  • 1,578
  • 3
  • 16
  • 34
  • 8
    This does not work anymore: Warning: Refusing to link: openssl Linking keg-only openssl means you may end up linking against the insecure, deprecated system OpenSSL while using the headers from Homebrew's openssl. – Jonatan Nov 01 '16 at 09:02
  • `export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"` before `pkg-config --modversion openssl` in order for it to work – ljmocic Feb 11 '20 at 15:19
1

Jonathan is right. The MacOS system open ssl is considered insecure. Here is what works for me

  1. Install or upgrade openssl via brew

  2. Add these to your CMakefile. Instead of hard coding you might choose to use a command line parameter or environment variable

    include_directories(BEFORE /usr/local/Cellar/openssl/1.0.2p/include) find_library(OPENSSL_LIB ssl PATHS /usr/local/Cellar/openssl/1.0.2p/lib NO_DEFAULT_PATH) find_library(CRYPTO_LIB crypto PATHS /usr/local/Cellar/openssl/1.0.2p/lib NO_DEFAULT_PATH)

To find the OpenSSL directory use the following command:

brew list openssl
Vlad
  • 9,180
  • 5
  • 48
  • 67
1

The cause of this issue is a bug in CMake -- it does not use alternate pkg-config paths correctly.

According to the merge request attached to the bug, the fix should be in cmake 3.17.0 (to be released in Feb 2020).

Otherwise, use this work-around. Hard coding it in your CMakeLists.txt will make things bad for people who use MacPorts instead of Homebrew.

micolous
  • 11
  • 3
0

As of late 2021 this works for me:

change to

cmake_minimum_required(VERSION 3.20.2) 

and

find_package(OpenSSL REQUIRED) 
hannes ach
  • 16,247
  • 7
  • 61
  • 84
0

If you have install openssl of specify version by brew, let's say, openssl@1.1, then you can use

brew info openssl@1.1

and it will tell you all you need to do:

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"
Eki
  • 641
  • 5
  • 14