cmakeについて漸く自分でも使ってみようと思いチュートリアルをC++11仕様に勝手に脳内変換しながら読むなどしていた。C++11erがcmakeを学習する際に初めに困る事は恐らく「g++にstd=c++11を渡す方法」だと思う。cmakeを習得済みならば困る事は無いはずだが、何故かチュートリアルのレガシーなC++のソースコードが全自動でC++11ソースにtransformされて見える様な場合は困るだろう。
cmake_minimum_required (VERSION 2.6) project (Tutorial) add_executable(Tutorial tutorial.cxx)
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
以上はcmakeの公式tutorialよりそのまま引用したcmake学習者が最初に学ぶべき"A Basic String Point (Step 1)"である。cmakeのチュートリアルを丸ごとC++11仕様にして邦訳版を作りgh-pagesにでもあげようかとは思ったものの、ドキュメントのライセンスがCC-BY-ND(改変禁止)なので面倒に思いヤメタ・w・
"cmake c++0x"でググるにStackOverflowに「clang++でc++0xしたいができない」的なスレがあった。(おそらく)cmake使いから「ADD_DEFINITIONS("-std=c++0x") を使うといいよ、CXX_FLAGSじゃなくてね。」と回答されている。そんなわけで、C++11erはcmakeを学習するに辺り以下の様な雛形を最初に試すとストレスフリーにチュートリアルを進められるかも。
cmake_minimum_required (VERSION 2.8) project (Tutorial) add_executable (Tutorial tutorial.cxx) add_definition ("-std=c++11")
// A simple program that computes the square root of a number
#include <iostream>
#include <cstdlib>
#include <cmath>
int main (int argc, char *argv[])
{
if (argc < 2)
{
std::cout
<< "Usage: "
<< argv[0]
<< " number"
<< std::endl;
return 1;
}
auto input_value = std::atof(argv[1]);
auto output_value = std::sqrt(input_value);
std::cout
<< "The square root of "
<< input_value " is "
<< output_value
<< std::endl;
}
変態的な事をするわけでなければMakefileをにゃんにゃん書く必要も無いので、多くの通常のソフトウェア開発に於いてCMakeを使う事はとても楽そうですね。ライブラリーのリンク指定なども随分と楽になりますし、これからは積極的にcmake使おうかな、って思ったり・w・