无法链接我自己的静态库

Unable to link my own static libraries

本文关键字:静态 自己的 我自己 链接      更新时间:2023-10-16

我正试图在windows中为GLFW/ONGL项目添加一个声音库。我已经将声音项目设置为输出静态库(.lib)。它编译得很好。

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

1>Link:
1>  LINK : ###ProjectsDeathRaceDebugDeathRace.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>###ProjectsDeathRaceDebugDeathRace.exe : fatal error LNK1120: 17 unresolved externals

我不知道该怎么办。Lnk2038应该意味着调试和发布之间的问题,但一切都处于调试模式。lnk2019对Sound.lib应该没问题吗?

非常感谢!

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

其他错误是由一个简单的事实引起的,即您不能将静态库与静态库(此处:OpenAL)链接,因为如果两个静态库引用同一静态代码(本质上导致多个定义),这将变得格外复杂。

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