Refugee VS The American Dream

Mark Mahmood arrived in the United States in February 2010 as a refugee from Iraq after spending three years applying to a United Nations refugee program. He came armed with a few dollars in his…

Smartphone

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




The Differences Between Static and Dynamic Libraries in C

Libraries are an essential part of programming — without it, we would have to write similar functions repetitively. But there is a difference between static and dynamic libraries.

Static linking will copy all the codes used in the program from the library modules into the compiling program. The actual linking is performed by programs called linkers as the last step in compiling a program.

In dynamic linking, the name of shared libraries are placed into an executable file while the actual linking happens at run time.

One of the advantage of using a dynamic library involves memory space — the shared library has but one copy kept in memory, which reduces the size of executable programs.

It is not uncommon for external programs to be changed — if that is the case, then static library would require recompilation and re-linking in order for it to update in the current executable file. With shared libraries, this is unnecessary.

Static libraries also have its own advantages: for one, programs using static libraries are arguably faster than programs using dynamic libraries. In addition, static libraries are less likely to run into compatibility issues.

The above code will compile all .c files with the flag -fPIC (Position Independent Code), which means that the generated machine code will not be dependent on a specific address in order to work.

The object files can now be compiled into a dynamic library by taking all of the .o files and using the -shared flag to make it a shared object. The compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension.

We can use nm -D if we want to see the list of functions.

The above code compiles the shared library — ‘-L.’ indicates that the shared library is in the current directory, -lall to include all library files and to look for liball specifically.

With this above command, all the .c extension files in the current working directory will be converted to object files. Only once we have the respective object files, we use then use the below command to create the library.

The archiver, aka ar, creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual file.

The flag -c tells ar to create an archive. The flag -r inserts the member files and replaces older files where needed (in this particular case, the *.o files).

It may look as if we’re done, but wait! We still need to index our library with the command below.

The index is necessary for listing each symbol and defining a member of an archive that is a relocatable object file. This allows the library, which may have hundreds of of symbols, quickly find the respective reference symbol for your particular program.

Depending on the computer system, you may or may not need ranlib.

Once the command above is established, you can execute it by running ./main.

Add a comment

Related posts:

How to earn money online without investment in 2022

Everyone wants to make money online but no one knows the easy way to make money online. There are many other websites that do not give you a single rupee. But today I am going to tell you the ways in…

Did the Pandemic Create the School of Unlearning?

Unlearning is crucial and extremely possible. This pandemic has force many to unlearn overnight and those who succeed reach new heights, break new records

New Notch Builder and Playback licensing options

New licensing options for Notch Builder and Playback makes both products more accessible than ever before.