← Back to section · Tutorials & Labs
Linking OpenSSL and liboqs into One Project with CMake
A walkthrough of a working CMakeLists: find_package for OpenSSL, pkg-config for liboqs, imported targets and link order, include paths and build types — with the reason behind every line.
Goal and Overall Approach
To actually run the code from the previous articles you need a build setup that links OpenSSL and liboqs into the same project. This step looks trivial, yet it is where many post-quantum experiment projects fall over: headers found but the link fails, the link passes but the loader picks a different copy of the library at runtime, or the demo works on one machine and fails everywhere else. Modern CMake’s answer is to use imported targets throughout: finding a package produces targets carrying complete properties — include paths, library paths, transitive dependencies — and your code only writes target_link_libraries, never hand-written -I, -L or -l flags. OpenSSL ships an officially maintained CMake config, so it is a plain find_package; liboqs’ installed layout varies more by version and channel, so this article uses the pkg-config route that works across distributions, and clearly marks where the local installation is the authority. The goal for the whole file: on a machine with both libraries installed, cmake -B build && cmake --build build succeeds in one pass.
A Complete CMakeLists, Walked Through
Here is the full file first, then a section-by-section reading (the pkg-config module name liboqs is the common case; if your installation differs, adjust to the config files it provides):
cmake_minimum_required(VERSION 3.16)
project(pq_demo C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
find_package(OpenSSL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBOQS REQUIRED IMPORTED_TARGET liboqs)
add_executable(pq_demo src/main.c)
target_link_libraries(pq_demo PRIVATE OpenSSL::Crypto PkgConfig::LIBOQS)
The key points, in order. find_package(OpenSSL REQUIRED) is CMake’s official module; it yields the imported targets OpenSSL::SSL and OpenSSL::Crypto. Code that only needs EVP and core algorithms links OpenSSL::Crypto; add OpenSSL::SSL for TLS, and it pulls in Crypto as a transitive dependency automatically. pkg_check_modules(... IMPORTED_TARGET liboqs) packages liboqs’ include paths, library paths and dependencies into the PkgConfig::LIBOQS target, which sits next to the OpenSSL target on the link line. List your own target first and libraries after it — the traditional linker requirement; CMake reorders imported targets with dependencies among themselves, but keeping the “depended-upon last” habit costs nothing. The IMPORTED_TARGET keyword is not optional decoration: without it, pkg-config returns a pile of variables and the old include_directories style creeps back in.
The Three Places This Usually Breaks
First, version cross-talk. Development machines often carry both the distribution’s OpenSSL and a hand-built newer one, and find_package may pick the wrong one. Pass -DOPENSSL_ROOT_DIR=/path/to/openssl explicitly, print OPENSSL_VERSION at configure time to confirm, and guard against the dynamic loader picking yet another copy at runtime: on Linux, checking with ldd which libcrypto the executable actually maps should be a fixed pre-delivery step. Second, static versus shared liboqs. Whether a source build of liboqs produces a static or shared library is governed by its BUILD_SHARED_LIBS build option; when linking statically, the OpenSSL symbols liboqs depends on must also be satisfied, and the pkg-config call needs the STATIC keyword or the --static variables in that scenario, or the link fails with a trail of undefined symbols. Third, build type. Single-config generators default to an empty build type — no optimization, no debug info — and cryptographic code can behave differently across optimization levels, especially constant-time code that relies on the compiler not reordering. This file defaults the build type to Release, and CI should additionally run a Debug build with sanitizers (the next article’s topic); both must pass before anything counts as done.
When liboqs Is Built from Source
If liboqs is installed from source, the typical flow is cmake -B build -DCMAKE_INSTALL_PREFIX=$HOME/.local && cmake --build build && cmake --install build — its build system is CMake-based across versions, while optional switches such as algorithm pruning follow that version’s README. With a non-system prefix, pkg-config finds liboqs.pc through the PKG_CONFIG_PATH environment variable, so add $HOME/.local/lib/pkgconfig to it. An even more self-contained approach is to pull the liboqs sources into the same build tree with CMake’s FetchContent or a git submodule and link its library target after add_subdirectory — the target name follows that version’s own CMakeLists, so this article deliberately does not hard-code it. The cost is slower builds; the payoff is a fully pinned version that reproduces on any machine. Submodules suit experiment projects; product projects are better served by system packages with a minimum-version constraint.
A Pre-Delivery Checklist
Fold the sections above into a checklist you can pin above the desk and tick off before every machine change or delivery. First, version verification: the OpenSSL version printed at configure time matches the target, and the libcrypto path shown by ldd points at the intended installation rather than the system’s older copy. Second, algorithm self-check: at startup, the sample program enumerates liboqs’ enabled algorithm list and the names OpenSSL can fetch, confirming that targets such as ML-KEM-768 and ML-DSA-65 are all present — any absence means the linked library is not the build you think it is. Third, a functional loop: run a complete key generation, encapsulation and decapsulation and compare the shared secrets, then a sign and verify round — “it links and starts” is not enough, because problems like a partially installed provider only surface when an algorithm is actually invoked. Fourth, build purity: delete the build directory and reconfigure from scratch to prove the setup does not depend on cache variables left by some earlier manual step, and keep a sanitizer build in CI (the next article’s topic). Fifth, warning discipline: compile with -Wall -Wextra and treat warnings as errors — behind a single “possible truncation” warning in cryptographic code there may be exactly one miscopied size. The checklist costs ten extra minutes per delivery and saves the most morale-destroying class of incident: “it worked on my machine”.
One more easily overlooked piece of experience: record this CMakeLists in the project’s readme together with the minimum versions of both libraries and the verified operating-system and compiler combinations. Most build failures in cryptographic projects are not configuration mistakes but environment drift — six months later a new colleague installs a minor version newer, an algorithm name or a default changes, and the build fails somewhere inexplicable. Treat the “verified combinations” as a project asset, rerun the checklist whenever either library is upgraded, and the build system goes back to being engineering rather than superstition.
Summary
Everything in this setup reduces to: imported targets instead of hand-written flags, find_package for OpenSSL, a pkg-config imported target for liboqs verified against the local installation, an explicit build-type default, and an ldd check of the actually loaded libraries before delivery. Keep this CMakeLists as a template and every later tutorial’s sample code drops straight into src/main.c. The next article turns to runtime quality: using AddressSanitizer and UBSan to catch memory errors in cryptographic code during testing.
Tags: CMakeOpenSSLliboqsEngineering