Click to See Complete Forum and Search --> : Perl code for compiling C/C++


Ben Briggs
02-24-2001, 02:59 PM
Here's a Perl script I wrote to make compiling one file programs easier.


#!/usr/bin/perl

my $language = $ARGV[0];
my $file = $ARGV[1];

if($language eq ("C" or "c"))
{
system("/usr/bin/gcc $file.c -o $file");
} else
{
system("/usr/bin/g++ $file.cpp -o $file");
}


Usage is:
./compile C hello
./compile C++ hello

The first example will compile "hello.c" into "hello". And the second, "hello.cpp" into "hello".

This could obviously be expanded to include error detections, but I don't really have the initiative to do so (feel free if you do though).

[ 24 February 2001: Message edited by: Ben Briggs ]

Sterling
02-25-2001, 12:40 PM
Wouldn't using a bash or csh shell script for this be better? I'm not saying that using Perl wasn't good - anything that helps you learn a language. But I think that a shell script might have been more efficient, ne?

Strike
02-25-2001, 02:09 PM
Actually, if you just do make foo and you have either foo.c or foo.cpp in the current directory, it will automatically do those for you.

Ben Briggs
02-25-2001, 02:48 PM
Sterling:

It probably could have been better written in bash or csh, but I don't feel like taking that step yet.

Strike:

Oh well... I was bored when I made it.