Unable to link my own static libraries

2019-07-19 17:26发布

问题:

I am trying to add a sound library to a GLFW / OpenGL project in windows. I have set the sound project to output a static library (.lib). It compiles fine.

In my main project, I add a reference to Sound and a dependency on Sound (to change the build order). I include "..\Sound\sound.h" in my main.cpp and Intellisense is happy with everything. All compiles well. However, the linker is pissed:

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

I have NO IDEA what to make of this. Lnk2038 is supposed to mean a problem between debug and release, but everything is in debug mode. The lnk2019 should be fine against Sound.lib?

Much obliged!

回答1:

Everything fine with your library (except you mixing debug and release runtime). You'll have to link a debug build when building a program in debug mode. Same is true for release builds. That's for error LNK2038 and warning LNK4098.

The other errors are caused by the simple fact that you can't link a static library with a static library (here: OpenAL), because that would get extra complicated if two static libraries would refer to the same static code (essentially leading to multiple definitions).

What you have to do for these errors to be solved is simple: Link your resulting executable file to your own Sound.lib as well as the right OpenAL library file and everything should be fine.