How can I configure my CMake project to install dependency packages?

495 Views Asked by At

I am trying to distribute a recent project - but need a way to download a few dependencies if the target OS is UNIX:

  if (UNIX)
      message("Detected Linux OS - install required")
      #Install libs here
  endif (UNIX)

I don't want to use a system command like apt since naturally different systems will have different package managers - is there a way I can install apt packages (such as libvorbis-dev) directly?

1

There are 1 best solutions below

0
starball On

Since you mentioned that all the libraries your project needs are on vcpkg, I'd suggest to just use vcpkg. You can read about how to use vcpkg with CMake in their docs on that topic.

You could also take a look at Conan Center and see if it has the libraries you want.

If you want the dependencies to be built from scratch instead of using a package manager that downloads pre-built binaries, you can take a look at FetchContent (and see also CMake's "Using Dependencies Guide").

Using a package manager might be simpler than trying to shoehorn system package manager installation commands into your CMake configuration phase (which is a pretty unconventional thing to do. Usually when it comes to packages from system package managers, CMake configurations just expect that the package has already been installed, with documentation telling build-users to manually install dependencies however they wish, with the CMake configuration just searching for installed packages using find_package. I think your users might even find it to be a bad kind of surprising if running configuration for your project suddenly requests installing a system package).