c++windowsclangglfwlld

LLD undefined symbol when attempting to link glfw


I've been trying to get an LLVM toolchain setup on my Windows 10 machine. I gave up on building from source and have the MSYS2 mingw-w64-clang-x86_64-toolchain package installed (clang version 13.0.0).

I can compile simple code that uses the C++ standard library. I'm using clang to compile, lld to link, and I should be using libc++.

To test linking an additional library, I'm using 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;

}

This compiles and runs fine if I comment out the glfwInit(); line and use this command :

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

So it seems lld is finding the libglfw3.a library file I placed in the lib\ directory. But if glfwInit(); is uncommented, with the same command I get a lot of unresolved symbol errors:

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)  

..and so on.

I built the glfw library from the glfw source code using CMake, Ninja, and Clang.

The win32_window.c.obj file and all others referenced by these errors are a couple directories deeper in the lib\ directory, but I can't seem to get clang/lld to find them.

What argument am I missing here?

Edit: I ran this

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

And got these two lines:

 "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"

Solution

  • Well, it seems I have a lot to learn about command line compiling/linking.

    I fixed it by adding -lgdi32 to the compile tags:

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

    Got the idea from this thread: https://github.com/ziglang/zig/issues/3319 From the thread, near the bottom, there is some good advice:

    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://learn.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").

    For some reason I assumed all the undefined function calls were from glfw, but they aren't, they are from the GDI32 Win32 library.

    Just goes to show, for anyone at my experience level, you should probably make sure to google ALL relevant text from your errors and don't make assumptions about the provenance of things..