Dieta de 17 dias funciona mesmo?

Na busca por uma vida mais saudável procuramos dietas, métodos e até mesmo remédios para emagrecimento e, nos deparamos com milhares deles. Ocorre que, muitos não proporcionam o que realmente…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Dynamic Libraries in C

Why use libraries?

When you do a big project that have many common functions that you need to reuse to others project, manage the each function individually are very confused an take disorder but the libraries are here to help us.

How works?

Basically when you need to reuse multiple functions in you program the compiler can create specific files (.a) to link in compile time, remember that a C program is a conglomerated of functions that works together, beginning by a main function commonly called the entry point, generally the functions are in the header file and you generate the library and link to the program to use them.

How to create the dynamic libraries?

First to all exist basically to types of libraries, static or dynamic if you want to know more about the static libraries look my other article about that, this post is focused in dynamic libraries.

First of all take ready the functions

First you need a functions to create the dynamic library, in this example the prototypes of the functions are in the headers file and the program that need to use the dynamic functions are called program.c, the functions to create the dynamic library is test.c

Compile the functions

Use the flag -fpic of gcc in this way

Create the shared library

Use the flag -shared to create it.

the shared library created

A recommendation about this step is move the shared library in a folder because the idea of this is can be used by others programs (/opt/lib folder for example).

Now what?

With the last steps you create the dynamic library but you need to tell to the gcc where find the library, remember that in each computer the location of the libraries can change.

Be careful in this step, the lib prefix in the name of the shared functions it’s mandatory if you don’t putted the -ltest.c will show you an error, in a litle words (libtest corresponds to -ltest), the -L flag indicate the folder in where can find the program the dynamic library.

Now export to take enable to the program and others

Now try to execute your program and you will see how works correctly.

What are the differences with the static libraries

Basically the main difference its the possibility of share the dynamic with others programs.

Advantages of use dynamic libraries

Drawbacks of use dynamic libraries

About the last advantages and drawbacks in static look the article about static libraries.

Add a comment

Related posts:

6 Word Stories

I recently started writing 6 word stories. This is a fun thing to try. And anyone can do it as writing six words doesn’t take that much time. Although it can be difficult to say what you want to say…

Little Pea

I remember like it was yesterday. I stand barefoot in the grass, picking the browned pigeon pea pods off the vine. My grandmother stands next to me, holding battered tin roasting pan catching the…

How to Add Password Reset to Your Node.js Authentication API using SendGrid

How to Add Password Reset to Your Node.js Authentication API using SendGrid.