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/antcontrib.properties"/>

	<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>
	
	<target name="build" depends="init">  
		<delete file="main"/>
		<foreach param="param.build" target="do.build">
			<path>  
				<fileset dir="${src.dir}" includes="*.cpp"/>  
			</path>
		</foreach>
	</target> 

	<target name="do.build">
		<propertyregex override="yes"
			property="filename" input="${param.build}"
			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"/>
		</exec>
	</target>

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

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

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

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

  1. BUILD FAILED
    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: java.io.IOException: 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:

WordPress.com Logo

You are commenting using your WordPress.com 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