Simple Ant build file to build and run cpp files

I’ve tried to look for a simple ant build file for compiling c++ files however I’ve had no joy. After couple of hours or so I’ve made my own. It is basic for now however I’m going to update it later. Anyhoo, here it is if you are interested.

<project name="builder" default="build" basedir=".">

	<taskdef resource="net/sf/antcontrib/"/>

	<property name="src.dir" value="src"/>
	<property name="build.dir" value="bin"/>
	<property name="etc.dir" value="etc"/>
	<property name="resultset" value=""/>

	<target name="init">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${src.dir}"/>
		<mkdir dir="${etc.dir}"/>
	<target name="build" depends="init">  
		<delete file="main"/>
		<foreach param="" target="">
				<fileset dir="${src.dir}" includes="*.cpp"/>  

	<target name="">
		<propertyregex override="yes"
			property="filename" input="${}"
			regexp=".*/(.*).cpp" replace="\1"/>
		<delete file="${build.dir}/${filename}.o"/>
		<echo message="building ${filename}.cpp"/>
		<exec executable="g++">
			<arg value="-Wall"/>
			<arg value="-c"/>
			<arg value="${src.dir}/${filename}.cpp"/>
			<arg value="-o"/>
			<arg value="${build.dir}/${filename}.o"/>

	<target name="make">
		<delete file="${etc.dir}/"/>
		<echo file="${etc.dir}/" append="true" message="path="/> 
		<foreach param="param.make" target="do.make">
				<fileset dir="${build.dir}" includes="*.o"/>  

	<target name="do.make">
		<propertyregex override="yes"
				property="filename" input="${param.make}"
				regexp=".*/(.*).o" replace="\1"/>
		<echo file="${etc.dir}/" append="true" message="${build.dir}/${filename}.o "/> 

	<target name="run" depends="make">
		<property prefix="make" file="${etc.dir}/"/>
		<echo message="${make.path}"/>
		<exec command="g++ ${make.path} -o main"/>
		<exec executable="./main"/>

One thought on “Simple Ant build file to build and run cpp files

    D:\Team City Projects\Testing\C_Final.xml:19: The following error occurred while executing this line:
    D:\Team City Projects\Testing\C_Final.xml:29: Execute failed: Cannot run program “g++”: CreateProcess
    error=2, The system cannot find the file specified

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s