代码业务比较复杂这里就不细说,主要记录下动态库的编译以及调用过程:
1. 创建动态链接库
gcc -shared -fPIC paraDocxml.c -o libmyxml.so
关于 -fPIC 参考这边笔记 http://blog.csdn.net/knight1840/article/details/47206855
可以看到,当前目录下多了一个文件libmyxml.so
接着生成可执行文件:
gcc TestFileOP.c -lmyxml -L. -lxml2 -L /usr/include/libxml2/ -otest
编译链接成功后会生成test在当前目录;
注意其中- l 参数后边跟的是需要链接的库名,-L .告诉编译器在查找库文件的目录
如果没有-l 和-L说明会出现错误:
collect2: ld returned 1 exit status
源文件:
paraDocxml.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libxml/parser.h> #include "paraDocxml.h" int paraDocxml(char *str_name,char *str_time,char *str_ver,char *str_md5,char *str_des,char *str_relation,char *str_spcversion) { xmlDocPtr doc; xmlNodePtr curNode; xmlChar *szKey; char *Doctmp="Grade_list.xml"; doc = xmlReadFile(Doctmp,"utf-8",XML_PARSE_RECOVER); if (NULL == doc) { fprintf(stderr,"Document not parsed successfully.\n"); return -1; } curNode = xmlDocGetRootElement(doc); if (NULL == curNode) { fprintf(stderr,"empty document.\n"); xmlFreeDoc(doc); return -1; } if (xmlStrcmp(curNode->name, BAD_CAST "grade_files")) { fprintf(stderr,"document of the wrong type, root node != root"); xmlFreeDoc(doc); return -1; } curNode = curNode->xmlChildrenNode; while(curNode != NULL) { printf("%s\n",curNode->name); if ((!xmlStrcmp(curNode->name, (const xmlChar *)"grade_file"))) { int ret; char *pch; szKey = xmlNodeGetContent(curNode); sscanf((char *)szKey,"%s\n%s\n%s\n%s\n%s\n%s\n%s\n",str_name,str_time,str_ver,str_md5,str_des,str_relation,str_spcversion); } curNode = curNode->next; } return 0; }
paraDocxml.h
#ifndef _paraDocxml_ #define _paraDocxml_ int paraDocxml(char *str_name,char *str_time,char *str_ver,char *str_md5,char *str_des,char *str_relation,char *str_spcversion); #endif ~
TestFileOP.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "paraDocxml.h"
void getCMDResult(const char *cmd,char *result)
{
char buf_ps[1024];
char ps[1024]={0};
FILE *ptr;
strcpy(ps,cmd);
if((ptr=popen(ps, "r"))!=NULL)
{
while(fgets(buf_ps, 1024, ptr)!=NULL)
{
strcat(result, buf_ps);
if(strlen(result)>1024)
{
break;
}
}
pclose(ptr);
ptr = NULL;
}
else
{
printf("the popen erro ");
}
}
int getCurVersion(char * curVersion) //get the version 6.0.6 or 6.0.9
{
FILE *fp;
char cmd[] = "ls -l |grep p6_tables";
char result[1024] = {0};
if ((fp=fopen("issuespc.net", "r")) == NULL)
{
printf("file is not exsit!\n");
getCMDResult(cmd,result);
if (strlen(result) > 0 )
strcpy(curVersion,"6.0.9");
}
else
{
strcpy(curVersion,"6.0.6");
}
fp = fopen("issuespc.net","a+");
if (NULL == fp)
return -1;
printf("%s\n",curVersion);
fprintf(fp,"%s\n",curVersion);
}
else
{
if( fgets(curVersion,sizeof(curVersion),fp)!= NULL)
{
printf("curVersuonforissue:%s",curVersion);
printf("get susses!\n");
printf("result:\n%s",result);
printf("curVersuon:\n%s",curVersion);
return 0;
}
}
return 0;
}
int main()
{
char curVersion[256];
char str_name[32]={0},str_time[32]={0},str_ver[32]={0};
char str_des[4000]={0},str_md5[64]={0};
char str_relation[128]={0};
char str_spcversion[32]={0};
getCurVersion(curVersion);
paraDocxml(str_name,str_time,str_ver,str_md5,str_des,str_relation,str_spcversion);
if (strstr(curVersion,str_spcversion)==NULL)
{
printf("The pekage is not match\n");
}
printf("curVersion:%s",curVersion);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。