Adding OpenSSL to your CMake project is a common task. CMake provides a built-in FindOpenSSL module that makes this relatively straightforward.
Here's a step-by-step guide, covering the most common scenarios.
Prerequisites: OpenSSL Installation
Before you can add OpenSSL to your CMake project, OpenSSL itself must be installed on your system.
Method 1: Using find_package(OpenSSL) (Recommended)
This is the standard and most robust way. CMake's FindOpenSSL module is designed to locate OpenSSL installations across various platforms.
1. CMakeLists.txt Example
# Minimum CMake version required
cmake_minimum_required(VERSION 3.10)
# Project name
project(MyOpenSSLApp CXX)
# Find OpenSSL
# REQUIRED means CMake will abort if OpenSSL is not found.
# COMPONENTS specifies which parts you need (SSL and Crypto are typical).
find_package(OpenSSL REQUIRED COMPONENTS SSL Crypto)
# Check if OpenSSL was found successfully
if(OPENSSL_FOUND)
message(STATUS "OpenSSL found: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL libraries: ${OPENSSL_LIBRARIES}")
else()
message(FATAL_ERROR "OpenSSL not found. Please install it or set OPENSSL_ROOT_DIR.")
endif()
# Add your executable target
add_executable(my_openssl_app main.cpp)
# Link OpenSSL to your executable
# OpenSSL provides imported targets (OpenSSL::SSL, OpenSSL::Crypto) which are
# preferred as they automatically include the correct include directories and link flags.
target_link_libraries(my_openssl_app PRIVATE OpenSSL::SSL OpenSSL::Crypto)
# For older CMake versions or if imported targets aren't available,
# you might use:
# target_link_libraries(my_openssl_app PRIVATE ${OPENSSL_LIBRARIES})
# target_include_directories(my_openssl_app PRIVATE ${OPENSSL_INCLUDE_DIR})
2. main.cpp Example (for testing)
#include <iostream>
#include <openssl/ssl.h>
#include <openssl/crypto.h>
int main() {
std::cout << "Using OpenSSL version: " << SSLeay_version(SSLEAY_VERSION) << std::endl;
std::cout << "Built against OpenSSL version: " << OPENSSL_VERSION_TEXT << std::endl;
// A simple check (example for using a crypto function)
long v = OpenSSL_version_num();
std::cout << "OpenSSL_version_num(): " << std::hex << v << std::endl;
return 0;
}
3. How to Build (General Steps)
mkdir build
cd build
cmake ..
cmake --build .
# On Windows, you might open the generated .sln file in Visual Studio
# Or for single-config generators: cmake --build . --config Release
./my_openssl_app # Or .\Debug\my_openssl_app.exe on Windows
Troubleshooting find_package(OpenSSL)
If find_package(OpenSSL) fails, here are common reasons and solutions:
-
OpenSSL Not Installed: Double-check the prerequisites for your OS.
-
OpenSSL Not in Standard Path:
-
Vcpkg Integration: If you installed OpenSSL via Vcpkg, you need to tell CMake to use Vcpkg's toolchain file.
# In your build directory (after vcpkg install openssl)
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/your/vcpkg/scripts/buildsystems/vcpkg.cmake
This will automatically handle finding OpenSSL (and other libraries) installed through Vcpkg.
-
Static vs. Shared Libraries: By default, FindOpenSSL prefers shared libraries. If you only have static libraries or want to explicitly link static:
Method 2: Manual Finding (Less Recommended, for Edge Cases)
If find_package(OpenSSL) consistently fails even with hints, or you have a very unusual OpenSSL setup, you can manually find the libraries and include directories. This is more fragile and less portable.
cmake_minimum_required(VERSION 3.10)
project(MyOpenSSLAppManual CXX)
# Manually specify path (replace with your actual paths)
set(OPENSSL_ROOT_PATH "/path/to/your/openssl/installation")
# Find include directory
find_path(OPENSSL_INCLUDE_DIR
NAMES openssl/ssl.h
PATHS "${OPENSSL_ROOT_PATH}/include"
NO_DEFAULT_PATH # Only search in specified paths
)
# Find SSL library
find_library(OPENSSL_SSL_LIBRARY
NAMES ssl ssleay32
PATHS "${OPENSSL_ROOT_PATH}/lib" "${OPENSSL_ROOT_PATH}/lib64"
NO_DEFAULT_PATH
)
# Find Crypto library
find_library(OPENSSL_CRYPTO_LIBRARY
NAMES crypto libeay32
PATHS "${OPENSSL_ROOT_PATH}/lib" "${OPENSSL_ROOT_PATH}/lib64"
NO_DEFAULT_PATH
)
# Combine libraries
set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
# Check if everything was found
if(OPENSSL_INCLUDE_DIR AND OPENSSL_LIBRARIES)
message(STATUS "OpenSSL manually found: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL libraries: ${OPENSSL_LIBRARIES}")
else()
message(FATAL_ERROR "OpenSSL manual find failed. Check paths.")
endif()
add_executable(my_openssl_app_manual main.cpp)
# Link and include
target_include_directories(my_openssl_app_manual PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(my_openssl_app_manual PRIVATE ${OPENSSL_LIBRARIES})
Key Takeaways:
- Always try
find_package(OpenSSL REQUIRED COMPONENTS SSL Crypto) first.
- Use
target_link_libraries(your_target PRIVATE OpenSSL::SSL OpenSSL::Crypto) for robust linking.
- If
find_package fails, try hinting OPENSSL_ROOT_DIR or using Vcpkg.
- Ensure OpenSSL is correctly installed on your system.