Creating a Skeleton C++ Project using GNU autotools

Installing the Software

Ensure you have a working system with autoconf automake make g++ gcc etc... Debian packages can be installed with the following command:

# apt-get install build-essential automake autoconf

or if your using aptitude...

# aptitude install build-essential automake autoconf


Creating the Script

Run the script below to create your new project in your current directory. To do this you will need to copy and paste the script into a file using your preferred text editor and then make the script executable:

$ chmod +x createproject

...substitute createproject with the filename of the script you just created.


Running the Script

Now run the script:

$ ./createproject

Once the script is complete you can start editing your C++ source (Main.cpp) and simply issue the command

$ make


Other Methods

For a more complete but slightly more complex solution you can use GNU autoproject. http://www.gnu.org/directory/All_Packages_in_Directory/autoproject.html

Most Integrated Development Environments (IDEs) will do all this for you such as KDevelop.


The Script

cut and paste from the next line down --->

 1. Create a new C++ Automake Project Folder
 1. Create project directory and misc. files ...
echo "Please enter a project name (no spaces) ..."
read projectname
mkdir -p "$projectname"
cd "$projectname"
mkdir src
touch NEWS README AUTHORS [[ChangeLog]]

 1. Create skeleton C++ code
echo '#include <string>' > src/Main.cpp
echo '#include <iostream>' >> src/Main.cpp
echo '' >> src/Main.cpp
echo 'int main(int argc, char *argv[]){' >> src/Main.cpp
echo '  std::cout << "Project skeleton code succesfully compiled and executed." << std::endl;' >> src/Main.cpp
echo '  return 0;' >> src/Main.cpp
echo '}' >> src/Main.cpp

 1. Create configure.ac ...
echo 'AC_INIT(src/Main.cpp)' > configure.ac
echo 'AM_INIT_AUTOMAKE('$projectname',0.1)' >> configure.ac
echo '#AM_CONFIG_HEADER(config.h)' >> configure.ac
echo 'AC_PROG_CC' >> configure.ac
echo 'AC_PROG_CXX' >> configure.ac
echo 'AC_PROG_INSTALL' >> configure.ac
echo '#AC_PROG_LIBTOOL' >> configure.ac
echo 'AC_OUTPUT(Makefile src/Makefile)' >> configure.ac

 1. Create Makefile.am ...
echo 'SUBDIRS = src' > Makefile.am
echo 'EXTRA_DIST =' >> Makefile.am

 1. Create src/Makefile.am ...
echo 'bin_PROGRAMS = '$projectname > src/Makefile.am
echo 'CXXFLAGS = -O2' >> src/Makefile.am
echo $projectname'_SOURCES = Main.cpp' >> src/Makefile.am
echo 'LDADD = ' >> src/Makefile.am
echo 'noinst_HEADERS = ' >> src/Makefile.am

 1. Run aclocal ...
aclocal

 1. Run autoconf...
autoconf

 1. Run automake ...
automake --add-missing

 1. Run configure...
./configure

 1. Compile program
make

 1. Execute program
src/$projectname

 1. Finished
echo "Project creation complete."


July 2004 SimonCapstick

LinuxHints/SimpleCPlusPlusProject (last edited 2006-03-22 19:02:41 by 212)