Qt CreatorでCMake+Ninjaを使う (Mac)

環境

  • Mac OS X Mountain Lion
  • Qt 5.3
  • cmake 3.0.1

qmakeからCMakeベースのプロジェクトに移行

まずはCMake, NinjaをHomebrewからインストール

    brew install cmake ninja

qmakeベースのプロジェクトからCMakeベースのプロジェクトに移行するため、下記のようなCMakeLists.txtを作成。

cmake_minimum_required(VERSION 2.8.11)

project(hello)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(hello_SOURCES main.cpp)

# Find the QtWidgets library
find_package(Qt5Widgets REQUIRED)

# Tell CMake to create the helloworld executable
add_executable(hello MACOSX_BUNDLE ${hello_SOURCES})

# Use the Widgets module from Qt 5.
target_link_libraries(hello Qt5::Widgets)

ちなみに下記のサイトを参考にした。
http://www.executionunit.com/blog/2014/01/22/moving-from-qmake-to-cmake/

コマンドラインでビルドしてみる

% cmake CMakeLists.txt -G Ninja
% ninja

エラーがなければhello.appが作成される。

Qt CreatorでCMakeのプロジェクトとしてオープンする

  1. Qt Creatorの設定でCMakeのパスを設定
  2. “Prefer Ninja generator”にチェックを入れる
  3. “File” -> “Open File or Project”でCMakeLists.txtを指定する
  4. 色々聞かれた後”Run CMake”をする時にGeneratorで”Ninja Generator”を選択して”Run CMake”をクリック
  5. 以下のようなエラーが出た場合、Qt CreatorがNinjaを認識していないので、代わりに”Unix Generator”を選択して”Run CMake”をする
  6. 上記ステップでエラーが出た場合、Projectの”Build Environment”のPATHにninjaへのパスが通っているか確認
  7. ビルドディレクトリ(xxx-build)を削除
  8. 再度”File” -> “Open File or Project”でCMakeLists.txtを指定し、”Run CMake”をする時にGeneratorで”Ninja Generator”を選択して”Run CMake”をクリックすると今度は成功する
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file: /Users/shinichi/Code/untitled-build/CMakeFiles/3.0.1/CMakeCCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file: /Users/shinichi/Code/untitled-build/CMakeFiles/3.0.1/CMakeCXXCompiler.cmake
CMake Error at CMakeLists.txt:1 (project):
No CMAKE_C_COMPILER could be found.

Tell CMake where to find the compiler by setting the CMake cache entry
CMAKE_C_COMPILER to the full path to the compiler, or to the compiler name
if it is in the PATH.


CMake Error at CMakeLists.txt:1 (project):
No CMAKE_CXX_COMPILER could be found.

Tell CMake where to find the compiler by setting the CMake cache entry
CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler
name if it is in the PATH.


CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
-- Configuring incomplete, errors occurred!