Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CMAKE_INSTALL_PREFIX from CMakeLists.txt file

Tags:

cmake

How do I set CMAKE_INSTALL_PREFIX in my root CMakeLists.txt file?

I have been doing

cmake_minimum_required(VERSION 2.8)
project(MyProject)

# Set default install prefix
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})

with the hopes that by installations would be destined to folders in the source tree. That is,

install(TARGETS my_exe DESTINATION bin/)

would install to ${CMAKE_SOURCE_DIR}/bin/. Instead, it keeps trying to write to /usr/local/bin (the default for Ubuntu 14.04).

I tried the answers to this question, but I still get the standard usr/local/ as my CMAKE_INSTALL_PREFIX when I check CMakeCache.txt.

The only working solution I have is to do

install(TARGETS my_exe DESTINATION "${CMAKE_SOURCE_DIR}/bin/")

but this then removes the user's ability to specify where the bin directory to install is.

tl;dr I would like make install to automatically install to ${CMAKE_SOURCE_DIR} by default, rather than /usr/local/.

like image 819
marcman Avatar asked Sep 14 '16 03:09

marcman


People also ask

What is cmakelists TXT?

CMakeLists.txt. . CMakeLists.txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists.txt file automatically and places it in the project root directory.

Where is the cmakelist config file?

There are two CMakeList.txt file in the example, one at the root and another in the example directory. The one at the root is the main configuration file. Within that config the example directorry is included (using add_subdirectory () ).

Where are the CMake options written?

The options that you choose in the editor are written to a file called CMakeSettings.json. This file provides command-line arguments and environment variables that are passed to CMake when you build the projects.

How do I add additional configuration to a CMake file?

To add an additional configuration, right click CMakeSettings.json and choose Add Configuration. You can also edit the file using the CMake Settings Editor. Right-click on CMakeSettings.json in Solution Explorer and choose Edit CMake Settings. Or, select Manage Configurations from the configuration drop-down at the top of the editor window.


1 Answers

CMake developers suggest to use given pattern for change default value of CMAKE_INSTALL_PREFIX inside CMakeLists.txt:

# Use this snippet *after* PROJECT(xxx):
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment> FORCE)
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

Using that approach

# Use this snippet *before* PROJECT(xxx):
SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment>)

is not recommended:

.. solution depends on the implementation details of the PROJECT command and is very fragile since it works "accidentally" for some versions of CMake. I don't consider it to be an option at all.

like image 123
Tsyvarev Avatar answered Nov 26 '22 07:11

Tsyvarev