Muh*_*mar 2 c++ exe character-encoding
我在VS 2005中编译我的C++项目解决方案时遇到链接错误.以下是该场景.
我有一个解决方案,让我们说MySolution有2个项目名称
MyTestLib是一个静态库类型的项目,具有字符集Use Multi-Byte Character Set,没有CLR支持
和
MyTestApp是一个.exe应用程序使用上面的lib与字符集Use Unicode Character Set和CLR支持
在MyTestLib没有与下面的定义两个重载函数
int Class1::test1(int a)
{
return a+4;
}
bool Class1::test1(LPCTSTR param)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
MyTestApp 从它的代码中调用它们
Class1 objcl1;
int a = objcl1.test1(12); //Works fine
Class1 objcl2;
string abc = "adsad";
bool t = objcl2.test1(abc.c_str()); //Error
Run Code Online (Sandbox Code Playgroud)
调用test1(LPCTSTR)版本会出错
Error 1 error C2664: 'int TestLib::Class1::test1(int)' : cannot convert parameter 1 from 'const char *' to 'int'
如果我改变声明bool t = objcl2.test1((LPCTSTR)abc.c_str()); //Now linking Error
然后我得到
Error 2 error LNK2001: unresolved external symbol "public: bool __thiscall TestLib::Class1::test1(wchar_t const *)" (?test1@Class1@TestLib@@$$FQAE_NPB_W@Z) TestProject.obj
但如果我将项目MyTestApp字符集更改为Use Multi-Byte Character Set然后所有错误都已解决.但我无法更改项目字符集,因为还有其他依赖项.
有什么工作吗?
问题是,当您构建MyTestLib此签名时:
bool Class1::test1(LPCTSTR param);
Run Code Online (Sandbox Code Playgroud)
变
bool Class1::test1(const char * param);
Run Code Online (Sandbox Code Playgroud)
因为LPCTSTR是在构建发生时根据"字符集"配置设置的宏.
现在,当您MyTestApp使用为UNICODE配置的"字符集" 构建时,它会看到函数签名(从头文件中,我假设):
bool Class1::test1(wchar_t const * param);
Run Code Online (Sandbox Code Playgroud)
所以链接器没有希望将该函数链接到库中的实际内容.
解决方法是简单地不使用LPTCSTR函数的类型 - 实现函数中该参数的类型将始终如此const char*,所以在函数声明中这样说.
| 归档时间: |
|
| 查看次数: |
220 次 |
| 最近记录: |