【问题标题】:Linking Error in C++ on visual studio 2013Visual Studio 2013 上的 C++ 中的链接错误
【发布时间】:2016-05-23 10:11:50
【问题描述】:

当我编译使用 xyz.lib 的项目“proj1”代码时出现以下错误(这是一个成功编译的不同项目)。

Error   3   error LNK2019: unresolved external symbol "int __cdecl Vsnprintf16(unsigned short *,unsigned int,unsigned short const *,char *)" (?Vsnprintf16@@YAHPAGIPBGPAD@Z) referenced in function "int __cdecl eastl::Vsnprintf(wchar_t *,unsigned int,wchar_t const *,char *)" (?Vsnprintf@eastl@@YAHPA_WIPB_WPAD@Z)  File : xyz.lib(abc.obj)

abc.cpp 调用了函数 sprintf 。

当我将 abc.h 和 abc.cpp 的所有代码移动到其他一些时,可以说 xyz 项目中已经存在的 def.h 和 def.cpp 文件,然后一切正常,没有链接错误。我不知道为什么。

我已经使用了 abc.cpp 中的 def.cpp 文件中使用的所有包含但同样的错误。

当我从 abc.cpp 中删除 sprintf() 调用时,一切正常。

如果有人能说明为什么会这样,请。谢谢

【问题讨论】:

  • 您的项目构建是否启用了 UNICODE?
  • 是的,我尝试了启用和禁用 unicode 宏,同样的错误。在项目设置字符集是“使用 Unicode 字符集”@marcinj
  • 看来你应该自己为这个函数添加定义

标签: c++ visual-studio-2013 linker libs


【解决方案1】:

我搜索了 msdn 以及 VS2015 和 VS2005 源代码文件夹,但没有找到 Vsnprintf16 的定义或声明。

我没用过eastl,但是这个函数好像你应该自己定义,你可以在下面的链接中找到例子:

https://github.com/BSVino/Digitanks/blob/master/common/eastl.cpp

https://github.com/electronicarts/EASTL/blob/master/test/source/main.cpp

作为参考,我将其包含在此处:

// EASTL also wants us to define this (see string.h line 197)
int Vsnprintf8(char* pDestination, size_t n, const char* pFormat, va_list arguments)
{
    #ifdef _MSC_VER
        return _vsnprintf(pDestination, n, pFormat, arguments);
    #else
        return vsnprintf(pDestination, n, pFormat, arguments);
    #endif
}

int Vsnprintf16(char16_t* pDestination, size_t n, const char16_t* pFormat, va_list arguments)
{
    #ifdef _MSC_VER
        return _vsnwprintf((wchar_t*)pDestination, n, (wchar_t*)pFormat, arguments);
    #else
        char* d = new char[n+1];
        int r = vsnprintf(d, n, convertstring<char16_t, char>(pFormat).c_str(), arguments);
        memcpy(pDestination, convertstring<char, char16_t>(d).c_str(), (n+1)*sizeof(char16_t));
        delete[] d;
        return r;
    #endif
}

【讨论】:

  • 但是 sprintf 仅用于该文件的大量文件中,只是它给出了链接错误。如果我将此文件的代码移动到另一个文件中,一切正常。代码正在获取您提到的函数的定义,但是对于 abc.cpp 文件,编译没有正确进行。为什么 ?这就是问题所在。我目前通过将 abc.cpp 文件的代码移动到另一个文件中来使代码工作。 @marcinj
  • 那么您的项目应用程序中有 Vsnprintf16 的定义吗?或者它可能出现在这个 xyz.lib 中?也许检查 abc.cpp 是否有任何与此 def.cpp 不同的单独配置/选项集。也许是一些特殊的预处理器定义。
  • 他没有提到他是如何构建eastl的,但他可能没有链接到eastl.cpp...我不太了解eastl。但是你做得很好,得到这个答案;这是一些研究工作。
  • abc.cpp 和 def.cpp 具有相同的属性。我检查了每个属性。 @marcinj
猜你喜欢
  • 2015-07-15
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
相关资源
最近更新 更多