我有一个已编译的raylib静态库:raylib.lib。我自己用Librarian > General > Additional Dependencies 编译了这个库:winmm.lib
这个库有一个 C 函数,如下所示:
void CloseWindow(void);
而且我还有我非常基本的使用 raylib 的 C++ 项目:
// main.cpp
#include "raylib.h"
int main()
{
InitWindow(100, 100, "");
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}
现在,当我使用Linker > Additional Dependencies编译我的项目时,设置为:
- raylib.lib;用户32.lib;shell32.lib;gdi32.lib
它编译和链接没有任何警告。
但是当我交换 raylib.lib 和 user32.lib 的顺序时:
- 用户32.lib;raylib.lib;shell32.lib;gdi32.lib
然后我得到一个链接器错误:
Link:
D:\Visual Studio Community 2019\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\link.exe
/ERRORREPORT:QUEUE /OUT:"bin\MyProject.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:bin
user32.lib raylib.lib shell32.lib gdi32.lib winmm.lib /MANIFEST
/MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG:FULL
/PDB:"bin\MyProject.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /TLBID:1 /DYNAMICBASE
/NXCOMPAT /IMPLIB:"bin\MyProject.lib" /MACHINE:X64 obj\Release\MyProject\main.obj
raylib.lib(core.obj) : error LNK2005: CloseWindow already defined in user32.lib(USER32.dll) [D:\example\MyProject.vcxproj]
bin\MyProject.exe : fatal error LNK1169: one or more multiply defined symbols found [D:\example\MyProject.vcxproj]
那么这怎么可能已经定义了呢?为什么链接顺序很重要?
更新
当我使用Librarian > General > Additional Dependencies 编译 raylib 时:winmm.lib; user32.lib然后我收到此警告(编译库时):
Lib:
D:\Visual Studio Community 2019\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\Lib.exe /OUT:"bin\raylib.lib" winmm.lib user32.lib /NOLOGO /MACHINE:X64 obj\Release\raylib\core.obj
obj\Release\raylib\models.obj
obj\Release\raylib\raudio.obj
obj\Release\raylib\rglfw.obj
obj\Release\raylib\shapes.obj
obj\Release\raylib\text.obj
obj\Release\raylib\textures.obj
obj\Release\raylib\utils.obj
user32.lib(USER32.dll) : warning LNK4006: __NULL_IMPORT_DESCRIPTOR already defined in winmm.lib(WINMM.dll); second definition ignored [D:\example\raylib.vcxproj]
raudio.obj : warning LNK4006: PlaySound already defined in winmm.lib(WINMM.dll); second definition ignored [D:\example\raylib.vcxproj]
core.obj : warning LNK4006: CloseWindow already defined in user32.lib(USER32.dll); second definition ignored [D:\example\raylib.vcxproj]
core.obj : warning LNK4006: ShowCursor already defined in user32.lib(USER32.dll); second definition ignored [D:\example\raylib.vcxproj]
以这种方式编译 raylib MyProject 不需要与user32.lib链接,它可以工作。
在这种情况下使用 raylib 的最佳方法是什么?我是否应该始终使用它的依赖项(user32.lib等)编译 raylib 并获得这些警告,或者我应该稍后在编译 MyProject 时链接它们(然后我需要确保 raylib 在它的依赖项之前链接)?