Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93225
  • 博文数量: 14
  • 博客积分: 490
  • 博客等级: 下士
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-20 11:00
文章分类

全部博文(14)

文章存档

2011年(1)

2008年(4)

2007年(9)

我的朋友
最近访客

分类: LINUX

2008-05-27 14:46:13

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) |
给主人留下些什么吧!~~