2

在 VS 2005 中编译我的 C++ 项目解决方案时出现链接错误。以下是场景。

我有一个解决方案,可以说MySolution其中有 2 个名为

MyTestLib是具有字符集且不支持的静态库Use Multi-Byte Character Set类型CLR项目

MyTestAppUse Unicode Character Set是一个 .exe 应用程序,使用带有字符集和CLR支持的上述库

其中MyTestLib有两个具有以下定义的重载函数

int Class1::test1(int a)
{
    return a+4; 
}

bool Class1::test1(LPCTSTR param)
{
    return true;
}

MyTestApp正在从它的代码中调用它们,例如

Class1 objcl1;
int a = objcl1.test1(12); //Works fine

Class1 objcl2;
string abc = "adsad";
bool t = objcl2.test1(abc.c_str()); //Error

调用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 Then 我得到

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那么所有错误都将得到解决。但我无法更改项目字符集,因为还有其他依赖项。

周围有工作吗?

4

1 回答 1

8

问题是当您构建MyTestLib此签名时:

bool Class1::test1(LPCTSTR param);

变成

bool Class1::test1(const char * param);

因为LPCTSTR是一个宏,根据构建发生时的“字符集”配置进行设置。

现在,当您MyTestApp使用为 UNICODE 配置的“字符集”构建时,它会将该函数签名(我假设来自头文件)视为:

bool Class1::test1(wchar_t const * param);

所以链接器没有希望将该函数链接到库中的实际内容。

解决方法是简单地不用LPTCSTR作函数的类型 - 已实现函数中该参数的类型将始终为const char*,因此只需在函数声明中说明即可。

于 2012-04-20T08:03:48.957 回答