Sunday 23 August 2009

Tip of the Day: Run a C Program Directly

For this short tip you'll need tcc, the Tiny C Compiler, which comes with most distributions out there, including Debian and Ubuntu. tcc is a small ANSI C compiler which offers the ability to run the program after compiling it, unlike GCC, which (as far as I know) doesn't offer this option. To install tcc in Debian and Ubuntu:

In Debian, as root:

apt-get install tcc

In Ubuntu, use:

sudo apt-get install tcc

Now let's test this. First, create your C source file, e.g.:

#!/usr/bin/tcc -run
#include <stdio.h>

int main ()
{
printf ("Hello, world!\n");
return 0;
}

Now, save this file as example.c or some other name and make it executable:

chmod 755 example.c # or chmod +x example.c

And the next step is just to run it!

./example.c

tcc will compile the source and run it automatically.

No comments: