ant,g++,cygwin/mingw

課題

前日の続きで,今度はC++コンパイル・リンク

対処

環境
gcc.exeの処置

cygwin の /usr/bin/gcc.exe を削除して,Windows7エクスプローラgcc-3.exeのシンボリックリンクgcc.exeを作成する.これは,前日の記事の通り.

main.cpp

#include <iostream>
#include <iomanip>
using namespace std;

main()
{
  cout <<"Hello, world!" <<endl;
}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestTS" default="build"  basedir="."
	 xmlns:cpptasks="antlib:net.sf.antcontrib.cpptasks">

  <target name="build">
    <cpptasks:cc outtype="executable" subsystem="console" outfile="hello">
     <fileset dir="." includes="*.cpp"/>
     <libset libs="stdc++"/>
    </cpptasks:cc>
  </target>

</project>

補遺

つぎのように,g++を指定してビルドすると,g++.exe のリンクも継ぎ直ししなければなりませんので,gcc で代用します.拡張子が「.cpp」,「.cxx」ならばC++としてビルドしてくれるので,その仕組みを利用しました.

  <cpptasks:cc name="g++" ....

それでも(その為?),ライブラリの指定はいるようです.

     <libset libs="stdc++"/>

MinGW g++

mingw でも ant+cygwinでビルドできます.cygwin gcc のように,リンクの継ぎ直しという先々の不安も抱えずに素直にビルドできます.ただ,mingw gcc の要求で,次のように「--enable-auto-import」が必要です.

mingw gccbuild.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestTS" default="build"  basedir="."
	 xmlns:cpptasks="antlib:net.sf.antcontrib.cpptasks">

  <target name="build">
    <cpptasks:cc outtype="executable" subsystem="console" outfile="hello">
     <fileset dir="." includes="*.cpp"/>
     <libset libs="stdc++"/>
     <linkerarg value="--enable-auto-import"/>
    </cpptasks:cc>
  </target>

</project>


先々の不安を抱えるよりも,MinGWを使うほうが良いと思いました.それに,cygwinと同様インストールは実に簡単です.bashからも普通に使えますし,(cygwin gccと異なり)Windowsネイティブでbuildされますが,異論なき場合は願ったり適ったりです.(cygwin gccWindowsネイティブにする場合は,さらに-mno-cygwinオプション指定が必要です.また,mingw gcc でも name="g++" にしたからと言って,上記のライブラリ指定などのオプションが不要になるわけでもありません.)

個人的にMinGWを使うことにしました.取り敢えず,おしまい.