Saturday, September 5, 2009

Using several code files

Sometimes it is very handy to use several code files for a particular program. For example, if the program is very big and difficult to handle then its necessary to split the whole program into several files and combine together to make the program work properly. Sometimes function can be moved into a separate file. Let's take an example on how to move the functions into a separate file from the main function.

In the file plus.hh the function is declared:

#ifndef PLUS_HH
#define PLUS_HH

int plus( int a, int b );

#endif // PLUS_HH


In the file plus.cc the function is defined:

int plus( int a, int b )
{
return a + b;
}


In the file main.cc the main function is defined:

#include <iostream>
#include <cstdlib>
#include "plus.hh"

int main( void )
{
cout << "3+4 = " << plus( 3, 4 ) << endl;
return EXIT_SUCCESS;
}


The files work together in that sense that the .hh files (header files) have the declarations of those functions the .cc files (source files) define. The actual code, e.g. the function bodies, is in the .cc files. The .hh files must be added to the program with the #include directive if the functions need to be used in that file.

The command <file> is used when the included file is in some of the systems default directories. The compiler automatically also links these files. "file.hh" means that the header file is in the same directory as the source file. The function definitions need to be included also into the compilation stage to get an executable program. This is done by linking the files together. The compiler also links:

> g++ o plus plus.cc main.cc
>./plus
3+4 = 7


If the different stages of compiling and linking ned to be separated, the source files (.cc) can first be compiled into object files (.o) which then are linked into an executable program. For example:

>g++ c plus.cc
>g++ c main.cc
>g++ o plus plus.o main.o
>./plus
3+4 = 7


It makes sense to separate the compiling and linking stages when compiling large programs. It is easy to locate which stage the errors are coming from.

Some important matters:
  • Always include only header files
  • Write the actual code into the source files
  • Since include adds the file as a part of the compiled file, it is possible that some file is included more than once. Overlapping definitions can prevent compilation
  • The directives #ifndef, #define and #endif tell the preprocessor to add the file only once into the compiled file no matter how many times it has been included
  • The readability of a small program can be increased by moving the main to the beginning of the file and adding the definitions of functions after that. The functions must be declared before the main


Reference: fredosaurus, gamedev, learncpp

Previous post

Next post


3 comments:

কিংকর্তব্যবিমূঢ় said...

এইটা খুবই কাজের একটা পোস্ট ... বেশ ঘাঁটাঘাটি করে একসময় এই জিনিস শেখা লাগছিল, এরকম গোছানো একটা ওয়াকথ্রু পাইলে সহজ হইতো :D

কিংকর্তব্যবিমূঢ় said...

আগের কমেন্টে নাম সই করতে ভুলে গেসি :D

- ফাহিম

eamon said...

নাম সই না করলেও বিখ্যাত কিংকর্তব্ববিমুরকে আমরা সবাই চিনি :)

Post a Comment