尝试 link glfw 时 LLD 未定义符号

LLD undefined symbol when attempting to link glfw

我一直在尝试在我的 Windows 10 机器上安装 LLVM 工具链。我放弃了从源代码构建并安装了 MSYS2 mingw-w64-clang-x86_64-toolchain 包(clang 版本 13.0.0)。

我可以编译使用 C++ 标准库的简单代码。我正在使用 clang 编译,lld 到 link,我应该使用 libc++。

为了测试 link 另一个库,我正在使用 glfw:

#include <iostream>
#include <vector>
#include "glfw3.h"

int main(int argc, char **argv) {
    glfwInit();

    std::vector<int> testVector = {4, 5, 6, 7, 2};
    testVector.push_back(23);

    std::cout << testVector[1] << std::endl;


    return 0;

}

如果我注释掉 glfwInit();,编译并运行良好。行并使用此命令:

clang++  -Iinclude\ -Llib\ -lglfw3 -v .\main.cpp

看来 lld 正在寻找我放在 lib\ 目录中的 libglfw3.a 库文件。 但是如果 glfwInit();未注释,使用相同的命令我得到很多未解决的符号错误:

ld.lld: error: undefined symbol: __declspec(dllimport) CreateDIBSection  
>>> referenced by libglfw3.a(win32_window.c.obj):(createIcon)     
ld.lld: error: undefined symbol: __declspec(dllimport) CreateBitmap    
>>> referenced by libglfw3.a(win32_window.c.obj):(createIcon)   
ld.lld: error: undefined symbol: __declspec(dllimport) DeleteObject
>>> referenced by libglfw3.a(win32_window.c.obj):(createIcon)  
>>> referenced by libglfw3.a(win32_window.c.obj):(createIcon)   
>>> referenced by libglfw3.a(win32_window.c.obj):(updateFramebufferTransparency)  

..等等。

我使用 CMake、Ninja 和 Clang 从 glfw 源代码构建了 glfw 库。

这些错误引用的 win32_window.c.obj 文件和所有其他文件是 lib\ 目录中更深的几个目录,但我似乎无法 clang/lld 找到它们。

我在这里缺少什么参数?

编辑:我运行这个

clang++ -### -Iinclude\ -Llib\ -lglfw3 -v .\main.cpp 

得到这两行:

 "C:/msys64/clang64/bin/clang++.exe" "-cc1" "-triple" "x86_64-w64-windows-gnu" "-emit-obj" "-mrelax-all" "--mrelax-relocations" "-disable-free" 
    "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "main.cpp" "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=none" 
    "-fmath-errno" "-fno-rounding-math" "-mconstructor-aliases" "-mms-bitfields" "-munwind-tables" "-target-cpu" "x86-64" "-tune-cpu" "generic" 
    "-debugger-tuning=gdb" "-v" "-fcoverage-compilation-dir=C:\Users\gcvan\testProgram" "-resource-dir" "C:/msys64/clang64/lib/clang/13.0.0" 
    "-I" "include\" "-internal-isystem" "C:/msys64/clang64/x86_64-w64-mingw32/include/c++/v1" "-internal-isystem" "C:/msys64/clang64/include/c++/v1" 
    "-internal-isystem" "C:/msys64/clang64/lib/clang/13.0.0/include" "-internal-isystem" "C:/msys64/clang64/x86_64-w64-mingw32/include" 
    "-internal-isystem" "C:/msys64/clang64/include" "-fdeprecated-macro" "-fdebug-compilation-dir=C:\Users\gcvan\testProgram" "-ferror-limit" 
    "19" "-fmessage-length=120" "-fno-use-cxa-atexit" "-fgnuc-version=4.2.1" "-fcxx-exceptions" "-fexceptions" "-exception-model=seh" 
    "-fcolor-diagnostics" "-faddrsig" "-o" "C:/Users/gcvan/AppData/Local/Temp/main-c1d43f.o" "-x" "c++" ".\main.cpp"
 "C:/msys64/clang64/bin/ld.lld" "-m" "i386pep" "-Bdynamic" "-o" "a.exe" "C:/msys64/clang64/x86_64-w64-mingw32/lib/crt2.o" 
     "C:/msys64/clang64/x86_64-w64-mingw32/lib/crtbegin.o" "-Llib\" "-LC:/msys64/clang64/x86_64-w64-mingw32/lib" "-LC:/msys64/clang64/lib" 
     "-LC:/msys64/clang64/x86_64-w64-mingw32/sys-root/mingw/lib" "-LC:/msys64/clang64/lib/clang/13.0.0/lib/windows" "-LC:\cppLibraries" "-lglfw3" 
     "C:/Users/gcvan/AppData/Local/Temp/main-c1d43f.o" "-lc++" "-lmingw32" "C:/msys64/clang64/lib/clang/13.0.0/lib/windows/libclang_rt.builtins-x86_64.a" 
     "-lunwind" "-lmoldname" "-lmingwex" "-lmsvcrt" "-ladvapi32" "-lshell32" "-luser32" "-lkernel32" "-lmingw32" 
     "C:/msys64/clang64/lib/clang/13.0.0/lib/windows/libclang_rt.builtins-x86_64.a" "-lunwind" "-lmoldname" "-lmingwex" "-lmsvcrt" "-lkernel32" 
     "C:/msys64/clang64/x86_64-w64-mingw32/lib/crtend.o"

嗯,看来我还有很多关于命令行的知识要学compiling/linking。

我通过将 -lgdi32 添加到编译标签来修复它:

clang++ -Iinclude\ -Llib\ -lglfw3 -lgdi32 -v .\main.cpp

从这个帖子中得到灵感:https://github.com/ziglang/zig/issues/3319 来自线程,靠近底部,有一些好的建议:

When you see undefined symbol: __imp_CreateDCW the trick is to look up what DLL that is in. A duck duck go search lands us at https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdcw which says at the bottom it is in Gdi32.dll. So you need addSystemLibrary("Gdi32").

出于某种原因,我假设所有未定义的函数调用都来自 glfw,但它们不是,它们来自 GDI32 Win32 库。

只是想表明,对于像我这样经验水平的任何人,您可能应该确保 google 从您的错误中找到所有相关文本,并且不要对事物的来源做出假设..