48
42

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.

C++初心者がMacでCMakeを利用してみる

Posted at

AWS SDK for C++ を利用してみようとおもったらCMakeなるものを利用していたので、先にそちらについて調べてみました。(C++ 初心者)

aws/aws-sdk-cpp: AWS SDK for C++
https://github.com/aws/aws-sdk-cpp

CMakeとは

CMake - Wikipedia
https://ja.wikipedia.org/wiki/CMake

CMakeはコンパイラに依存しないビルド自動化のためのフリーソフトウェアであり、様々なオペレーティングシステムで動作させることができる。CMakeは階層化ディレクトリや複数のライブラリを利用するアプリケーションをサポートするよう設計されている。実際のビルドにおいては、make、Xcode、Visual Studioのようなネイティブのビルド環境が利用される。CMake自身は最小限の依存関係を持つよう設計されており、ビルドするにはC++コンパイラのみを必要とする[4]。

g++コマンドでコンパイルするよりも超便利って認識で良いのかな。

検証環境

MacBook Proでgccなどはそのままな環境です。

> sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.6
BuildVersion:   17G4015

> gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

CMakeをMacにインストール

CMakeをMacにインストールするには、Homebrewが利用できるようです。

Monobit Unity Networking (MUN) official document
http://www.monobitengine.com/doc/mun/contents/SetupServer_MacOSX/Cpp/Install_CMake.htm

> brew install cmake
()
==> Summary
🍺  /usr/local/Cellar/cmake/3.13.3: 5,543 files, 50.8MB

インストールできたらコマンドを叩いてみます。

> cmake
Usage

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>
  cmake [options] -S <path-to-source> -B <path-to-build>

Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to
re-generate its build system.

Run 'cmake --help' for more information.

CMakeを利用してみる

下記を参考に利用させてもらいました。

複数ソースファイルがある方がCMakeの便利さがわかるっぽいのですが、まだ*.hppファイルなにそれ?おいしいの?な初心者なので、単一ファイルです!

ごく簡単なcmakeの使い方 - Qiita
https://qiita.com/termoshtt/items/539541c180dfc40a1189

> mkdir 任意のディレクトリ
> cd 任意のディレクトリ
> touch main.cpp
> touch CMakeLists.txt
main.cpp
#include <iostream>

int main()
{
    std::cout << "Hello! World!" << std::endl;
    return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_executable(Main main.cpp)

必要なファイルが用意できたら、CMakeでコンパイルしてみます。

> cmake .

-- The C compiler identification is AppleClang 10.0.0.10001044
-- The CXX compiler identification is AppleClang 10.0.0.10001044
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /任意のディレクトリ

なにやらいろんなファイルが生成されました。

> ls -l
total 72
-rw-r--r--   1 hoge  Users  13295  1 22 13:00 CMakeCache.txt
drwxr-xr-x  15 hoge  Users    480  1 22 13:00 CMakeFiles
-rw-r--r--   1 hoge  Users     65  1 22 12:15 CMakeLists.txt
-rw-r--r--   1 hoge  Users   4820  1 22 13:00 Makefile
-rw-r--r--   1 hoge  Users   1377  1 22 13:00 cmake_install.cmake
-rw-r--r--   1 hoge  Users     96  1 22 12:59 main.cpp

こちらの記事によると、CMakeを利用してコンパイルする場合には、専用のディレクトリ内で実行するのが良いとのことです。

CMakeの使い方(その1) - Qiita
https://qiita.com/shohirose/items/45fb49c6b429e8b204ac

なぜなのか気になって調べてみたら、下記が詳しかったです。

CMake : out-of-sourceビルドで幸せになる - Qiita
https://qiita.com/osamu0329/items/7de2b190df3cfb4ad0ca

ソースファイルと同じディレクトリ内で実行しちゃだめってわけではなさそうなので、今回はそのまますすめます。

CMakeコマンドで出力されるファイルについては、Makefileが自動生成されてウマーくらいの認識でよさそう?ほんとに?

実行ファイルを作成するのに、makeコマンドを実行します。

> make
Scanning dependencies of target Main
[ 50%] Building CXX object CMakeFiles/Main.dir/main.cpp.o
[100%] Linking CXX executable Main
[100%] Built target Main

これでMainファイルが作成されました。
実行してみます。

> ./Main
Hello! World!

おおー。実行できました。

まとめ

今回みたいなシンプルな構成だと、g++コマンドだけでもよさそうですが、構成が複雑になってくるとCMakeを利用するのが良さそうです。

> g++ -o Main2 main.cpp
> ./Main2
Hello! World!

参考

CMake - Wikipedia
https://ja.wikipedia.org/wiki/CMake

Monobit Unity Networking (MUN) official document
http://www.monobitengine.com/doc/mun/contents/SetupServer_MacOSX/Cpp/Install_CMake.htm

ごく簡単なcmakeの使い方 - Qiita
https://qiita.com/termoshtt/items/539541c180dfc40a1189

CMakeの使い方(その1) - Qiita
https://qiita.com/shohirose/items/45fb49c6b429e8b204ac

CMake : out-of-sourceビルドで幸せになる - Qiita
https://qiita.com/osamu0329/items/7de2b190df3cfb4ad0ca

48
42
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
48
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?