gpt4 book ai didi

具有不同字符集的项目中的 C++ 链接错误

转载 作者:太空狗 更新时间:2023-10-29 23:53:35 26 4
gpt4 key购买 nike

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

我有一个解决方案可以说 MySolution 其中有 2 个项目命名

MyTestLib 是一个带有字符集的静态库类型项目使用多字节字符集 并且没有CLR 支持

MyTestApp 是一个 .exe 应用程序,使用上述库和字符集 Use Unicode Character Set 并支持 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 错误 C2664:“int TestLib::Class1::test1(int)”:无法将参数 1 从“const char *”转换为“int”

如果我将语句更改为 bool t = objcl2.test1((LPCTSTR)abc.c_str());//现在链接错误然后我得到

错误 2 error LNK2001: 未解析的外部符号 "public: bool __thiscall TestLib::Class1::test1(wchar_t const *)"(?test1@Class1@TestLib@@$$FQAE_NPB_W@Z) TestProject.obj

但是如果我将项目 MyTestApp Character Set 更改为 Use Multi-Byte Character Set 那么所有的错误都会得到解决。但是我无法更改项目字符集,因为还有其他依赖项。

附近有工作吗?

最佳答案

问题是,当您构建 MyTestLib 这个签名时:

bool Class1::test1(LPCTSTR param);

成为

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

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

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

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

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

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

关于具有不同字符集的项目中的 C++ 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com