こんにちは。
CMake を使っていると度々エラーに遭遇しますが、今回はよく発生するパッケージが見つからないエラーの対処法を記載します。
状況
apt 等でインストールできるパッケージの場合は CMake 関連のファイルもシステムにインストールされると思いますが、git 等から直接ダウンロードする場合は CMake のインストールができていない可能性があります。
システムから認識されていないパッケージを CMakeLists.txt の find_package で使おうとすると、以下のようなエラーが発生します。
以降では例として、該当パッケージ名を example_pkg とします。
CMake Error at CMakeLists.txt:1 (find_package):
By not providing "Findexample_pkg.cmake" in CMAKE_MODULE_PATH this
project has asked CMake to find a package configuration file provided by
"example_pkg", but CMake did not find one.
Could not find a package configuration file provided by "example_pkg"
with any of the following names:
example_pkgConfig.cmake
example_pkg-config.cmake
Add the installation prefix of "example_pkg" to CMAKE_PREFIX_PATH or set
"example_pkg_DIR" to a directory containing one of the above files. If
"example_pkg" provides a separate development package or SDK, be sure it
has been installed.
CMakeLists.txt 内のエラー該当箇所
find_package(example_pkg REQUIRED)
上記エラーは CMake に必要なファイルがシステムにインストールされていない(/usr/local/lib/cmake/example_pkg/example_pkgConfig.cmake が生成されていない)ために起こっているエラーです。
解決策
まず、パッケージがあるディレクトリに入ります。
$ git clone "....../example_pkg.git"
$ cd example_pkg
CMake に対応しているパッケージでは、CMakeLists.txt がディレクトリ内に存在しているはずです。
(存在していない場合は、CMakeLists.txt があるディレクトリまで移動しておきます)
この CMake を使ってシステムにパッケージをインストールします。
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
上記のように、新たに作成した build ディレクトリ内で cmake によって Makefile を作成し、インストールします。
インストール時にはスーパーユーザの権限が必要(上記コマンドの最終行では sudo が必要)なので注意してください。
これにより、/usr/local/include/example_pkg/ 以下のヘッダファイルや /usr/local/lib/libexample_pkg.so が生成されるだけでなく、/usr/local/lib/cmake/example_pkg/ 以下に CMake 関連のファイルが生成されます。
以上で、CMake のエラーが起こらなくなります。
因みに、生成されたファイル一覧は build ディレクトリ内の install_manifest.txt で確認できます。
パッケージのアンインストール
一度インストールしたパッケージを完全に消去したい場合、build ディレクトリ内で以下を実行します。
$ sudo make uninstall
これで、make install によって生成されたファイルが完全に消去されます。
最後に
CMake の find_package でパッケージが見つからないエラーの対処法を紹介しました。
質問や要望等があれば、コメント欄にてお知らせください。
コメント
An intriguing discussion is worth comment. I believe that you should publish more
on this subject matter, it might not be a taboo subject but typically people don’t
talk about these subjects. To the next! Kind regards!!
Hello, Neat post. There’s an issue together with your site in internet explorer, might check this… IE nonetheless is the market leader and a huge component to folks will pass over your magnificent writing due to this problem.
Great beat ! I wish to apprentice whilst you amend your site, how could i subscribe for a weblog web site? The account aided me a acceptable deal. I have been a little bit familiar of this your broadcast provided brilliant transparent concept
Hi there to all, how is the whole thing, I think every one is getting more
from this website, and your views are fastidious for new people.