Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532681
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2008-11-17 13:38:52

 看了marvell给的示例程序,他们是给我们一个库,让我们调他的函数。但具体代码他们没有给我们。

我也写了完成同样功能的代码,但不是用库的形式做的。之后,开始尝试做一下。


静态库:libReliableData.a的生成:

1生成 .o文件,不管是静态函数库还是动态函数库,都是由*.o目标文件生成。
        gcc -c  rd.c -o rd.o
2生成库:用ar -cr生成libReliableData.a
       ar -cr libReliableData.a rd.o
3用库编译程序:
    gcc test.c -o test -L. -lReliableData
4运行程序:
    ./test 程序正常运行,用的函数接口都是库里的了。
    静态库同时被链接到程序代码,被主程序调用的函数目标文件连同主程序组合成单一的 可执行程序。


动态库:
 1生成.o文件,不管是静态库还是动态库,都是由*.o目标文件生成。
      gcc -c rd.c -o rd.o
  2生成库:用gcc -shared -fPCI
        gcc -shared -fPCI librd.so rd.o
  3编译程序:
    
gcc test.c -o test -L. -lrd
  3运行程序:
    由于库中的代码没有编译到程序中去
    如果运行./test 会报错:
 
error while loading shared libraries: libmyhello.so: cannot open shared object file: No such file or directory

如何找到生成的动态库有3种方式:

1)把库拷贝到/usr/lib和/lib目录下。

(2)在LD_LIBRARY_PATH环境变量中加上库所在路径。

例如动态库libhello.so在/home/example/lib目录下:

$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/example/lib

(3) 修改/etc/ld.so.conf文件,把库所在的路径加到文件末尾,并执行ldconfig刷新。这样,加入的目录下的所有库文件都可见。

当静态库和动态库同名时, gcc命令将优先使用动态库。

 
--- rd_old.c    2008-11-18 14:50:26.000000000+0800
+++ rd_new.c    2008-11-18 14:49:44.000000000 +0800
@@ -1,10 +1,56 @@
 #include
 #include
 #include
-#include
 #include
 #include "rd.h"
 
+//校验和算法
+/*Reliable data defines*/
+
+#define VALID_BUF_STAMP 0xCAFECAFE
+#define CHECKSUM        0xb36e1207
+#define RD_FORMAT_VER   0x00001017
+#define IMEI_TYPE       0xCAFE0010
+#define NVM_TYPE        0xCAFE0020
+#define END_BUF_STAMP   0xDADADADA--------------------全局变量放到.c文件中
+#define CREATE_SIZE     128*1024  
+#define COUNTER_MAX     100
+
+#define IMEI_FILE "imei1234.bin"
+#define RF_FILE "GsmCalData.nvm"
+#define CREATE_FILE "rd.bin"
+
+/* Data structor */
+
+typedef struct tag_buffer_flash_header
+{
+    unsigned long validBufferStamp;
+    unsigned long bufCheckSum;        
+    unsigned long version;
+    unsigned long reserve1;
+    unsigned long reserve2;
+}FLASH_BUFFER_HEADER;
+
+
+typedef struct tag_flash_entry_header
+{
+    unsigned long entryType;
+    unsigned long entrySize;
+    unsigned char fileName[128];
+    //unsigned long *pFileToWrite;  
+}FLASH_ENTRY_HEADER;
+
+typedef struct tag_reliable_data
+{
+    FLASH_BUFFER_HEADER header;
+    FLASH_ENTRY_HEADER entry;
+}RD;
+/*Global variables*/
+
+FLASH_ENTRY_HEADER *entry[COUNTER_MAX];
+FLASH_BUFFER_HEADER buffer_header;
+unsigned long end_stamp = END_BUF_STAMP;
+int counter=0;// entry counter
 
 
 void buffer_header_initial()
@@ -18,6 +64,7 @@
 
 }
 
+/*function:  get file size */
 static unsigned long get_file_size( const char *filename )
 {  
    
@@ -56,6 +103,7 @@
 #endif
 }
 
+/*function : read file contents to buffer */
 static int read_file( char *filename, char *buffer, unsigned long length)
 {
     FILE *fp;
@@ -82,7 +130,7 @@
      return -1;    
         }
 }
-
+/* write buffer to file */
 static int write_file( const char *filename, char *buffer, unsigned long length)
 {
     FILE *fp;
@@ -109,6 +157,7 @@
      return -1;    
         }
 }
+/* free entry pointer */
 void free_entry()
 {
    int i;
@@ -117,18 +166,21 @@
     free( entry[i] );
    }
 }
