Build your own C library
Tags: cpp, visualcode, beginners, programming
Published: 2021-10-27 14:22:31

## *Hi everyone, this is my first article.* 
If i make any grammar mistakes, let me know.
I am not a native english speaker

Today I want to talk to you about **"personal C library"**.
Often functions written by us are very useful and will likely need to be reused in new programs.
You can then group these functions into a useful **library** to facilitate their reuse.

First let's start with a simple program that we all know. **HELLO WORLD!**

```javascript
#include <stdio.h>

void main() /*standard library */
{
    printf("HELLO WORLD!"); /*output displayed*/
    getchar(); /*Press any key to continue.*/
}
```

I used `Visual Studio Code` as a text editor, but you can use whatever you want.

So far everything ok. 
But if I wanted to put my function in a personal library to be able to reuse it, how do I do it?

Each library consists of two parts: a header file and the file that contains the code.
The header file, which is denoted with a suffix **.h** 
The header file contains constants and types, along with prototypes for the functions available in the library.

### Header file

First let's write our header file and save it in a file called `mylib.h`

```javascript
/* mylib.h */
extern void hello();
```
This line of code is a function prototype.
_The word **extern** in **C**_ represents functions that will be linked at a later time

Now let's write our code in a file called `mylib.c`

```javascript
/* mylib.c */
#include <stdio.h>
#include "mylib.h"

void hello()
{
    printf("HELLO WORLD! with my personal library ");
}
```
Note that the file includes its own header file (`mylib.h`) and `this is enclosed in double quotes` instead of the major and minor symbols. 
The latter being used only for system libraries.

###It's time to create the main program!
Then write the following program into a file and call it `main.c`

```javascript
/* main.c */
#include <stdio.h>
#include "mylib.h"

void main()
{
    hello();
    getchar();

    printf("HELLO PROGRAMMER ! this greeting does not use your personal library ");
    getchar();
}

```
This code includes the utility (`mylib.h`) library we created.


## FILL IN AND RUN THE CODE WITH A LIBRARY

If you have an **IDE** to program in **C**, you don't need to know how to compile, it will do it for you.
####So, run the program and watch the magic.

Otherwise if you want to create an executable, for example if you use **LINUX** and you have a **gcc compiler** installed, here are the steps you need to do.

Open the terminal in the folder containing the three files:

* `mylib.h`
* `mylib.c`
* `main.c`

and type: 

`gcc -c -g mylib.c`
`gcc -c -g main.c`

This line creates a file called `mylib.o` and `main.o` which contains the machine code for the main program.
The **-c** option causes the compiler to produce an object file for the library

#####Now we need to link the two object files by typing the following command.

`gcc -o myprogramname main.o mylib.o`

This command links main.o with mylib.o to form an executable called `myprogramname`. To run the program, type `myprogramname` on the command line.

Or if you also use `Visual Studio Code`, like me, an `.exe` will be created in the folder have you used.

##### I hope I was clear and that you enjoyed this article ! 

#### Now its time for you to create your own libraries.