10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CMAKE_INSTALL_PREFIXの初期値を変更する

Last updated at Posted at 2019-01-17

CMakeの変数CMAKE_INSTALL_PREFIXは実行時に引数で渡せば変更可能だが、デフォルト値を変えたい場合にどうするか、という話。

解決方法

CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULTという変数を使う。
https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT.html

この変数はユーザによってCMAKE_INSTALL_PREFIXが変更されていない場合にTRUEになるため、以下のように利用するとデフォルト値を変えることができる。

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX /path/to/default CACHE PATH "..." FORCE)
endif()

ダメな例

その1

以下のように書いた場合、CMAKE_INSTALL_PREFIXはそもそも定義済みなため、if文の条件を満たせない。

if(NOT DEFINED CMAKE_INSTALL_PREFIX)
  set(CMAKE_INSTALL_PREFIX /path/to/default CACHE PATH "..." FORCE)
endif()

その2

間違ってはいないがUNIX系でしか動かない(Windows用の設定を書けばいいという話ではない)。

if(CMAKE_INSTALL_PREFIX STREQUAL "/usr/local")
  set(CMAKE_INSTALL_PREFIX /path/to/default CACHE PATH "..." FORCE)
endif()
10
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?