C++ 具有不同字符集的项目内的链接错误
C++ Linking error within projects having different character set
我在VS 2005中编译C++项目解决方案时遇到链接错误。下面是场景。
我有一个解决方案,假设MySolution
其中有 2 个项目命名
MyTestLib
是一个静态库类型的项目,具有字符集Use Multi-Byte Character Set
且没有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 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);
成为
bool Class1::test1(const char * param);
因为LPCTSTR
是一个宏,它根据构建发生时的"字符集"配置进行设置。
现在,当您使用为 UNICODE 配置的"字符集"构建MyTestApp
时,它会将函数签名(我假设来自头文件)视为:
bool Class1::test1(wchar_t const * param);
因此,链接器没有希望将该函数链接到库中的实际内容。
解决方法是简单地不使用LPTCSTR
作为函数的类型 - 实现的函数中该参数的类型将始终const char*
,因此只需在函数声明中说明即可。
- CMake 项目链接静态库
- 尝试使用 gradle 构建指向静态库的 c++ 项目链接时找不到头文件
- 将错误GoogleMock与我在linux下的C++项目链接
- 将OpenBLAS与Qt Creator项目链接
- 从大型 github 项目链接共享库
- 将TBB库与QTCreator与CMAKE项目链接
- 如何与Libm.so和libdl.so与我的Bazel项目链接
- 将C++项目与 Visual Studio 中的窗体项目链接
- GTEST测试项目链接链接到其他可执行文件
- Visual Studio,将项目链接在一起并进行测试
- 如何在Xcode4上从头开始构建C++项目-链接现有和新的外部库
- 我怎么能说视觉工作室将库与我的项目链接起来
- 添加类后项目链接错误
- CMake:从外部项目链接共享C++对象会生成具有相对路径的二进制文件,而不是绝对路径
- Netbeans/C++:将2个项目链接在一起(可执行文件/动态库)
- 裸机目标与cmake项目链接错误
- 使用lua lib的Xcode c++项目链接错误
- 试图从另一个项目链接类时出现链接器错误
- 如何将code::块设置为在目标链接器选项之前链接项目链接器选项
- 无法将一个项目的头和 CPP 文件与另一个项目链接 LNK2019:未解决的外部符号错误