一.生成库文件
cat test.h
#ifndef __TEST_H__
#define __TEST_H__
#include <string.h>
char CmdStr[200][200];
class TEST
{
public:
TEST();
virtual ~TEST();
public:
int SplitBufByChar( char *buf1, char ch, char tc,int stno);
};
#endif
cat test.cpp
#include "test.h"
TEST::TEST()
{
for(int i=0;i<200;i++)
{
CmdStr[i][0]='\0';
}
}
TEST::~TEST()
{
for(int i=0;i<200;++i)
{
CmdStr[i][0]='\0';
}
}
int TEST::SplitBufByChar( char *buf1, char ch, char tc,int stno)
{
char *ss0, *ss1;
int i,j;
char T[200][500];
ss0 = buf1; i = 0;
while ( (ss1=strchr( ss0, ch )) != NULL ) {
*ss1 = 0; strcpy( T[stno+i], ss0 );
i++; ss0= ss1+1; *ss1= ch;
}
if ( tc=='C' ) for (j=stno;j<stno+i;j++) strcpy(CmdStr[j],T[j]);
return( i );
}
cat makefile
DEST=libtest.a
OBJS=test.o
CC= g++
CPPFLAGS = -O2 -Wall
SHAREFLAGS = -shared -fPIC
INCLUDES = -I.
$(DEST) : $(OBJS)
$(CC) $(SHAREFLAGS) $(INCLUDES) $(CPPFLAGS) -o $@ $^
test.o : test.cpp test.h
$(CC) $(INCLUDES) $(CPPFLAGS) -c $< -o $@
二.使用动态库
cat protest.cpp
#include "test.h"
#include <string>
#include <vector>
using namespace std;
typedef struct{
int id;
string name;
}STUDENT;
typedef std::vector <STUDENT> STRSET;
//extern char CmdStr[200][200];
int main()
{
char sc[200];
TEST test1;
STUDENT stdt1,stdt2,stdt3;
STRSET settest;
stdt1.id=200;
stdt1.name="GPRS100~SMS-03-17~新GPRS100元套餐办理~";
stdt2.id=100;
stdt2.name="GPRS200~SMS-03-18~新GPRS100元套餐办理~";
stdt3.id=300;
stdt3.name="GPRS300~SMS-03-19~新GPRS100元套餐办理~";
settest.push_back(stdt1);
settest.push_back(stdt2);
settest.push_back(stdt3);
for(std::vector <STUDENT>::iterator it=settest.begin();it !=settest.end();it++)
{
sprintf(sc,"%s",((STUDENT)*it).name.c_str());
test1.SplitBufByChar(sc,'~','C',1);
//test1.~TEST();
printf("cmdstr[1]:%s\n",CmdStr[1]);
printf("cmdstr[2]:%s\n",CmdStr[2]);
printf("cmdstr[3]:%s\n",CmdStr[3]);
}
return 0;
}
cat makefile
DEST=protest
OBJS=protest.o
CC= g++
CPPFLAGS = -O2 -Wall
INCLUDES = -I. -I../libtest/
LIBS =-L../libtest/ -ltest
$(DEST) : $(OBJS)
$(CC) $(INCLUDES) $(LIBS) $(CPPFLAGS) -o $@ $^
protest.o : protest.cpp
$(CC) $(INCLUDES) $(CPPFLAGS) -c $< -o $@