In this post, we will see How to Create Header File in C / CPP | Independent compilation in C language.
Check following programs.
Here, we will write two programs addfun.c and subfun.c . addfun.c consists of function add() for addition of two numbers while subfun.c consists of sub() for subtraction.
We will create our own header file header.h . We will include header.h in another file sample.h which consists of main() function.
Check following programs.
Here, we will write two programs addfun.c and subfun.c . addfun.c consists of function add() for addition of two numbers while subfun.c consists of sub() for subtraction.
We will create our own header file header.h . We will include header.h in another file sample.h which consists of main() function.
Watch following video:
Check following program code.
Program (addfun.c)
Program (subfun.c)
Program (header.h)
Program (sample.c)
Output (How to compile and execute)
1. We should compile these files by using option -c. Reason for this is, addfun.c and subfun.c do not consist of main() function while sample.c does not consist of add() & sub().
2. We have to create single executable file a.out by gcc addfun.o subfun.o sample.o .
3. We have to include header.h using following syntax.
#include "header.h"
not like
#include<header.h>
Reason for this is, when we write #include<header.h>, it takes the path of C library where all standard header files exist. While our header file header.h exist in the folder where there are addfun.c, subfun.c, sample.c; we should include it like #include "header.h".
C, C++, Java follows independent compilation i.e. we can compile above modules/files in any order. It doesn't affect on final output.
Check following:
Output
While language Ada follows Separate compilation i.e. Compilation should be done in proper order. Files which define functions should be compiled first and then compile the file which call these functions.
Program (addfun.c)
int add(int a, int b) { int c; c=a+b; return c; } |
Program (subfun.c)
int sub(int a, int b) { int c; c=a-b; return c; } |
Program (header.h)
int add(int, int); int sub(int, int); |
Program (sample.c)
#include<stdio.h> #include "header.h" void main() { int x=3,y=5; int sum; int s; sum=add(x,y); s=sub(x,y); printf("Addition=%d\n",sum); printf("Subtraction=%d\n",s); } |
Output (How to compile and execute)
>>> gcc -c addfun.c >>> gcc -c subfun.c >>> gcc -c sample.c >>>gcc addfun.o subfun.o sample.o >>> ./a.out Addition=8 Subtraction=-2 |
Important Note:
1. We should compile these files by using option -c. Reason for this is, addfun.c and subfun.c do not consist of main() function while sample.c does not consist of add() & sub().
2. We have to create single executable file a.out by gcc addfun.o subfun.o sample.o .
3. We have to include header.h using following syntax.
#include "header.h"
not like
#include<header.h>
Reason for this is, when we write #include<header.h>, it takes the path of C library where all standard header files exist. While our header file header.h exist in the folder where there are addfun.c, subfun.c, sample.c; we should include it like #include "header.h".
Independent Compilation
C, C++, Java follows independent compilation i.e. we can compile above modules/files in any order. It doesn't affect on final output.
Check following:
Output
>>> gcc -c subfun.c >>> gcc -c sample.c >>> gcc -c addfun.c >>>gcc addfun.o subfun.o sample.o >>> ./a.out Addition=8 Subtraction=-2 |
While language Ada follows Separate compilation i.e. Compilation should be done in proper order. Files which define functions should be compiled first and then compile the file which call these functions.
No comments:
Post a Comment