【问题标题】:Unable to link my own static libraries无法链接我自己的静态库
【发布时间】:2012-01-28 09:32:18
【问题描述】:

我正在尝试将声音库添加到 Windows 中的 GLFW / OpenGL 项目。我已将声音项目设置为输出静态库 (.lib)。它编译得很好。

在我的主项目中,我添加了对 Sound 的引用和对 Sound 的依赖(以更改构建顺序)。我在 main.cpp 中包含“..\Sound\sound.h”,Intellisense 对一切都很满意。一切都编译得很好。但是,链接器很生气:

1>Link:
1>  LINK : ###\Projects\DeathRace\Debug\DeathRace.exe not found or not built by the last incremental link; performing full link
1>Sound.lib(sound.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in base_objects.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alListenerfv referenced in function "public: bool __thiscall SoundAPI::Startup(void)" (?Startup@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alListener3f referenced in function "public: bool __thiscall SoundAPI::Startup(void)" (?Startup@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alGetError referenced in function "public: bool __thiscall SoundAPI::Startup(void)" (?Startup@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alutGetError referenced in function "public: bool __thiscall SoundAPI::Startup(void)" (?Startup@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alutInit referenced in function "public: bool __thiscall SoundAPI::Startup(void)" (?Startup@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alutExit referenced in function "public: bool __thiscall SoundAPI::Shutdown(void)" (?Shutdown@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alDeleteBuffers referenced in function "public: bool __thiscall SoundAPI::Shutdown(void)" (?Shutdown@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alDeleteSources referenced in function "public: bool __thiscall SoundAPI::Shutdown(void)" (?Shutdown@SoundAPI@@QAE_NXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alutCreateBufferFromFile referenced in function "public: unsigned int * __thiscall SoundAPI::LoadSound(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?LoadSound@SoundAPI@@QAEPAIV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourcei referenced in function "public: unsigned int * __thiscall SoundAPI::CreateSource(void)" (?CreateSource@SoundAPI@@QAEPAIXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSource3f referenced in function "public: unsigned int * __thiscall SoundAPI::CreateSource(void)" (?CreateSource@SoundAPI@@QAEPAIXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourcef referenced in function "public: unsigned int * __thiscall SoundAPI::CreateSource(void)" (?CreateSource@SoundAPI@@QAEPAIXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alGenSources referenced in function "public: unsigned int * __thiscall SoundAPI::CreateSource(void)" (?CreateSource@SoundAPI@@QAEPAIXZ)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourceQueueBuffers referenced in function "public: bool __thiscall SoundAPI::Queue(unsigned int *,unsigned int *)" (?Queue@SoundAPI@@QAE_NPAI0@Z)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourcePlay referenced in function "public: bool __thiscall SoundAPI::Play(unsigned int *)" (?Play@SoundAPI@@QAE_NPAI@Z)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourcePause referenced in function "public: bool __thiscall SoundAPI::Pause(unsigned int *)" (?Pause@SoundAPI@@QAE_NPAI@Z)
1>Sound.lib(sound.obj) : error LNK2019: unresolved external symbol __imp__alSourceStop referenced in function "public: bool __thiscall SoundAPI::Stop(unsigned int *)" (?Stop@SoundAPI@@QAE_NPAI@Z)
1>###\Projects\DeathRace\Debug\DeathRace.exe : fatal error LNK1120: 17 unresolved externals

我不知道该怎么做。 Lnk2038应该是debug和release之间的问题,但一切都是debug模式。 lnk2019 对 Sound.lib 应该没问题吧?

非常感谢!

【问题讨论】:

  • base_objects.obj (无论来自哪里)似乎是编译版本。
  • base_objects.h 和 .cpp 定义了许多抽象数据类,例如 Object、PhysicsObject 和 StaticObject。它们的属性与其他许多代码文件中的任何一个都没有区别,我能说得最好。什么会导致这种情况(以及如何撤消)?
  • 没有看到实际项目就没有简单的方法来告诉调试/发布问题,但是您的大多数链接错误是由于没有正确链接到 OpenAL(您的声音库似乎使用它,但不是包括它)
  • 链接器错误已修复 (d'oh),但无法弄清楚 .obj 为何处于发布模式。我可以上传任何特定文件或 sn-p 以提供帮助吗?解决方案是巨大的

标签: c++ windows visual-studio-2010 linker


【解决方案1】:

你的库一切正常(除了你混合调试和发布运行时)。在调试模式下构建程序时,您必须链接调试版本。发布版本也是如此。这是针对错误LNK2038 和警告LNK4098

其他错误是由于您无法将静态库与静态库(此处为:OpenAL)链接这一简单事实引起的,因为如果两个静态库引用相同的静态代码(本质上是导致多个定义)。

要解决这些错误,您必须做的事情很简单:将生成的可执行文件链接到您自己的 Sound.lib 以及正确的 OpenAL 库文件,一切都会好起来的。

【讨论】:

  • 酷,谢谢!对于那个 .obj 文件,我如何从发布更改为调试?我处于“调试”模式,所有属性页中的所有内容似乎都设置为“调试”。不过,可能确实有些问题,因为 base_objects 是第一个文件,按字母顺序排列。 “参考”标签不应该为我处理链接吗?它有用于在对话框中链接依赖项和输入的设置?
  • 刚刚意识到这是一个多么愚蠢的问题。为 alut.lib 和 openal32.lib 添加了链接器输入,并且 lnk2019 消失了。德普。但是,仍然无法弄清楚为什么我的 .obj 处于发布模式
  • 将我的运行时库从 /MD 更改为 /MDd。成功了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
相关资源
最近更新 更多