VS2010中DLL和static LIB都导出STL类时候怎么解决冲突
有多个工程,有些是作为静态库,有些是DLL的,里面的类包含std::string,
在静态库中的类包含了static的string,而DLL中有个类是继承于string的,两个编译生成的LIB文件中都存在basic_string的定义,这样我在主程序中调用这些库,就会有重复定义的问题,这个如何解决。
我将静态库的运行时库换成Common Language Runtime Support (/clr)就可以了,但其他的换成这样的就会报错。
静态库,test2
class test2
{
public:
test2(void);
~test2(void);
public:
std::string str;
static std::string static_str;
};
动态库,test1
#ifdef EXP_STL
# define DECLSPECIFIER __declspec(dllexport)
# define EXPIMP_TEMPLATE
#else
# define DECLSPECIFIER __declspec(dllimport)
# define EXPIMP_TEMPLATE extern
#endif
EXPIMP_TEMPLATE template class DECLSPECIFIER std::allocator<char>;
EXPIMP_TEMPLATE template class DECLSPECIFIER std::basic_string<char>;
EXPIMP_TEMPLATE template class DECLSPECIFIER std::basic_filebuf<char>;
EXPIMP_TEMPLATE template class DECLSPECIFIER std::basic_ofstream<char>;
class DECLSPECIFIER test1 : public std::basic_string<char>
{
public:
test1(void);
~test1(void);
static std::ofstream stream;
};
主程序,testapp
#include <string>
#include <fstream>
#include "../testdll/test1.h"
#include "../testlib/test2.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::string str = "11111";
test1 str1;
str1.stream.put('1');
test2 str2;
str2.str = "2";
str2.static_str = "static";
return 0;
}
这个是静态库test2不使用CLR时,编译程序报的错误:
testlib.lib(test2.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in testdll.lib(testdll.dll)
2>testlib.lib(test2.obj) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator=(char const *)" (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z) already defined in testdll.lib(testdll.dll)
2>D:\Workspace\Projects\Solution1\Debug\testapp.exe : fatal error LNK1169: one or more multiply defined symbols found
求教怎么解决这个问题,这些在VS2005上编译是不存在问题的。
使用CLR会不会以来.NET库?