Difference between revisions of "Linux Libraries"

From Teknologisk videncenter
Jump to: navigation, search
m (Creating libfilearchive.a)
m (Sourcefile containing main)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
Linux Libraries comes in two forms: Static and shared.
+
Linux Libraries comes in two forms: Static and shared. Static libraries are linked at compile time while shared libraries are linked at runtime. The following three sourcefiles ''file1.c'', ''file2.c'' and ''file3.c'' will be used to build a static and a shared library.
=Static Libraries=
+
=Source files=
Static libraries link needed code from the object (.o) files or an archive of object files into the binary file.
+
The following four sourcefiles are used:
==Static example==
+
*file1.c contains a const array of '''one''' megabyte
Pack three files ''file1.c'', ''file2.c'' and ''file3.c'' to ''libfilearchive.a''
+
*file2.c is quite small
*file1.c is quite small
 
*file2.c contains a const array of '''one''' megabyte
 
 
*file3.c contains a const array of '''two''' megabyte
 
*file3.c contains a const array of '''two''' megabyte
 +
*hi.c contains main and uses functions from ''file1.c'' and ''file2.c'' but not ''file3.c''
  
===File1.c===
+
==File1.c==
 
<source lang=c>
 
<source lang=c>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
Line 22: Line 21:
 
}
 
}
 
</source>
 
</source>
===File2.c===
+
==File2.c==
 
<source lang=c>
 
<source lang=c>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
Line 31: Line 30:
 
}
 
}
 
</source>
 
</source>
===File3.c===
+
==File3.c==
 
<source lang=c>
 
<source lang=c>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
 
//Author: Henrik Thomsen <heth@mercantec.dk>
Line 45: Line 44:
 
}
 
}
 
</source>
 
</source>
===Creating libfilearchive.a===
+
==Sourcefile containing main==
 +
This sourcefile only use functions from ''file1.c'' and ''file2.c'' - note that ''file3.c'' is not used.
 +
<source lang=c>
 +
//Author: Henrik Thomsen <heth@mercantec.dk>
 +
//Name: hi.c
 +
#include <stdio.h>
 +
 
 +
extern void p1( void );
 +
extern void p2( void );
 +
extern void p3( void );
 +
 
 +
int main( void ) {
 +
        printf("Starting main\n=============\n\n");
 +
        p1();
 +
        p2();
 +
        return(0);
 +
}
 +
</source>
 +
 
 +
=Static Libraries=
 +
Static libraries link needed code from the object (.o) files or an archive of object files into the binary file at compile time. See example below.
 +
==Creating libfilearchive.a==
 
Notice the size of the object files from and the size of the ''.a'' archivefile. It contains all three ''.o'' files.
 
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>
Line 53: Line 73:
 
[heth@localhost ]$ ar rc libfilearchive.a file1.o file2.o file3.o
 
[heth@localhost ]$ ar rc libfilearchive.a file1.o file2.o file3.o
 
[heth@localhost ]$ ls -l
 
[heth@localhost ]$ ls -l
-rw-rw-r--. 1 heth heth         334 Dec 23 10:32 file1.c
+
-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 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     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         345 Dec 23 10:39 file3.c
+
-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 2098824 Dec 23 10:40 file3.o
 
-rw-rw-r--. 1 heth heth 3150842 Dec 23 10:40 libfilearchive.a
 
-rw-rw-r--. 1 heth heth 3150842 Dec 23 10:40 libfilearchive.a
 +
</source>
 +
==Using the archive==
 +
 +
 +
 +
====Compiling and linking====
 +
When using the archive link it in - and in this case add current directory to the linkers searchpath ''-L."
 +
<source lang=bash>
 +
[heth@localhost ]$ gcc hi.c -lfilearchive -L. -o hi
 +
[heth@localhost ]$ ./hi
 +
Starting main
 +
=============
 +
 +
I am file1
 +
txt is 1048576 bytes
 +
I am file2
 +
</source>
 +
'''Note:''' The size of the binary file ''hi'' proves that ''file3'' is not linked in.
 +
<source lang=bash>
 +
[heth@localhost libleg]$ ls -l
 +
total 15432
 +
-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
 +
-rwxrwxr-x. 1 heth heth 1067104 Dec 23 11:25 hi
 +
-rw-rw-r--. 1 heth heth    219 Dec 23 11:24 hi.c
 
</source>
 
</source>
  
 +
=Shared example=
 +
Shared libraries are linked at compile time - meaning that all binaries that use a shared library can be updated by updating the shared library only.
 +
See example below.
 +
==Creating libfilearchive.a==
 +
Notice the size of the object files from and the size of the ''.so'' shared library file. It contains all three ''.o'' files.
 +
<source lang=bash>
 +
[heth@localhost ]$ gcc -fPIC -c file1.c
 +
[heth@localhost ]$ gcc -fPIC -c file2.c
 +
[heth@localhost ]$ gcc -fPIC -c file3.c
 +
[heth@localhost ]$ gcc -shared -o libshared.so file1.o file2.o file3.o
 +
[heth@localhost ]$ ls -l
 +
-rw-rw-r--. 1 heth heth    334 Dec 23 10:32 file1.c
 +
-rw-rw-r--. 1 heth heth 1050288 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    1536 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 2098864 Dec 23 10:40 file3.o
 +
-rw-rw-r--. 1 heth heth 3162256 Dec 23 13:17 libshared.so
 +
</source>
 +
==Using the archive==
 +