+/* add an entry */
 int add_entry( const char* entry_name)
 {
+  unsigned long read_size;
   if( counter < COUNTER_MAX )
   {
-    
-    entry[counter] = (FLASH_ENTRY_HEADER *)malloc( entry_size );
+    //alloc memory
+    entry[counter] = (FLASH_ENTRY_HEADER *)malloc( sizeof(FLASH_ENTRY_HEADER) );
     if( entry[counter] == NULL )
     {
     printf( "when add entry[%d] alloc memory failed\n" ,counter);
         free_entry();
         return -1;
     }
+    //entry type
     if( counter == 0 )
     {
        entry[counter]->entryType = IMEI_TYPE;    
@@ -137,11 +189,28 @@
     {
     entry[counter]->entryType = NVM_TYPE ;
     }
+    //entry name
     memset(entry[counter]->fileName, 0, 128 );
-    strcpy(entry[counter]->fileName, entry_name );
-    if( (entry_size = get_file_size(entry_name) ) != -1)
+    if( strlen(entry_name) <= 128)
+    {    
+     strcpy(entry[counter]->fileName, entry_name );
+    }-------如果entry_name大于128时可能要失败,所以加判断
+    else
+    {
+       printf( "Add entry name is too long!\n" );
+       free_entry();
+       return -1;  
+    }
+    //entry size
+    if( (read_size = get_file_size(entry_name) ) != -1)
     {
-        entry[counter]->entrySize = entry_size;
+        entry[counter]->entrySize = read_size;
+    }
+    else
+    {
+       printf( "Read file:%s failed!\n",entry_name );
+       free_entry();
+       return -1;   
     }
     counter++;
     return 0;
@@ -154,29 +223,42 @@
   }
 }
 
+/* create the reliable data as a file */
 int create_rd( const char* rd_name)
 {
    int i;
    char *create_buffer; //save the whole contens which used to create the reliale data
    char *file_buffer;//save contents of entry file
-   unsigned long length = header_size;//record the position to copy contents;   
-
+   unsigned long length = sizeof(FLASH_BUFFER_HEADER);//record the position to copy contents;   
+   
+   //create header
    buffer_header_initial();
-   //alloc memory
+
+   //alloc whole memory buffer
    create_buffer = (char *)malloc( CREATE_SIZE* sizeof(char) );
    if (create_buffer == NULL)
    {
       printf( "Fail to alloc memory to create reliable data  buffer, errno is: %d, %s \n", errno, strerror( errno ) );
       return -1;
    }
+   //initial whole buffer
    memset( create_buffer, 0xff, CREATE_SIZE );
-   memcpy( create_buffer, &buffer_header, header_size);//buffer header
+   memcpy( create_buffer, &buffer_header, sizeof(FLASH_BUFFER_HEADER));//buffer header
    
    // copy entry contents to create_buffer
    for( i=0; i< counter; i++)
    {    
-        memcpy( create_buffer+length, entry[i], entry_size);
-        length += entry_size;
+        if( entry[i] != NULL )
+        {
+          memcpy( create_buffer+length, entry[i], sizeof(FLASH_ENTRY_HEADER));
+        }
---------如果entry[i]为空,则崩溃,所以要加判断,为了安全,用memcpy时一定要小心。当在add_entry里第一个空时,则这个程序可能就崩,所要一定要加判断。
+        else
+        {
+              printf( "Entry[%d] pointer is NULL or add_entry() return failed!\n" , counter);
+              free_entry();
+              return -1;
+        }
+        length += sizeof(FLASH_ENTRY_HEADER);
     file_buffer = (char *)malloc( entry[i]->entrySize);
         if (file_buffer == NULL)
         {
@@ -200,17 +282,33 @@
         length += entry[i]->entrySize;
         free( file_buffer );
    }   
-    //add end stamp
+   //add end stamp
    memcpy( create_buffer+length, &end_stamp, sizeof( unsigned long) );
    // create the reliable data
    if(write_file( rd_name, create_buffer, CREATE_SIZE) == -1)
    {
     printf( "Failed to create reliable data file.\n" );
         free_entry();
+        return -1;
文件:ReliableData2.rar
大小:19KB
下载:下载
    }
    free_entry();
    return 0;
 }




静态库与动态库

linux中有两类函数库,分别是静态库和动态库。

 

静态库是一些目标文件的集合,通常为后缀位.o的文件。通过ar工具打包而成,这类库的名字一般是libxxx.a;其中xxx为给定的静态库的文件名,如libm, 为数学函数库,用户也可以创建自己的静态库,在创建可执行程序的过程中,静态库同时被链接到程序代码,被主程序调用的函数目标文件连同主程序组合成单一的 可执行程序,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库的支持,因为所有的函数都已经被编译进去了。当然这也会成为他的缺点,因为如果 静态函数库改变了,那么你的程序必学重新编译。

动态函数库,这类库的名字一般是libxxx.so; 相对于静态函数库,动态函数库在编译的时候并没有被编译进目标代码中,你的程序执行到相关函数时才调用该函数库里的相应函数,因此动态函数库所产生的可执 行文件比较小,由于函数库没有被整合到你的程序,而是程序运行时动态的申请并调用。所以程序的运行环境中必须提供相应的库。动态函数库的改变并不影响你的 程序。使用动态库创建执行程序,分为两个阶段:

链接阶段,即通过ld创建执行程序时,链接编译器会在指定的动态库搜索、解析被主程序调用的函数及其其他变量等,如引用被找到,则在执行程序的XCOFF头结构的loader区域,建立包含引用的动态库的影像,反之,如在指定的动态库中没有找到此引用的定义,编译器会给出类似未定义的符号引用错误,这不同于静态库,包含引用的目标文件并不和执行程序链接在一起。

另一个阶段为运行阶段即在执行程序运行时程序运行时系统相关模块将读取定义执行程序的XCOFF头结构中的信息,查找并加载相关的动态库,假设,所有被应用的动态库都被定位且加载后,程序将开始运行


==================================================


3、查看库中的符号

有时候可能需要查看一个库中到底有哪些函数,nm命令可以打印出库中的涉及到的所有符号。库既可以是静态的也可以是动态的。nm列出的符号有很多,常见的有三种:

一种是在库中被调用,但并没有在库中定义(表明需要其他库支持),用U表示;

一种是库中定义的函数,用T表示,这是最常见的;

另外一种是所谓的“弱 态”符号,它们虽然在库中被定义,但是可能被其他库中的同名符号覆盖,用W表示。

例如,假设开发者希望知道上文提到的hello库中是否定义了 printf():

$nm libhello.so |grep printf U

其中printf U表示符号printf被引用,但是并没有在函数内定义,由此可以推断,要正常使用hello库,必须有其它库支持,再使用ldd命令查看hello依赖于哪些库:

$ldd hello libc.so.6=>/lib/libc.so.6(0x400la000) /lib/ld-linux.so.2=>/lib/ld-linux.so.2 (0x40000000)

从上面的结果可以继续查看printf最终在哪里被定义,有兴趣可以go on


4、生成库

第一步要把源代码编绎成目标代码。

以下面的代码为例,生成上面用到的hello库:

 /* hello.c */

#include <> 

void sayhello()

{

  printf("hello,world ");

}

用gcc编绎该文件,在编绎时可以使用任何全法的编绎参数,例如-g加入调试代码等: gcc -c hello.c -o hello.o

(1)连接成静态库 连接成静态库使用ar命令,其实ar是archive的意思

 $ar cqs libhello.a hello.o

(2)连接成动态库 生成动态库用gcc来完成,由于可能存在多个版本,因此通常指定版本号:

$gcc -shared -Wl,-soname,libhello.so.1 -o libhello.so.1.0 hello.o

另外再建立两个符号连接:

$ln -s libhello.so.1.0 libhello.so.1

$ln -s libhello.so.1 libhello.so

这样一个libhello的动态连接库就生成了。最重要的是传gcc -shared 参数使其生成是动态库而不是普通执行程序。 -Wl 表示后面的参数也就是-soname,libhello.so.1直接传给连接器ld进行处理。实 际上,每一个库都有一个soname,当连接器发现它正在查找的程序库中有这样一个名称,连接器便会将soname嵌入连结中的二进制文件内,而不是它正 在运行的实际文件名,在程序执行期间,程序会查找拥有 soname名字的文件,而不是库的文件名,换句话说,soname是库的区分标志。 这样做的目的主要是允许系统中多个版本的库文件共存,习惯上在命名库文件的时候通常与soname相同 libxxxx.so.major.minor 其中,xxxx是库的名字,major是主版本号,minor 是次版本号


===========================================================

旧的东西:


旧文件:ReliableData2.rar
大小:19KB
下载:下载




新文件:ReliableData3.rar
大小:17KB
下载:下载

阅读(1429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~