- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Like Windows, Linux also provides modules for creating static library.

1. What is static library?
[A] It's a library that contains a list of API for a certain work, or to do something specifically that is pre-compiled and you don't have to recompile it again. If you know more about Linux, then you may know that a static library simply is just a compilation or a package of object code.
[*] Static library under Linux has extension ".a", while it is ".lib" in Windows.

2. What is the purpose of static library?
[A] As far as I know, firstly, since it's a library, it certainly gives APIs for convenient programming. It's already precompiled, so it costs less time to compile and load APIs than other types of libraries. Additionally, in case the author of the library wants to hide the actual source code.

3. When should I use static library?
[A] Well, you can use it any time you want. However, nowadays, Linux-ers don't prefer static library due to the faster computers and compilers.

Let's create our first Linux static library. Certainly, the programming language in use is C, or you can use C++.

I - Creating the library
1. Prepare:
[+] You need to create 2 files: one is the header, one is the source.
[+] Naming: all Linux libraries start with prefix "lib"

--[libhello.h]--


/*
* @author [JaPh]
* @date 14 August, 2009
* @file libhello.h
* @license freeware
*/

void hello( );

--[libhello.c]--

/*
* @author [JaPh]
* @date 14 August, 2009
* @file libhello.c
* @license freeware
*/

#include <stdio.h>

void hello( ) {
printf("[JaPh]Tutorial: Creating Linux Static Library\n"
"Message: Hello! This is a static library.\n");
}

2 - Compile the object code

[japh@localhost C]$ gcc -c libhello.c

3 - Create the library

[japh@localhost C]$ ar -cvsq libhello.a libhello.o
a - libhello.o

Finally, the file libhello.a created. This is our target static library

II - Use the library
The next thing we need to do is to test whether our library works or not.
Let's write a simple program to call the function hello() inside the library libhello.a

--[hello_main.c]--

/*
* @author [JaPh]
* @date 14 August, 2009
* @file hello_main.c
* @license freeware
*/

#include "libhello.h"

int main( ) {
hello( );
return 0;
}

Then compile:

[japh@localhost C]$ gcc -o hello hello_main.c -L/path/to/library -lhello

You need to specify the location where you put the file libhello.a
Since it's a standard under Linux, you don't have to specify -libhello, use -lhello instead.
Finally, run our program

[japh@localhost C]$ ./hello
[JaPh]Tutorial: Creating Linux Static Library
Message: Hello! This is a static library.

There you have your first Linux static library. Pretty simple and very handy
If you don't want to type again and again for those steps, I've written this tiny script for auto-work everything. You just need to put all 3 files above into the same directory, then run the script below. You can change things in the script for personally wants.

#!/bin/sh
echo "[1] Compiling Object Code"
gcc -Wall -c libhello.c

echo "[2] Creating library"
ar -cvsq libhello.a libhello.o

echo "[3] Compiling program"
gcc -o hello hello_main.c -L. -lhello

echo "[4] Running program"
./hello


Well, that's all I have for you in this topic.

Have fun!@

0 comments

Post a Comment