易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

修改linux启动参数挽回/etc/inittab文件

最近做linux系统裁剪,修改嵌入式系统的inittab文件时误将本机inittab文件修改。
reboot后出现
INIT: /etc/inittab[2]: missing id field
INIT: /etc/inittab[3]: missing id field.
Enterrunlevel:
输入3,或1,或singgle后出现
INIT: no more processess left in this runlevel
系统停止。
google,baidu到一篇引 ......

Linux 静态与动态链接库

文章选取的例子非常简单,上手容易,只是为了讲述静态与动态链接库的生成和链接过
    程,还有他们之间的区别。以下例子在 gcc 4.1.1 下顺利通过。 
文件预览
文件目录树如下,如你所见,非常简单。
libtest/  
|-- lt.c  
|-- lt.h  
`-- test. ......

linux下IP v6 tcp客户端源码示例

/***********************************
*
*client.c
*
**********************************/

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sy ......

linux下共享内存(shm)使用示例

#include <sys/ipc.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define PERM IPC_CREAT //S_IRUSR|S_IWUSR
#include <errno.h>
int main(int argc,char **argv)
{
int shmid[2048];
c ......

解决Linux中Too many open files问题

1、相关命令:
ulimit –a //查看当前设置
ulimit –n 2048 //即设成2048,按实际需要设置
2、用户环境参数文件配置:
在/etc/profile中加入如下内容:
if
 [ $SHELL 
=
 
"
/bin/ksh
"
 ]; then 
ulimit 
-

16384
 ulimit&nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号