C/C++ programming with linux

All the linux distros come with default programming tools (eg- gcc compiler, make utility, gdb debugger etc.). It makes it fun n powerful to program in linux with C/C++ by using the tools provided.
Note : Some linux distros don’t come with g++ compiler which is used for compiling C++ codes. eg- Ubuntu. We can install it by the command
sudo apt-get install g++

Let’s discuss gcc, make, n gdb with an example.
Create a directory for your code and move to that directory.

mkdir first
cd first

now we will create a main.c file in this directory. You can use any of your favorite text editor to create main.c file which cotains the text

#include <stdio.h>
#include "reciprocal.hpp"

int main(int argc, char **argv){
	int i;

	i = atoi(argv[1]);
	printf("The reciprocal of %d is %g\n", i, reciprocal(i));
	return 0;
}

it uses a header file reciprocal.hpp. Let’s create reciprocal.hpp file

#ifdef __cplusplus
extern "C"{
#endif

extern double reciprocal(int i);

#ifdef __cplusplus
}
#endif

now create a c++ file to define the function reciprocal(). We name that file reciprocal.cpp

#include <cassert>
#include "reciprocal.hpp"

double reciprocal(int i){
	assert(i!=0);
	return 1.0/i;
}

That’s all for creating a simple function to test the programming in linux. Here we have three files- main.c reciprocal.hpp & reciprocal.cpp

Let’s compile reciprocal.cpp with g++ compiler

g++ -c reciprocal.cpp

this will generate a compiled reciprocal.o file in our directory.

Now compile main.c with gcc compiler

gcc -c main.c

this will generate a compiled main.o file

Now we need to link both the object files, we that by

g++ -o reciprocal main.o reciprocal.o

this will generate an executable file reciprocal in the current directory. That’s it.

We can run the executable file by just typing

./reciprocal

or we can use gdb debugger to run it as

gdb reciprocal

which will give us the output

GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08

Copyright (C) 2011 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later 

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-linux-gnu".

For bug reporting instructions, please see:

...

Reading symbols from /home/nandal/LinuxC/first/reciprocal...done.

(gdb) 

Now we can simply enter the run command

(gdb)run 4
this will give the output
Starting program: /home/nandal/LinuxC/first/reciprocal 4

The reciprocal of 4 is 0.25

[Inferior 1 (process 9318) exited normally]

(gdb) 

We can use help command to view various commands available for gdb. We can set break points for debugging the code. Use man gdb to view the details about the gdb.

In the above code if we make a change in any source file, we need to compile the specific source file and all the other source file which are dependent on the that source file. If we make changes to reciprocal.cpp then we also need to recompile main.c with reciprocal.cpp because that’s dependent on reciprocal.cpp. For that we need to be aware of all the dependency among the source files. It’s fine for few files, but when we are dealing with hundreds of them, it becomes very tedious and confusing. There comes the role of make utility.
Make
We can use make utility to chek all these dependency and help in creating the changes to the source files. Now we will create a Makefile for our program which contains the dependency tree of the source files. It’s very simple. Create a Makefile with your favourite text editor with the contents:

reciprocal: main.o reciprocal.o
	g++ $(CFLAGS) -o reciprocal main.o reciprocal.o

main.o: main.c reciprocal.hpp
	gcc $(CFLAGS) -c main.c

reciprocal.o: reciprocal.cpp reciprocal.hpp
	g++ $(CFLAGS) -c reciprocal.cpp

clean:
	rm -f *.o reciprocal

and save it. The above file states that reciprocal is dependent on main.o and reciprocal.o therefore if any changes are made to either of these will file will results in changes to reciprocal file.
In the same way main.o is dependent on main.c and reciprocal.hpp and reciprocal.o is dependent on reciprocal.cpp and reciprocal.hpp.
Here we have defined clean which can be used to make a clean build bye the command

make clean

It will remove all the object files and the reciprocal file from the current directory.
That’s all. Now if you make changes to any of the source files in this project, you won’t need to compile them separately and keeping track of the dependencies. All care would be taken by the make utility.
After making changes, simply use the make command in the terminal and the make will use the Makefile info for cheking the dependencies and will compile and link the program accordingly.

make

if you made no changes to any source file of reciprocal program, and then use the make command, the make utility will indentify it and won’t compile any of the file.

make
make: `reciprocal' is up to date.

Use man or info to view more detail about gcc, gdb, and make in your linux terminal window. Have fun.
Share it with your twitter acount:

One thought on “C/C++ programming with linux

  1. Pingback: Derrick

Leave a Reply