Shared library files are normally kept in a ''/usr/lib'' subdirectory and called ''.so'' (Shared Obkect). Actual path can be seen in ''/etc/ld.so.conf''. In our example the the we add the path to our ''.so'' file in the working directory.
 +
 +
 +
===Compiling and linking===
 +
<source lang=bash>
 +
[heth@localhost libleg]$ gcc hi.c -lshared -L. -o hi
 +
[heth@localhost libleg]$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./hi
 +
Starting main
 +
=============
  
 +
I am file1
 +
txt is 1048576 bytes
 +
I am file2
 +
[heth@localhost libleg]$ ls -l
 +
total 18524
 +
-rw-rw-r--. 1 heth heth    334 Dec 23 10:32 file1.c
 +
-rw-rw-r--. 1 heth heth 1050288 Dec 23 11:42 file1.o
 +
-rw-rw-r--. 1 heth heth    125 Dec 23 10:26 file2.c
 +
-rw-rw-r--. 1 heth heth    1536 Dec 23 11:42 file2.o
 +
-rw-rw-r--. 1 heth heth    345 Dec 23 10:39 file3.c
 +
-rw-rw-r--. 1 heth heth 2098864 Dec 23 11:42 file3.o
 +
-rwxrwxr-x. 1 heth heth  18400 Dec 23 13:24 hi
 +
-rw-rw-r--. 1 heth heth    219 Dec 23 11:24 hi.c
 +
-rwxrwxr-x. 1 heth heth 3162256 Dec 23 13:17 libshared.so
 
</source>
 
</source>
[[Category:Linux]]
+
[[Category:Linux]][[Category:Linux Programming]][[Category:C]]

Latest revision as of 06:51, 25 October 2023

Linux Libraries comes in two forms: Static and shared. Static libraries are linked at compile time while shared libraries are linked at runtime. The following three sourcefiles file1.c, file2.c and file3.c will be used to build a static and a shared library.

Source files

The following four sourcefiles are used:

  • file1.c contains a const array of one megabyte
  • file2.c is quite small
  • file3.c contains a const array of two megabyte
  • hi.c contains main and uses functions from file1.c and file2.c but not file3.c

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) );
}

Sourcefile containing main

This sourcefile only use functions from file1.c and file2.c - note that file3.c is not used.

//Author: Henrik Thomsen <heth@mercantec.dk>
//Name: hi.c
#include <stdio.h>

extern void p1( void );
extern void p2( void );
extern void p3( void );

int main( void ) {
        printf("Starting main\n=============\n\n");
        p1();
        p2();
        return(0);
}

Static Libraries

Static libraries link needed code from the object (.o) files or an archive of object files into the binary file at compile time. See example below.

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 ]$ gcc -c file3.c
[heth@localhost ]$ ar rc libfilearchive.a file1.o file2.o file3.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

Using the archive

Compiling and linking

When using the archive link it in - and in this case add current directory to the linkers searchpath -L."

[heth@localhost ]$ gcc hi.c -lfilearchive -L. -o hi
[heth@localhost ]$ ./hi
Starting main
=============

I am file1
txt is 1048576 bytes
I am file2

Note: The size of the binary file hi proves that file3 is not linked in.

[heth@localhost libleg]$ ls -l
total 15432
-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
-rwxrwxr-x. 1 heth heth 1067104 Dec 23 11:25 hi
-rw-rw-r--. 1 heth heth     219 Dec 23 11:24 hi.c

Shared example

Shared libraries are linked at compile time - meaning that all binaries that use a shared library can be updated by updating the shared library only. See example below.

Creating libfilearchive.a

Notice the size of the object files from and the size of the .so shared library file. It contains all three .o files.

[heth@localhost ]$ gcc -fPIC -c file1.c
[heth@localhost ]$ gcc -fPIC -c file2.c
[heth@localhost ]$ gcc -fPIC -c file3.c
[heth@localhost ]$ gcc -shared -o libshared.so file1.o file2.o file3.o 
[heth@localhost ]$ ls -l
-rw-rw-r--. 1 heth heth     334 Dec 23 10:32 file1.c
-rw-rw-r--. 1 heth heth 1050288 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    1536 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 2098864 Dec 23 10:40 file3.o
-rw-rw-r--. 1 heth heth 3162256 Dec 23 13:17 libshared.so

Using the archive

Shared library files are normally kept in a /usr/lib subdirectory and called .so (Shared Obkect). Actual path can be seen in /etc/ld.so.conf. In our example the the we add the path to our .so file in the working directory.


Compiling and linking

[heth@localhost libleg]$ gcc hi.c -lshared -L. -o hi
[heth@localhost libleg]$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./hi
Starting main
=============

I am file1
txt is 1048576 bytes
I am file2
[heth@localhost libleg]$ ls -l
total 18524
-rw-rw-r--. 1 heth heth     334 Dec 23 10:32 file1.c
-rw-rw-r--. 1 heth heth 1050288 Dec 23 11:42 file1.o
-rw-rw-r--. 1 heth heth     125 Dec 23 10:26 file2.c
-rw-rw-r--. 1 heth heth    1536 Dec 23 11:42 file2.o
-rw-rw-r--. 1 heth heth     345 Dec 23 10:39 file3.c
-rw-rw-r--. 1 heth heth 2098864 Dec 23 11:42 file3.o
-rwxrwxr-x. 1 heth heth   18400 Dec 23 13:24 hi
-rw-rw-r--. 1 heth heth     219 Dec 23 11:24 hi.c
-rwxrwxr-x. 1 heth heth 3162256 Dec 23 13:17 libshared.so