Home > Software engineering >  CMAKE include src and install directories on different OS
CMAKE include src and install directories on different OS

Time:06-10

I am learning to use CMake and trying to understand where should I place files from different open-source libraries when I download them. I am talking about install location.

On linux include directory is this by convention: /usr/local/include/

What is default location for Windows and Mac OS? I know that these locations can change, but what is the most common location I should place them?

Can something like this work?

install(FILES include/sort/sort.hpp DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${CMAKE_PROJECT_NAME}/sort)

CodePudding user response:

On windows usually the install prefix is something like C:/Program Files/<Package Name>, the usual install prefix on Unix being /usr/local. Usually this directory contains subdirectories like include containing the headers, lib containing (import) libraries, ect..

Using MSVC for the Windows targets those include directories are not automatically available to the compiler.

This is the reason why you should try to use find_package, if package configuration scripts or find scripts are provided by the installation which has the added benefit of also adding any dependencies with just 2 commands in your cmake project (find_package and target_link_libraries). If the package is installed in the default location, find_package should be able to locate the scripts to load without specifying the install location manually.

For packages not providing this kind of info, writing a find script of your own creating imported targets allows for easy reuse.


Can something like this work?

install(FILES include/sort/sort.hpp DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${CMAKE_PROJECT_NAME}/sort)

In general you should avoid hardcoding absolute destination paths into your install paths. Instead use destination paths relative to the install prefix, since the install prefix may be overwritten, e.g. if the user runs the installation via cmake --install <build dir> --prefix <install directory> or when using cpack to generate a package.

Usually you place public headers of your library in a separate directory, even in your sources (which you seem to be doing) and install the directory with

install(DIRECTORY include/sort TYPE INCLUDE)

Resulting in cmake choosing the exact location based on its defaults for the include files for the target system.

CodePudding user response:

Use a cross-platform package manager instead of manually managing the included directories. It handles the differences between different platforms.

vcpkg is one of the best package managers currently available. You can easily use it via project_options.

  • Related