Difference between revisions of "Linux Libraries"

From Teknologisk videncenter
Jump to: navigation, search
m (Creating libfilearchive.a)
m (Static example)
Line 3: Line 3:
 
Static libraries link needed code from the object (.o) files or an archive of object files into the binary file.
 
Static libraries link needed code from the object (.o) files or an archive of object files into the binary file.
 
==Static example==
 
==Static example==
Pack two files '''file1.c''' and '''file2.c''' to '''libfilearchive.a'''
+
Pack three files '''file1.c''', '''file2.c''' and '''file3.c''' to '''libfilearchive.a'''
 
===File1.c===
 
===File1.c===
 
<source lang=c>
 
<source lang=c>
Line 25: Line 25:
 
void p2( void ) {
 
void p2( void ) {
 
         printf("I am file2\n");
 
         printf("I am file2\n");
 +
}
 +
</source>
 +
===File3.c===
 +
<source lang=c>
 +
//Author: Henrik Thomsen <heth@mercantec.dk>
 +
//Name: file3.c
 +
#include <stdio.h>
 +
#define MB 1024*1024
 +
void p3( void ) {
 +
        // Initialize 2MB array first and last member
 +
        // so we are sure the array is not truncated.
 +
        const char txt[MB*2] = {[0]=0x0ff, [sizeof(txt)-1]=0xff };
 +
        printf("I am file3\n");
 +
        printf("txt is %d bytes\n", sizeof(txt) );
 
}
 
}
 
</source>
 
</source>
 
===Creating libfilearchive.a===
 
===Creating libfilearchive.a===
Notice the size of the object file from '''file1.c''' and the '''.a''' archivefile. It contains the 1MB const array.
+
Notice the size of the object files from and the size of the '''.a''' archivefile. It contains all three '''.o''' files.
 
<source lang=bash>
 
<source lang=bash>
 
[heth@localhost ]$ gcc -c file1.c
 
[heth@localhost ]$ gcc -c file1.c
Line 38: Line 52:
 
-rw-rw-r--. 1 heth heth        125 Dec 23 10:26 file2.c
 
-rw-rw-r--. 1 heth heth        125 Dec 23 10:26 file2.c
 
-rw-rw-r--. 1 heth heth      1496 Dec 23 10:31 file2.o
 
-rw-rw-r--. 1 heth heth      1496 Dec 23 10:31 file2.o
-rw-rw-r--. 1 heth heth 1051950 Dec 23 10:32 libfilearchive.a
+
-rw-rw-r--. 1 heth heth         345 Dec 23 10:39 file3.c
 +
-rw-rw-r--. 1 heth heth 2098824 Dec 23 10:40 file3.o
 +
-rw-rw-r--. 1 heth heth 3150842 Dec 23 10:40 libfilearchive.a
 
</source>
 
</source>
  

Revision as of 10:42, 23 December 2018

Linux Libraries comes in two forms: Static and shared.

Static Libraries

Static libraries link needed code from the object (.o) files or an archive of object files into the binary file.

Static example

Pack three files file1.c, file2.c and file3.c to libfilearchive.a

File1.c

//Author: Henrik Thomsen <heth@mercantec.dk>
//Name: file1.c
#include <stdio.h>
#define MB 1024*1024
void p1( void ) {
        // Initialize 1MB array first and last member
        // so we are sure the array is not truncated.
        const char txt[MB];// = {[0]=0x0ff, [MB-1]=0xff };
        printf("I am file1\n");
        printf("txt is %d bytes\n", sizeof(txt) );
}

File2.c

//Author: Henrik Thomsen <heth@mercantec.dk>
//Name: file2.c
#include <stdio.h>
void p2( void ) {
        printf("I am file2\n");
}

File3.c

//Author: Henrik Thomsen <heth@mercantec.dk>
//Name: file3.c
#include <stdio.h>
#define MB 1024*1024
void p3( void ) {
        // Initialize 2MB array first and last member
        // so we are sure the array is not truncated.
        const char txt[MB*2] = {[0]=0x0ff, [sizeof(txt)-1]=0xff };
        printf("I am file3\n");
        printf("txt is %d bytes\n", sizeof(txt) );
}

Creating libfilearchive.a

Notice the size of the object files from and the size of the .a archivefile. It contains all three .o files.

[heth@localhost ]$ gcc -c file1.c
[heth@localhost ]$ gcc -c file2.c
[heth@localhost ]$ ar rc libfilearchive.a file1.o file2.o
[heth@localhost ]$ ls -l
-rw-rw-r--. 1 heth heth         334 Dec 23 10:32 file1.c
-rw-rw-r--. 1 heth heth 1050248 Dec 23 10:32 file1.o
-rw-rw-r--. 1 heth heth         125 Dec 23 10:26 file2.c
-rw-rw-r--. 1 heth heth       1496 Dec 23 10:31 file2.o
-rw-rw-r--. 1 heth heth         345 Dec 23 10:39 file3.c
-rw-rw-r--. 1 heth heth 2098824 Dec 23 10:40 file3.o
-rw-rw-r--. 1 heth heth 3150842 Dec 23 10:40 libfilearchive.a


</source>