Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563459
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-05-16 15:07:10

osip 中有很多十分轻量级并且高效的库函数,本文简单的介绍一下如何在 linux 平台下编译安装并且将 osip 中的库函数引入到你自己的代码中。

1. 编译安装 osip
  1.1 下载 osip 的源码包 ,
     1.2 解压 osip 的源码包  ,   tar -xzf osip.****.tar.gz
     1.3 编译源码包,并制定安置生成的可执行二进制文件的路径(就是存放 .so 文件的路径,不过在这里 .so 文件在 ... target path/lib/ 文件夹下面)
         ./configure --prefix=/usr/lib64/
            如果不指定 --prefix= target path 的话, 装有可执行二进制文件的 lib 文件夹 和装有头文件的 include 文件夹将会自动的在 /usr/local/
            路径下面被生成,可以通过创建软链接的方式将其引入到 /usr/lib64/ 的下面,然后通过命令 ldconfig 来更新存放链接的索引文件即可。

2. 引入 osip 库来编写属于你自己的代码
    首先,最重要的一点就是,生成的 include/ 下面的源代码有一点点的问题,需要修改一下,不然在运行的时候会报错
        这个错误是由于跨平台编程的时候,linux 平台下面少写了引入 头文件 time.h , sys/time.h 而造成的
         
        将路径切换到你刚刚 --preifx= target path 的, target path 的下面,找到 include 文件夹,
        然后切换到 include/osip2/ 下面打开 osip.h 文件,
        在 #ifndef #define 结束之后的那一行开始的地方添加  "#include #include " 这两个头文件的引入
        并且在 include/osip2/osip_time.h 文件中同样的地方,添加 "#include " 即可
       
        编写文件代码 test.cpp
        编译文件的 g++ 运行命令中不要忘了添加 -losip2 来将库文件包含在源代码中
         // test.cpp
        

点击(此处)折叠或打开

  1. #ifndef OSIP_MT  // do not forget this macro import
  2. #define OSIP_MT
  3. #endif

  4. #include <osip2/osip_mt.h>
  5. #include <osip2/osip_condv.h>
  6. #include <osip2/osip.h>

  7. void* print ( void *arg )
  8. {
  9.    for ( int i = 0 ; i < 10 ; i++ )
  10.    printf ("this is only a test \n") ;
  11.     
  12.    return (void*) 0 ;
  13. }

  14. int main ( int argc ,char **argv )
  15. {
  16.   void * thread_obj = NULL ;
  17.   
  18.   typedef void*(*func_ptr_t )(void*) ;
  19.   
  20.    func_ptr_t func_ptr ;

  21.    func_ptr = print ;

  22.  // (*func_ptr) () ;
  23.    // 20000 means the space you ask from the system (bits)
  24.    // func_ptr means the method pointer ,
  25.    // you want the thread load which method input its pointer is ok
  26.    // the last argument is the parameters pass into the function you want the thread load
  27.    // because the method print we want to run do not need it , so we pass NULL is ok

  28.   thread_obj = (void *)osip_thread_create ( 20000 ,func_ptr , NULL ) ;
  29.     
  30.   if ( thread_obj == NULL )
  31.   {
  32.     perror ("failed to create thread obj by osip_thread_create method ") ;
  33.     return -1 ;
  34.   }

  35.  printf ("here we go to run the thread \n") ;
  36.     
  37.  if ( osip_thread_join ( (struct osip_thread*) thread_obj ) != 0 )
  38.  {
  39.     perror ("failed to execute osip_thread_join method") ;
  40.     return -1 ;        
  41.  }

  42.   osip_free ( thread_obj ) ; // do not release the space
  43.   thread_obj = NULL ;
  44.   return 0 ;
  45. }


         运行命令
         g++ test.cpp -losip2 -o test

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