Linux系统共享库编程
一、说明
类似Windows系统中的动态链接库,Linux中也有相应的共享库用以支持代码的复用。Windows中为*.dll,而Linux中为*.so。下面详细介绍如何创建、使用Linux的共享库。
二、创建共享库
在mytestso.c文件中,代码如下:
#include <stdio.h>
#include <stdlib.h>
int GetMax(int a, int b)
{
if (a >= b)
return a;
return b;
}
int GetInt(char* psztxt)
{
if (0 == psztxt)
return -1;
return atoi(psztxt);
}
然后使用下列命令进行编译:
gcc -fpic -shared mytestso.c -o mytestso.so
-fpic 使输出的对象模块是按照可重定位地址方式生成的
编译成功后,当前目录下有mytestso.so,此时已成功创建共享库mytestso.so。
三、使用共享库
共享库中的函数可被主程序加载并执行,但是不必编译时链接到主程序的目标文件中。主程序使用共享库中的函数时,需要事先知道所包含的函数的名称(字符串),然后根据其名称获得该函数的起始地址(函数指针),然后即可使用该函数指针使用该函数。
在mytest.c文件中,代码如下:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
void* pdlhandle;
char* pszerror;
int (*GetMax)(int a, int b);
int (*GetInt)(char* psztxt);
int a, b;
char* psztxt = "1024";
// open mytestso.so
pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
pszerror = dlerror();
if (0 != pszerror) {
printf("%s\n", pszerror);
exit(1);
}
// get GetMax func
GetMax = dlsym(pdlhandle, "GetMax");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s\n", pszerror);
exit(1);
}
// get GetInt func
GetInt = dlsym(pdlhandle, "GetInt");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s\n", pszerror);
exit(1);
}
// call fun
a = 200;
b = 600;
printf("max=%d\n", GetMax(a, b));
printf("txt=%d\n", GetInt(psztxt));
// close mytestso.so
dlclose(pdlhandle);
}
然后使用如下命令进行编译:
gcc mytest.c -ldl -o mytest
-ldl选项,表示生成的对象模块需要使用共享库
(1)dlopen()
第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库。
-环境变量LD_LIBRA
相关文档:
1. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI) 就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......
linux shell pwd 显示当前路径
假若有test.cpp
g++ test.cpp -o test
./test
想在test中找到当前执行程序所在的路径
可以再test.cpp中使用readlink函数
具体见如下实例:
#include<iostream>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<string>
using ......
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <s ......
/******************************
*
* server.c
*
******************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<errno.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/wait.h> ......
系统要求:
内存:推荐1G
Swap分区:设为内存的2倍
/tmp磁盘空间:400MB以上三
磁盘空间:软件3.5G 数据1.2G
所需软件:
SuSE Linux10 for x86
Oracle database 10gR2 for Linux32
Orarun-1.8-109.15.i586.rpm软件包,可以从http://Ftp.novell.com/partners/Oracle/sels-9下载
&n ......