[openssl] CMake not able to find OpenSSL library

I am trying to install a software that uses cmake to install itself. When I run cmake .. on the command line, it gives me following error in the CMakeLists.txt on the line that says find_package(OpenSSL REQUIRED):

-- Could NOT find Git (missing:  GIT_EXECUTABLE) 
ZLib include dirs: /usr/include
ZLib libraries: /usr/lib/arm-linux-gnueabihf/libz.so
Compiling with SSL support
CMake Error at /usr/local/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES
  OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/local/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:291 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/share/cmake-2.8/Modules/FindOpenSSL.cmake:313 (find_package_handle_standard_args)
  CMakeLists.txt:436 (find_package)

Here is the part of the file CMakeLists.txt where the error is coming from:

#
# OpenSSL
#
if (WITH_SSL)
    message("Compiling with SSL support")

    if (USE_CYASSL)
        # Use CyaSSL as OpenSSL replacement.
        # TODO: Add a find_package command for this also.
        message("CyaSSL include dir: ${CYASSL_INCLUDE_DIRS}")
        message("CyaSSL libraries: ${CYASSL_LIB}")

        # Additional to the root directory we need to include
        # the cyassl/ subdirectory which contains the OpenSSL
        # compatability layer headers.
        foreach(inc ${CYASSL_INCLUDE_DIRS})
            include_directories(${inc} ${inc}/cyassl)
        endforeach()

        list(APPEND LIB_LIST ${CYASSL_LIB})
    else()
        # TODO: Add support for STATIC also.
        find_package(OpenSSL REQUIRED)

        message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
        message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")

        include_directories(${OPENSSL_INCLUDE_DIR})
        list(APPEND LIB_LIST ${OPENSSL_LIBRARIES})
    endif()
endif(WITH_SSL)

I have OpenSSL installed here:

ssl header is here   -- > /usr/local/ssl/include/openssl/
ssl library is here  -- > /usr/local/ssl/lib/libssl.a
                          /usr/local/ssl/lib/libcrypto.a
openssl is here      -- > /usr/local/ssl/bin

I have in my .profile:

export LD_LIBRARY_PATH=/usr/local/ssl/include/openssl:/usr/lib:/usr/local/lib:/usr/lib/pkgconfig:/usr/local/include/wx-2.8/wx:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
export OPENSSL_ROOT_DIR=/usr/local/ssl
export OPENSSL_LIBRARIES=/usr/local/ssl/lib/

PATH = /usr/local/ssl/bin:$PATH

How can I resolve this error?

EDIT:

Now I am getting this error

/usr/local/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
dso_dlfcn.c:(.text+0x10): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x24): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x30): undefined reference to `dlclose'

This question is related to openssl cmake

The answer is


you are having the FindOpenSSL.cmake file in the cmake module(path usr/shared.cmake-3.5/modules) # Search OpenSSL

 find_package(OpenSSL REQUIRED) 
if( OpenSSL_FOUND )
    include_directories(${OPENSSL_INCLUDE_DIRS})
    link_directories(${OPENSSL_LIBRARIES})
    message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")

target_link_libraries(project_name /path/of/libssl.so /path/of/libcrypto.so)

Please install openssl from below link:
https://code.google.com/p/openssl-for-windows/downloads/list
then set the variables below:

OPENSSL_ROOT_DIR=D:/softwares/visualStudio/openssl-0.9.8k_WIN32
OPENSSL_INCLUDE_DIR=D:/softwares/visualStudio/openssl-0.9.8k_WIN32/include
OPENSSL_LIBRARIES=D:/softwares/visualStudio/openssl-0.9.8k_WIN32/lib

sudo apt install libssl-dev works on ubuntu 18.04.


Note for Fedora 27 users: I had to install openssl-devel package to run the cmake successfully.

sudo dnf install openssl-devel


fixed it on macOS using

brew install openssl
cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_LIBRARIES=/usr/local/opt/openssl/lib

If you're using Ubuntu, run sudo apt install libssl-dev.


This is what I added in the CMakeList.txt (which worked):


# https://cmake.org/cmake/help/latest/command/find_package.html
# in the above link, it states:
# "In Module mode, CMake searches for a file called Find<PackageName>.cmake. 
#  The file is first searched in the CMAKE_MODULE_PATH, then among the Find 
#  Modules provided by the CMake installation. If the file is found, it is
#  read and processed by CMake. It is responsible for finding the package,
#  checking the version, and producing any needed messages. Some find-modules
#  provide limited or no support for versioning; check the module documentation."
#
# FindOpenSSL.cmake can be found in path/to/cmake/Modules
#
# https://cmake.org/cmake/help/latest/module/FindOpenSSL.html
#

find_package(OpenSSL REQUIRED)
if (OPENSSL_FOUND)
  # Add the include directories for compiling
  target_include_directories(${PROJECT_NAME} PUBLIC ${OPENSSL_INCLUDE_DIR})

  # Add the static lib for linking
  target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)

  message(STATUS "Found OpenSSL ${OPENSSL_VERSION}")

else()

  message(STATUS "OpenSSL Not Found")

endif()

Just for fun ill post an alternative working answer for the OP's question:

cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/ -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/

@Morwenn is right. You need to config the openssl DIR. Before that you may need to make sure you have it. you should check whether you have it. first run openssl version,then if you have it you can win + r run openssl and you win find the core dir since it may not name as openssl in your system.

enter image description here

enter image description here


I had the same problem (openssl) and this worked for me on Ubuntu 14.04.1 LTS. The solution is the same up to Ubuntu 18.04 (tested).

sudo apt-get install libssl-dev

Same problem, and fixed it on my centos 6.5 using the following command.

yum install openssl-devel

Just in case...this works for me. Sorry for specific version of OpenSSL, but might be desirable.

# On macOS, search Homebrew for keg-only versions of OpenSSL
# equivalent of # -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/ -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/
if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
    execute_process(
        COMMAND brew --prefix OpenSSL 
        RESULT_VARIABLE BREW_OPENSSL
        OUTPUT_VARIABLE BREW_OPENSSL_PREFIX
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if (BREW_OPENSSL EQUAL 0 AND EXISTS "${BREW_OPENSSL_PREFIX}")
        message(STATUS "Found OpenSSL keg installed by Homebrew at ${BREW_OPENSSL_PREFIX}")
        set(OPENSSL_ROOT_DIR "${BREW_OPENSSL_PREFIX}/")
        set(OPENSSL_INCLUDE_DIR "${BREW_OPENSSL_PREFIX}/include")
        set(OPENSSL_LIBRARIES "${BREW_OPENSSL_PREFIX}/lib")
        set(OPENSSL_CRYPTO_LIBRARY "${BREW_OPENSSL_PREFIX}/lib/libcrypto.dylib")
    endif()
endif()

...

find_package(OpenSSL REQUIRED)
if (OPENSSL_FOUND)
  # Add the include directories for compiling
  target_include_directories(${TARGET_SERVER} PUBLIC ${OPENSSL_INCLUDE_DIR})
  # Add the static lib for linking
  target_link_libraries(${TARGET_SERVER} OpenSSL::SSL OpenSSL::Crypto)
  message(STATUS "Found OpenSSL ${OPENSSL_VERSION}")
else()
  message(STATUS "OpenSSL Not Found")
endif()

If you can use pkg-config: pkg_search_module() can find OpenSSL for you.

# 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()

target_link_libraries(${YOUR_TARGET_HERE} ${OPENSSL_LIBRARIES})