Skip to content

Install the library and a CMake package - #75

Open
dmeoli wants to merge 1 commit into
oxfordcontrol:mainfrom
dmeoli:cmake-install
Open

dmeoli wants to merge 1 commit into
oxfordcontrol:mainfrom
dmeoli:cmake-install

Conversation

@dmeoli

@dmeoli dmeoli commented Oct 19, 2026

Copy link
Copy Markdown

At the moment cmake --install does nothing, so a project that wants to link Clarabel.cpp without adding it as a subdirectory has to copy the headers and rust_wrapper/target/release/libclarabel_c.* by hand and write its own find module. I use Clarabel.cpp as a QP backend of a C++ library (SMS++) and of its packages, where this is what I ended up doing.

This PR adds:

  • install() rules for the headers (include/ as it is), the shared and the static library, with GNUInstallDirs, and on Windows the DLL in bin;
  • a package lib/cmake/Clarabel/ClarabelConfig.cmake (plus the version file) that defines the imported targets Clarabel::clarabel_c and Clarabel::clarabel_c_static, with the include directory, the FEATURE_* definitions of the enabled cargo features, and Eigen3::Eigen if Eigen is found (the C++ headers need it, the C ones do not);
  • the same two names as aliases of libclarabel_c_shared and libclarabel_c_static, so that a consumer is written the same way with find_package and with add_subdirectory;
  • the option CLARABEL_BUILD_EXAMPLES (default ON, as now); since the examples were the only targets depending on the cargo target, libclarabel_c is now built by ALL, otherwise with the examples off nothing would trigger cargo;
  • a few lines on installation in the README.

Two details I ran into while testing:

  • the static library needs the system libraries of the Rust standard library (cargo rustc -- --print native-static-libs gives -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc here). Without them, linking an executable to the current libclarabel_c_static fails with undefined references to log, exp and so on, so on Linux both libclarabel_c_static and the installed static target now link Threads::Threads ${CMAKE_DL_LIBS} m rt util. I did not add a list for macOS and Windows, which I cannot test;
  • libclarabel_c.so has no SONAME, so linking it by full path records that absolute path in the executable; the installed target is marked IMPORTED_NO_SONAME, and the executable records libclarabel_c.so, found through its RUNPATH.

I tested it on Linux (Ubuntu 24.04, GCC 13.3, CMake 3.28, Rust stable) with a separate project built against the installed tree, which compiles examples/c/example_qp.c against both targets and examples/cpp/example_qp.cpp against the shared one (all three solve), and with the same C example linked to Clarabel::clarabel_c_static through add_subdirectory. The default build with the examples is unchanged, and -DCLARABEL_CARGO_FEATURES=serde,faer-sparse gives FEATURE_FAER_SPARSE;FEATURE_SERDE in the installed package.

#72 also adds install rules, for the use in acados; this PR installs the headers and a package with targets as well, and keeps the build directory logic as it is.

cmake --install now installs the headers, the shared and the static
library and a package (ClarabelConfig.cmake and its version file) that
defines the imported targets Clarabel::clarabel_c and
Clarabel::clarabel_c_static, so that another project can use an
installed Clarabel.cpp with find_package(Clarabel). The same names are
aliases of the two interface targets when Clarabel.cpp is added with
add_subdirectory.

The cargo target is built by default, since the examples, which were
the only targets depending on it, can now be left out with
CLARABEL_BUILD_EXAMPLES=OFF. The compile definitions of the enabled
features are passed on to the installed targets.

The static library also needs the system libraries of the Rust standard
library: on Linux both libclarabel_c_static and the installed static
target now link them, while before any executable linked to
libclarabel_c_static failed with undefined references to log, exp and
the like.

The shared library has no SONAME, so the installed target is marked
IMPORTED_NO_SONAME, and executables record libclarabel_c.so rather than
the absolute path of the library.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant