1. Create a header file includes the function description, function declaration. for example:
patrick@debian:~/test/libtest$ vi test_lib.h
|
In the test_lib.h, the following information may be added.
#ifndef __TEST_LIB_H__ #define __TEST_LIB_H__
/* print the "hello world" information */ void print_hello( void );
#endif
|
2. Create a implementation file includes the function description and function implementation. for example:
patrick@debian:~/test/libtest$ vi test_lib.c
|
In the file, added the following codes.
#include <stdio.h>
#include "test_lib.h"
void print_hello( void ) { printf( "Hello World!\n" ); }
|
3. Compile the file to the shared library file.
gcc -Wall -shared -o libtest_lib.so test_lib.c
|
4. View the shared library file.
patrick@debian:~/test/libtest$ file libtest_lib.so libtest_lib.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
|
5. Create a test file to test the library function.
patrick@debian:~/test/libtest$ vi test_main.c
|
#include <stdio.h>
#include "test_lib.h"
int main(int argc, char *argv[]) { print_hello();
return 0; }
|
6. Compile the test code file.
gcc -Wall -o test_main test_main.c -ltest_lib -L.
|
7. Modefied the environment variable so that the library can be found.
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
|
8. run the programme.
patrick@debian:~/test/libtest$ ./test_main Hello World! patrick@debian:~/test/libtest$
|
阅读(1054) | 评论(0) | 转发(0) |