#ifndef __DATETIME_H
#define __DATETIME_H
/* 日期结构 */
typedef struct
{
int year;
int mon;
int day;
}DATETYPE;
/* 时间结构 */
typedef struct
{
char hour;
char min;
char sec;
}TIMETYPE;
/* 函数原型说明 */
class libTest
{
public:
int getdate(DATETYPE *d);
int gettime(TIMETYPE *t);
};
#ifdef SHARED
extern "C" int (*_getdate)(libTest * _lib,DATETYPE *d);
#else
int _getdate(libTest * _lib,DATETYPE *d);
#endif
#ifdef SHARED
extern "C" int (*_gettime)(libTest * _lib,TIMETYPE *t);
#else
int _gettime(libTest * _lib,TIMETYPE *t);
#endif
#endif
#include "time.h"
#include "libTest.h"
int libTest::getdate(DATETYPE *d)
{
long ti;
struct tm *tm;
time(&ti);
tm=localtime(&ti);
d->year=tm->tm_year+1900;
d->mon=tm->tm_mon+1;
d->day=tm->tm_mday;
return 0;
}
int libTest::gettime(TIMETYPE *t)
{
long ti;
struct tm *tm;
time(&ti);
tm=localtime(&ti);
t->hour=tm->tm_hour;
t->min=tm->tm_min;
t->sec=tm->tm_sec;
return 0;
}
int _getdate(libTest * _lib,DATETYPE *d)
{
return _lib->getdate(d);
}
int _gettime(libTest * _lib,TIMETYPE *t)
{
//libTest _lib=new libTest();
return _lib->gettime(t);
}
通过g++ libTest.cpp -shared -fPIC -o libTest.so 编程成功,生成了libTest.so
然后编写调用程序test.cpp
#include "stdio.h" /* 包含标准输入输出文件 */
#include "dlfcn.h" /* 包含动态链接功能接口文件 */
#include <iostream>
#define SOFILE "./libTest.so" /* 指定动态链接库名称 */
#define SHARED /* 定义宏,确认共享,以便引用动态函数 */
#include "libTest.h" /* 包含用户接口文件 */
using namespace std;
int main()
{
DATETYPE d;
TIMETYPE t;
libTest _lib;
void *dp;
char *error;
puts("动态链接库应用示范");
dp=dlopen(SOFILE,RTLD_LAZY); /* 打开动态链接库 */
if (dp==NULL) /* 若打开失败则退出 */
{
fputs(dlerror(),stderr);
return 1;
}
_getdate=(int(*)(libTest * _lib,DATETYPE * d))dlsym(dp,"getdate"); /* 定位取日期函数 */
error=dlerror(); /* 检测错误 */
if (error) /* 若出错则退出 */
{
fputs(error,stderr);
return 1;
}
_getdate(&_lib,&d); /* 调用此共享函数 */
printf("当前日期: %04d-%02d-%02d\n",d.year,d.mon,d.day);
_gettime=(int(*)(libTest * _lib,TIMETYPE * t))dlsym(dp,"gettime"); /* 定位取时间函数 */
error=dlerror(); /* 检测错误 */
if (error) /* 若出错则退出 */
{
fputs(error,stderr);
return 1;
}
_gettime(&_lib,&t); /* 调用此共享函数 */
printf("当前时间: %02d:%02d:%02d\n",t.hour,t.min,t.sec);
dlclose(dp); /* 关闭共享库 */
return 0; /* 成功返回 */
}
可我怎么编译test.cpp都编译不过
老大们,帮帮忙!