将带VECTOR的类导出到DLL的问题
我将一些代码导出成DLL,然后用EXE连接,发现在DLL中使用到的std::vector 异常,经过研究,我发现是代码生成中运行时库设置有误造成。
我有几个问题:
1:运行时阶段2者有什么区别(当然还有MD/MDd。。。懒得测试了)
2:如果我自己写个vector模版类,还会异常吗
3:能否做到MT/MTd做的DLL,MT MTd都能用
以下是归纳的测试代码:
//testdll.h:
//结论
//1.当DLL、EXE都使用MT或MTd时,打印正常: 1 abcd 1 1 1 1 4
//2.当DLL使用MT,EXE使用MTd时,打印为:0 0 0 0 0 4 :只有不使用vector时正常,vector中的push_back(。。)出错。
//3.当DLL使用MTd,EXE使用MT时,打印为:xxxxxxx(一堆无用数字) 然后出错。。。,注释掉出错部分后,当只有不使用vector时正常,vector完全出错。
//结论为,无论什么时候都应使用1.。。。
//当使用MT时就无法定义宏_DEBUG,库连接错误,故无法检测异常。(但是我还是决定使用MT。。。)
//testdll.h:
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <vector>
#ifdef DLL
#define MYDLL __declspec(dllexport)
#endif
#ifdef DLL
class MYDLL testclass
#else
class testclass
#endif
{
public:
testclass();
testclass(char* t__);
~testclass();
public:
char* t;
};
#ifdef DLL
class MYDLL testdll
#else
class testdll
#endif
{
public:
testdll(void);
testdll(testclass* cpi);
~testdll(void);
public:
std::vector<testclass*>* tpvpi;
};
#ifdef DLL
class MYDLL testdll2
#else
class testdll2
#endif
{
public:
testdll2(void);
testdll2(int* cpi);
~testdll2(void);
public:
std::vector<int*>* tpvpi;
};
#ifdef DLL
class MYDLL testdll3
#else
class testdll3
#endif
{
public:
testdll3(void);
testdll3(int* cpi);
~testdll3(void);
public:
std::vector<int*> tpvpi;
};
#ifdef DLL
class MYDLL testdll4
#else
class testdll4
#endif
{
public:
testdll4(void);
testdll4(int* cpi);
~testdll4(void);
public:
std::vector<int> tpvpi;
};
#ifdef DLL
class MYDLL testdll5
#else
class testdll5
#endif
{
public:
testdll5(void);
testdll5(int cpi);
~testdll5(void);
public:
std::vector<int> tpvpi;
};
#ifdef DLL
class MYDLL testdll6
#else
class testdll6
#endif
{
public:
testdll6(void);
testdll6(int* cpi);
~testdll6(void);
public:
int* ttpi;
};
//testdll.cpp:
#include "testdll.h"
#ifdef DLL
testclass::testclass()
{
t=NULL;
}
testclass::testclass(char* t__)
{
if(!t__)
{
t=NULL;return;
}
t=new char[strlen(t__)+1];
strcpy(t,t__);
return;
}
testclass::~testclass()
{
if(!t)return;
delete t;
t=NULL;
}
testdll::testdll(void)
{
tpvpi=new std::vector<testclass*>();
tpvpi->clear();
}
testdll::testdll(testclass* cpi)
{
tpvpi=new std::vector<testclass*>();
testclass* tcpi=cpi;
tpvpi->clear();
tpvpi->push_back(tcpi);
}
testdll::~testdll(void)
{
if(!tpvpi)return;
for(unsigned long i=0;i<tpvpi->size();i++)
delete (*tpvpi)[i];
tpvpi->clear();
delete tpvpi;
}
testdll2::testdll2(void)
{
tpvpi=new std::vector<int*>();
tpvpi->clear();
}
testdll2::testdll2(int* cpi)
{
tpvpi=new std::vector<int*>();
tpvpi->clear();
tpvpi->push_back(cpi);
}
testdll2::~testdll2(void)
{
if(!tpvpi)return;
for(unsigned long i=0;i<tpvpi->size();i++)
delete (*tpvpi)[i];
tpvpi->clear();
delete tpvpi;
}
testdll3::testdll3(void)
{
tpvpi.clear();
}
testdll3::testdll3(int* cpi)
{
tpvpi.clear();
tpvpi.push_back(cpi);
}
testdll3::~testdll3(void)
{
for(unsigned long i=0;i<tpvpi.size();i++)
delete (tpvpi)[i];
tpvpi.clear();
}
testdll4::testdll4(void)
{
tpvpi.clear();
}
testdll4::testdll4(int* cpi)
{
tpvpi.clear();
tpvpi.push_back(*cpi);
}
testdll4::~testdll4(void)
{
tpvpi.clear();
}
testdll5::testdll5(void)
{
tpvpi.clear();
}
testdll5::testdll5(int cpi)
{
tpvpi.clear();
tpvpi.push_back(cpi);
}
testdll5::~testdll5(void)
{
tpvpi.clear();
}
testdll6::testdll6(void)
{
ttpi=NULL;
}
testdll6::testdll6(int* cpi)
{
ttpi=new int;
*ttpi=*cpi;
}
testdll6::~testdll6(void)
{
delete ttpi;
}
#else
#pragma comment(lib,"testdll.lib")
int _tmain(int argc, char* argv[])
{
//*
testclass* tpi=new testclass("abcd");
testdll* pt=new testdll(tpi);
printf("%d\n",pt->tpvpi->size());
if(pt->tpvpi->size()>0)
{
printf((*(pt->tpvpi))[0]->t);
printf("\n");
}
//*/
int* tpi2=new int;
*tpi2=4;
//*
testdll2* pt2=new testdll2(tpi2);
printf("%d\n",pt2->tpvpi->size());
testdll3* pt3=new testdll3(tpi2);
printf("%d\n",(pt3->tpvpi).size());
testdll4* pt4=new testdll4(tpi2);
printf("%d\n",(pt4->tpvpi).size());
testdll5* pt5=new testdll5(*tpi2);
printf("%d\n",(pt5->tpvpi).size());
//*/
testdll6* pt6=new testdll6(tpi2);
printf("%d\n",*(pt6->ttpi));
return 1;
}
#endif
//虽然有了结论,但我还不知道在运行时阶段2者有什么区别。。。请教高手