推荐一本书
玩过星际的人都会深有所感。不过让人喜爱的是小白花。
+++++仿佛轻云应笑我--相携水岸弄朝霞--此生只为听天籁--坐拥书城看落花+++++
假设有下面几个文件:
头文件String.h,声明相关函数原形,内容如下:
Strlen.c:函数Strlen的实现,获取给定字符串的长度,内容如下:
Strlnen.c:函数StrNlen的实现,获取给定字符串的长度,如果输入字符串的长度大于指定的最大长度,则返回最大长度,否者返回字符串的实际长度,内容如下:
生成静态库:
利用GCC生成对应目标文件:
gcc �Cc Strlen.c Strnlen.c
如果对应的文件没有错误,gcc会对文件进行编译生成Strlen.o和Strnlen.o两个目标文件(相当于windows下的obj文件)。然后用
ar创建一个名字为libstr.a的库文件,并把Strlen.o 和Strnlen.o的内容插入到对应的库文件中。,相关命令如下:
ar �Crc libstr.a Strlen.o Strnlen.o
命令执行成功以后,对应的静态库libstr.a已经成功生成。
/***********************************
Filename : String.h
Description :
Author : HCJ
Date : 2006-5-7
************************************/
int Strlen(char *pStr);
int StrNlen(char *pStr, unsigned long ulMaxLen);
/**************************************
Filename : get string length
Description :
Author : HCJ
Date : 2006/5/7
**************************************/
#include<stdio.h>
#include<assert.h>
int Strlen(char *pStr)
{
unsigned long ulLength;
assert(NULL != pStr);
ulLength = 0;
while(*pStr++)
{
ulLength++;
}
return ulLength;
}
**********************************************
Fileneme: mystrnlen.c
Description: get input string length,if string large
max length input return max length,
else real length
Author: HCJ
Date : 2006-5-7
**********************************************/
#include<stdio.h>
#include<assert.h>
int StrNlen(char *pStr, unsigned long ulMaxLen)
{
unsigned long ulLength;
assert(NULL != pStr);
if(ulMaxLen <= 0)
{
printf("Wrong Max Length!\n");
return -1;
}
ulLength = 0;
while(*pStr++ && ulLength < ulMaxLen)
{
ulLength++;
}
return ulLength;
}
生成动态链接库:
gcc -fpic -shared -o libstr.so Strlen.c Strnlen.c
-fpic 使输出的对象模块是按照可重定位地址方式生成的。
-shared指定把对应的源文件生成对应的动态链接库文件libstr.so文件。
对应的链接库已经生成,下面看一下如何使用对应的链接库。
静态库的使用:
假设有下面的文件要使用对应的的静态库:
编译生成对应的目标文件:
gcc -c -I/home/hcj/xxxxxxxx main.c
生成可执行文件:
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.a
其中-I/home/hcj/xxxxxxxx和-L/home/hcj/xxxxxxxx是通过-I和-L指定对应的头文件和库文件的路径。
libstr.a是对应的静态库的名称。这样对应的静态库已经编译到对应的可执行程序中。执行对应的可执行文件便可以对应得函数调用的结果。
/*****************************************
FileName: main.c
Description: test static/dynamic library
Author: HCJ
Date : 2005-5-7
******************************************/
#include<stdio.h>
#include <String.h> //静态库对应函数的头文件
int main(int argc, char* argv[])
{
char str[] = {"hello world"};
unsigned long ulLength = 0;
printf("The string is : %s\n", str);
ulLength = Strlen(str);
printf("The string length is : %d(use Strlen)\n", ulLength);
ulLength = StrNlen(str, 10);
printf("The string length is : %d(use StrNlen)\n", ulLength);
return 0;
}
动态库的分为隐式调用和显式调用两种调用方法:
隐式调用的使用使用方法和静态库的调用差不多,具体方法如下:
gcc -c -I/home/hcj/xxxxxxxx main.c
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.so //这里是*.so
在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动态链接库配置文件中。否则在执行相关的可执行文件的时候就会出现载入动态链接库失败的现象。在编译所引用的动态库时,可以在gcc采用
�Cl或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。
显式调用:
/*****************************************
FileName: main2.c
Description: test static/dynamic library
Author: HCJ
Date : 2005-5-7
******************************************/
#include<stdio.h>
#include<dlfcn.h>
int main(int argc, char* argv[])
{
//define function pointor
int (*pStrlenFun)(char* pStr); //声明对应的函数的函数指针
int (*pStrnlenFun)(char* pStr, int ulMaxLen);
char str[] = {"hello world"};
unsigned long ulLength = 0;
void *pdlHandle;
char *pszErr;
pdlHandle = dlopen("./libstr.so", RTLD_LAZY); //加载链接库/libstr.so
if(!pdlHandle)
{
printf("Failed load library\n");
}
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s\n", pszErr);
return 0;
}
//get function from lib
pStrlenFun = dlsym(pdlHandle, "Strlen"); //获取函数的地址
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s\n", pszErr);
return 0;
}
pStrnlenFun = dlsym(pdlHandle, "StrNlen");
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s\n", pszErr);
return 0;
}
printf("The string is : %s\n", str);
ulLength = pStrlenFun(str); //调用相关的函数
printf("The string length is : %d(use Strlen)\n", ulLength);
ulLength = pStrnlenFun(str, 10);
printf("The string length is : %d(use StrNlen)\n", ulLength);
dlclose(pdlHandle);
return 0;
}
gcc -o mian2 -ldl main2.c
用gcc编译对应的源文件生成可执行文件,-ldl选项,表示生成的对象模块需要使用共享库。执行对应得文件同样可以得到正确的结果。
相关函数的说明如下:
(1)dlopen()
第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库。
-环境变量LD_LIBRARY_PATH列出的用分号间隔的所有目录。
-文件/etc/ld.so.cache中找到的库的列表,用ldconfig维护。
-目录usr/lib。
-目录/lib。
-当前目录。
第二个参数:指定如何打开共享库。
-RTLD_NOW:将共享库中的所有函数加载到内存
-RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数
(2)dlsym()
调用dlsym时,利用dlopen()返回的共享库的phandle以及函数名称作为参数,返回要加载函数的入口地址。
(3)dlerror()
该函数用于检查调用共享库的相关函数出现的错误。
这样我们就用简单的例子说明了在Linux下静态/动态库的创建和使用。
比Windows强多了。
LaTeX 123
http://edt1023.sayya.org/tex/latex123/latex123.html
现在全面转向Ubuntu了,有开源UML工具做的如同StarUML的吗?有流程图软件吗?
12.13
天凉无冬意
芭蕉昨夜雨
小径衔花落
如丝随风起
12.13
门外风收雨住
小鸟花丛深处
################
#
# ButtonDock Functions. These collection of functions create
# single icon buttons that cause the button to hover above
# everything when the mouse is moved atop of it. To get these
# to work it should only take copying all the five functions
# CreateDock, EnterHandler, LeaveHandler, RestoreButton
# and CreateActiveButton. Then call the function 'CreateDock'
# for each button on the dock bar. The function works as follows:
#
# CreateDock '$0' '$1' '$2' '$3' '$4' '$5' '$6' '$7' '$8' '$9'
# $0 -> ButtonId
# $1 -> Geometry
# $2 -> Icon
# $3 -> LargeIcon
# $4 -> X offset (from upper left hand corner)
# $5 -> Y offset (from upper left hand corner)
# $6 -> action for Mouse 1
# $7 -> action for Mouse 2
# $8 -> action for Mouse 3
# $9 -> Size of LargeIcon
#
###################################
DestroyFunc CreateDock
AddToFunc CreateDock
+ I DestroyModuleConfig FvwmDock-$0: *
+ I *FvwmDock-$0: Geometry $1
+ I *FvwmDock-$0: Font none
+ I *FvwmDock-$0: Colorset 9
+ I *FvwmDock-$0: Frame 0
+ I *FvwmDock-$0: Padding 0 0
+ I *FvwmDock-$0: (Id D$0, Icon $2)
+ I Module FvwmButtons FvwmDock-$0
+ I AddToFunc StartFunction I Module FvwmAuto 100 -menter
'EnterHandler $0 "$2" $3 $4 $5 $6 $7 $8 $9' 'LeaveHandler $0 $2'
DestroyFunc EnterHandler
AddToFunc EnterHandler
+ I ThisWindow (FvwmDock-$0) ThisWindow (!FvwmDock-*) Deschedule 35
+ I ThisWindow (FvwmDock-$0) WindowId $[last_winid] (FvwmDockA$0) Deschedule 35
+ I ThisWindow (FvwmDockA$0) Deschedule 45
+ I ThisWindow (FvwmDock-$0) SendToModule FvwmDock-$0 ChangeButton D$0
Icon "shadow.png"
+ I ThisWindow (FvwmDock-$0) CreateActiveButton $0 '$1' $2 $3 $4 $5 $6 $7 $8
# Delay ms should be longer than delay in FvwmAuto
DestroyFunc LeaveHandler
AddToFunc LeaveHandler
+ I ThisWindow SetEnv last_winid $[w.id]
+ I ThisWindow (FvwmDockA$0) Schedule 110 35 RestoreButton $0 $1
+ I ThisWindow (FvwmDock-$0) Schedule 110 45 RestoreButton $0 $1
DestroyFunc RestoreButton
AddToFunc RestoreButton
+ I SendToModule FvwmDock-$0 ChangeButton D$0 Icon $1
+ I KillModule FvwmButtons FvwmDockA$0
DestroyFunc CreateActiveButton
AddToFunc CreateActiveButton
+ I DestroyModuleConfig FvwmDockA$0: *
+ I *FvwmDockA$0: Pixmap none
+ I *FvwmDockA$0: BoxSize fixed
+ I *FvwmDockA$0: Frame 0
+ I *FvwmDockA$0: Font none
+ I *FvwmDockA$0: Padding 0 0
+ I *FvwmDockA$0: Rows 1
+ I *FvwmDockA$0: Columns 1
+ I ThisWindow PipeRead `echo *FvwmDockA$0: Geometry
$8+$(($[w.x]$3))+$(($[w.y]$4))`
+ I *FvwmDockA$0: (Icon $2, Action(Mouse 1) '$5', Action(Mouse 2)
'$6', Action(Mouse 3) '$7')
+ I Module FvwmButtons FvwmDockA$0
How these functions work is all you need to do is call the CreateDock
function from your StartFunction (or fvwm2rc file) for each icon you
want in your dock. As seen above the CreateDock function takes 10
arguments, that tell it what size, icons, and actions you want to bind
to the button. For example say you wanted to create a two icon dock
with one icon above the other. What you would do is call the
CreateDock function twice for both docks, and then use the geometry
option to stack the two icons right next to each other. For example I
added the two lines to my StartFunction;
+ I CreateDock '10' '56x56-74+62@1' '48x48/firefox.png'
'64x64/firefox.png' '-20' '-30' 'FvwmFireFox' 'Nop' 'Nop' '64x64'
+ I CreateDock '11' '56x56-74+116@1' '48x48/xmms.png' '64x64/xmms.png'
'+20' '-30' 'FvwmXmms' 'Nop' 'FvwmMixer' '64x64'
This creates two Docks, number 10 and number 11. Then when I move my
mouse over one of the Docks, it will replace the current icon with
shadow.png and then make a temporary Dock which has the large icon in
it. The following three pictures should show how this effect works.
Depending on your cpu this may be a little choppy, but all in all I
still think it looks good.