LoginSignup
8
10

More than 1 year has passed since last update.

WindowsでCMakeをするために

Last updated at Posted at 2020-08-20

アブストラクト

MSYS2を使うとWindowsでも簡単にCMakeができます。
(詳しくは対処法の項を参照)

ある晴れた昼下がり

macが壊れました。
急遽Windowsを発掘して作業する必要に迫られました。

Windowsにcmakeを入れて遊ぼうとする

いつも通りCMakeのサイトからCMakeを入れ、C++のプログラムをコマンドプロンプトでCMakeしようとしました。
ソースコードな以下の通りです。

test.cpp
#include<iostream>
int main(){
  std::cout << "Hello World!!" << std::endl;
  return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(test)
add_executable(test test.cpp)

cmakeをしてみると次のようなエラーが出ます。

-- The CXX compiler identification is unknown
...

どうやらCMAKE_CXX_COMPILERの設定が無いようなので、セットします。
今回はMingwのg++を使うことにしました。

CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_COMPILER C:¥Mingw¥bin¥g++)
project(test)
add_executable(test test.cpp)

すると、次のようなエラーが出ます。

...
  when parsing string
    C:¥Mingw¥bin¥g++
  Invalid character escape '¥M'
...

¥マークが悪さをしているようです。
調べてみると、'¥'や''ではなく'/'を使うべきらしいので書き換えます。

CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_COMPILER C:/Mingw/bin/g++)
project(test)
add_executable(test test.cpp)

すると、次のようなエラーが出ます。

...
  The CMAKE_CXX_COMPIER:
    C:/Mingw/bin/g++
  is not a full path to an existing compiler tool.
...

C:/Mingw/bin/g++が見つからない...?
仕方がないので、g++をセットします。

CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_COMPILER g++)
project(test)
add_executable(test test.cpp)

すると、次のようなエラーが出ます。

...
  The C++ compiler:
    C:/Mingw/bin/g++
  is not able to compile a simple test program.
...

g++が動かないようです。
当然g++ test.cppは動きます。
この辺りで、コマンドプロンプトでCMakeをするのを諦めました。

対処法

MSYS2を使います。使い方はこちらの記事に詳しくありました。

手順

1 . MSYS2をインストールする(https://www.msys2.org/)

2 . MSYS2 MinGW64 shellで開発環境をインストールする

pacman -Syuu
pacman -S base-devel
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw64/mingw-w64-x86_64-cmake

3 . MSYS2 MinGW64 shell上でCMakeを行う。その際、オプション-G "MSYS Makefiles"を付ける。

cmake .. -G "MSYS Makefiles"

これでめでたくCMakeができました。

8
10
1

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
8
10