Search code examples
c++visual-studiolinker-errorsglfw

Problem with GLFW linking, as it used in SharedLib (DLL)


Environment : Visual Studio 2019.

I'm developing an Engine as a SharedLib (DLL) and I made an example that uses this DLL so I can launch the Engine. at this stage I have to add GLFW library to the engine, so I added GLFW as a submodule (git) to my project and built this library as a StaticLib (.lib) with static runtime sets to "On". and added GLFW as a reference in the engine as in the second picture below. the build of GLFW goes very well and I get GLFW.lib. so I added some functions of GLFW to the engine code like glfwMakeContextCurrent() at this point when I build the Engine I get weird linking errors for many functions except glfwInit().

here is a picture of an example of one of my linking errors: enter image description here

the project view:

enter image description here

Note: opengl32.lib already added to the engine in the Input in the Linker.

Please if you have any thoughts, I would appreciate it if you can share them

EDIT

Here is the full linker commandline:

enter image description here


Solution

  • Thank you Everyone for your help. I got relatively a solution and I want to share it with you in case someone fall in the same situation.

    Short Answer:

    I converted the GLFW from static library to shared library while the building, and the linking errors just gone.

    Long Answer

    I wrote a Premake script to generate the GLFW project with configuration set to Shared Library and staticruntime to Off [meaning (Multi-threaded DLL (/MD)) for Runtime Library in Visual Studio configuration]. beside some includes and defines (take a look at the script) and I linked the GLFW library to the engine and I copied the .dll to the location of Sandbox/example application based on the engine and everything works pretty fine.

    here is the script in case:

    project "GLFW"
    kind "SharedLib"
    language "C"
    
    targetname ("glfw3")
    targetdir ("bin/" .. outputdir .. "/%{prj.name}")
    objdir ("bin-intermediate/" .. outputdir .. "/%{prj.name}")
    
    files
    {
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
        "src/internal.h",
        "src/glfw_config.h",
        "src/mappings.h",
        "src/egl_context.h",
        "src/osmesa_context.h",
        "src/wgl_context.h",
        "src/context.h",
        "src/init.c",
        "src/input.c",
        "src/monitor.c",
        "src/vulkan.c",
        "src/window.c",
        "src/context.c"
    
    }
    
    includedirs {
        "%{prj.name}/src",
        "%{prj.name}/include"
    }
    
    filter "system:windows"
        buildoptions { "-std=c11", "-lgdi32" }
        systemversion "latest"
        staticruntime "Off"
        
    
        files
        {
            "src/win32_platform.h",
            "src/win32_joystick.h",
            "src/win32_init.c",
            "src/win32_joystick.c",
            "src/win32_monitor.c",
            "src/win32_time.c",
            "src/win32_thread.c",
            "src/win32_window.c",
            "src/wgl_context.c",
            "src/egl_context.c",
            "src/osmesa_context.c"
        }
    
        defines
        {
            "_GLFW_WIN32",
            "_CRT_SECURE_NO_WARNINGS",
            "WIN32",
            "_WINDOWS",
            "UNICODE",
            "_UNICODE",
            "_GLFW_BUILD_DLL"
        }
    
        links{
            "kernel32.lib",
            "user32.lib",
            "gdi32.lib",
            "winspool.lib",
            "shell32.lib",
            "ole32.lib",
            "oleaut32.lib",
            "uuid.lib",
            "comdlg32.lib",
            "advapi32.lib"
        }
    
        postbuildcommands{
            ("{COPY} %{cfg.buildtarget.relpath} ../../../bin/" .. outputdir .. "/Sandbox")
        }
        
    filter { "system:windows", "configurations:Debug"}
        buildoptions "/MDd"
    filter { "system:windows", "configurations:Release"}
        buildoptions "/MD"