What exactly are bookstores?
JUAN CARLOS GIRALDO DUQUE

What exactly are bookstores?

In C, libraries (or libraries) are known as certain types of files that we can import or include in our program. These files contain the specifications of different functionalities already built and usable that we can add to our program, such as reading from the keyboard or showing something on the screen, among many others.

By being able to include these libraries with definitions of different functionalities, we can save a lot of things, imagine for example that every time we need to read from the keyboard, we must then create a function that does it (something really complex), since we can have the libraries in C ++, we will be able to make use of a great variety of functions that will make our lives easier and will increase the modularity of our codes.

In conclusion: Libraries are files (not always external) that allow us to carry out different tasks without having to worry about how they are done but simply understand how to use them. The C libraries allow us to make our programs more modular and reusable, also facilitating the creation of programs with quite complex functionalities in a few lines of code.

STATIC AND DYNAMIC LIBRARY DIFFERENCES AND HOW THEY WORK.

I don't know if in windows it is exactly the same as in linux, but the difference is the following.

A static library (.lib on windows, .a on linux) is loaded when the program is compiled. The necessary functions of that library are copied into your executable. If you take the executable from one computer to another, the program will work anyway, even if the library is not on the new computer, since the executable has its own copy. The problem is that the executable will be larger, since it carries a copy of the library functions.

A dynamic library (.dll on windows, .so on linux) is loaded at the time of program execution, as needed. The executable does NOT have a copy of the library's functions and it needs the library to function. If you take the executable to another computer, you must also take the library with you or make sure it is already there. The advantage is that the executable is usually smaller.

Usually the libraries that are used in many programs or that are in the operating system are usually dynamic, since it is easy for the computer to have them installed and it saves you from having several copies of them in several executables.

The libraries that a single program uses is better to make them static, unless they are many and very large and generate a gigantic, unwieldy executable. You do not care to distribute a small program with a library next to it than just the program, but a little bigger.

In order to obtain our own library, it would not be bad to know that it is a makefile.

HOW TO BUILD A MAKE FILE

Makefile what is it?

It is a tool or utility that helps us determine how our program will run.

How to compile a makefile

We will need files that will be our dependencies which will give us functionality with our file.

These dependencies are.

Files.o Files.h and Files.c

PRACTICAL EXAMPLE:

program: main.o output.o .calculator.o // name of our program and                          //  Dependencies to execute 
        
        gcc -o main.o output.o calculator.o // Command to execute        

These are the ones our program depends on.

main.o: main.c functions.h
      gcc -c main.c

output.o: output.c functions.c
      gcc -c output.c

calculator.o: calculator.c functions.c
        
      gcc -c calculator.c        

This is a special rule, it is a "phony" rule that is fictitious which helps us to eliminate any .o and the program.

clean:
        
      rm -f program *. o        

How to execute the compilation of that program?

to compile this in our terminal we will have to execute commands with the make statement.

EXAMPLES:

-> $ make program // with this command we execute our complete program.
        
-> $ make // compile our entire program again.        

HOW TO CREATE A STATIC LIBRARY

Creating a static library is very simple and consists, by and large, of the following two steps:

-> Compile the source code into object code.
-> Collect all the object code in a file that represents the Library.
        
-> Suppose we have a simple C program like the one presented below:        

## File name: holberton.h

#ifndef HOLBERTON_H

#define HOLBERTON_H


int \ \_putchar (char c);

int \ \_islower (int c);

int \ \_isalpha (int c);


#endif

        

## File name: main.c

#include "holberton.h"

int main (void)

{

int r;

    r = _isalpha ('H');

    _putchar (r + '0');

    r = _isalpha ('o');

    _putchar (r + '0');

    r = _isalpha (108);

    _putchar (r + '0');

    r = _isalpha (';');

    _putchar (r + '0');

    _putchar ('\ n');

    return (0);

        
}        

We will create a static library with the following two commands:

->$ gcc -static -c main.c -o main.c.o

->$ ar -rc liball.a \ \*. or        

note that we have named our Library "liball.a". This is no accident; The name must start with "lib" and end with ".a" so that it can be linked to gcc in a program.

Now that our Library is ready, we can redistribute it along with the header holberton.h so that other programmers can make use of it in their programs.

Notice how we make liberal use of data types and functions that we have defined in our Library. For this program to compile, we only need the header to be present; To link it, we must use the gcc flag "-l" (lowercase L, no quotes).

->$ gcc main.c -L. -the double L

->$ ./a.out
        
1110        

Given the "-lall" flag, GCC will start looking for a file named "libalgebra.a" (hence the importance of the name used for the Library). The "-L" flag tells GCC where this file is located.


To view or add a comment, sign in

More articles by Juan Duque

  • Recursion for newbies?

    I think this is one of the most complex issues to explain and understand, many times it is really difficult to make our…

  • static and dynamic libraries in linux

    When you are compiling your sources in Linux, one of the options is the type of link you want to make in your static or…

  • Compilation in C

    Here we will go into more detail about how the complete compilation process works, from the moment we write a file in…

Insights from the community

Others also viewed

Explore topics