Working with Multiple Source Files

Requirements:
1. Linux Distribution (obviously)
2. A text editor :
Vim, Emacs, gedit, kate, atom
3. A Compiler :
gcc or clang
4. A Shell:
bash, xterm, sh,

My setup for this tutorial:
1. Debian GNU/Linux 9.4
2. gedit (Text editor)
3. gcc version 6.3.0-18+deb9u1 (Debian’s Release version for Debian 9.1)
4. bash

Hello all, I am sorry for the lack of posts, i have been a little busy, i will be picking back up with it soon, I should have one a week out for a couple months. In this one we will go over working with multiple source files

Programs don’t normally run from one source file. They are split up for
modularity. This allows for easier recompilation, as the other source file will
be the only thing that needs to be recompiled. Today , we will create a new
project folder and setup a new project to work on. We will call it multisource:

  clim@clim:~/Desktop$ mkdir multisource
  clim@clim:~/Desktop$ cd multisource
  clim@clim:~/Desktop/multisourceC$ gedit func.h func.c main.c

Of course you can always use a different editor, but for the tutorial, I will
use gedit. We will take the swap function we used in the Variables, Pointers, and
functions tutorial and implement it here. First go to the func.h file. Header
files are used to declare functions to be used in other files. Simply write this:

#ifndef FUNC_H_
#define FUNC_H_

  void swap(int * x, int * y);

#endif //Func.h

Remember how I said you can either create the function before the main function in main.c , or you can declare it before the main function and implement it at the bottom? Here I am declaring a function to be used. It is good programming practiceto use header guards to keep things from being placed in the code twice. This is done by using the preprocessor macros i describe in the first 2 lines. Ifndef stands for If Not Defined. We learned what define does, so the logic is :
If FUNC_H_ is not defined, define FUNC_H_ and then paste the rest of the block
in the source file, until the endif. Save that file go into your func.c and write:

#include "func.h"

  void swap(int * x , int * y){
    int temp = *x;
    *x= *y;
    *y = temp;
  }

This is a simple file, you only need to include the header file which contains
the functions you are implementing. And finally the main.c:

#include 
#include "func.h"


int main(int argc, char * argv[]){
    int foo = 55;
    int bar = 77;

    printf("Printing Variables:\nFoo: %d\nBar: %d\n",foo, bar);
    printf("Swapping Variables");
    swap(&foo,&bar);
    printf("Printing Variables:\nFoo: %d\nBar: %d\n",foo, bar);

    return 0;

}

Now we go back to our shell and type :

  clim@clim:~/Desktop/multisource$ gcc func.c main.c -o test1
  clim@clim:~/Desktop/multisource$ ./test1

GCC gives me the ability to input multiple files, compile, and link
them all at once. At this time i also want to go over this process from gcc.
When you input the files s such, use -o to create the object file, it also links
everything. However the compilation process and the linking process could be done
seperate, lets see how that works:

  clim@clim:~/Desktop/multisource$ gcc -c func.c
  clim@clim:~/Desktop/multisource$ gcc -c main.c
  clim@clim:~/Desktop/multisource$ gcc -o test1 func.o main.o

The first two steps are compiling func.c into func.o, and main.c into main.o,
after the third step is to link using the -o compiler flag. We will go over the
compiler flags in another tutorial. This can also get a little overwhelming when
creating projects with several sources, so people usually put these all into a
build system such as make, cmake, qmake, or scons. We will go over some of those
as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s