JPHHLASH

how to run C/C++ programs in linux

Home
header files
Linux Commands
access windows partition from linux
startup
compiling from source code
how to run C/C++ programs in linux
some useful shell scripts
environment variables
fork and exec
Using apache
php in linux
read/write in linux

How to run C/C++ programs in linux

You probably have gcc installed, very simply:
$ gcc infile.c
will create an executable called a.out which you can run with:
$ ./a.out
"man gcc" for more details.

Download Gcc from here

Gcc download

If the package isnt pre-compiled then compile the packages by
$ ./configure

$ make

# make install

check any README and INSTALL files

To only compile a file
gcc -c filename.c
For C++
g++ -c filename.cpp

To debug a file
gcc -g filename.c

To add a #define macro without editing the source file
g++ -c -D HELL=6 filename.c
To link the file and create the executable fil(Unlike Windows environment,executable files here dont end with .exe .They dont have any extension
gcc -o filename filename.o
OR
g++ -o filename filename.o
here filenmae.o is the object file

If we want to compile and link multiple files, we need to use make
We need to create the Makefile file first in order to use make.The Makefile looks like this:-
hello:hello.o
gcc -o hello hello.o

hello.o: hello.c
gcc -c hello.c

clean :
rm -f *.o hello

There need to be a tab before the next line begins

The Makefile ,c/c++ file ,object files should be under the same directory
Now give the command
$make
gcc -c hello.c
gcc -o hello hello.o

The Commands are executed as Makefile has specified.
Now to clean up the redundant files ,use the command
$make clean
rm -f *.o hello

To add flags without editing the Makefile
$make CFLAGS=-02 (02 optimises the code so it runs as quickly as possible)
OR
$make CFLAGS=-g
gcc -c -g hello.c
gcc -o -g hello hello.o

To execute the executable file ,use the commands
$./filename

Feedback, query, ideas? Email crenshaw_jo@yahoo.com.