glfwstatic-linkingpremake

Linking GLFW with a static library using Premake


I'm using Premake to manage the build configuration of my project, which consists of two static libraries ("Engine" and "Game") and an executable ("Launcher"). But when I attempt to integrate/link GLFW into my Engine library, I'm encountering linking errors.

I should mention I'm trying to link GLFW statically. I'm also using visual studio 2022 on a windows platform.

2>------ Build started: Project: Launcher, Configuration: Debug x64 ------
2>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
2>LINK : warning LNK4217: symbol 'strncmp' defined in 'libucrtd.lib(strncmp.obj)' is imported by 'Engine.lib(init.obj)' in function '_glfwParseUriList'
2>LINK : warning LNK4286: symbol 'strncmp' defined in 'libucrtd.lib(strncmp.obj)' is imported by 'Engine.lib(input.obj)'
2>LINK : warning LNK4286: symbol 'strncmp' defined in 'libucrtd.lib(strncmp.obj)' is imported by 'Engine.lib(context.obj)'
2>LINK : warning LNK4286: symbol 'strncmp' defined in 'libucrtd.lib(strncmp.obj)' is imported by 'Engine.lib(egl_context.obj)'
2>LINK : warning LNK4217: symbol 'free' defined in 'libucrtd.lib(free.obj)' is imported by 'Engine.lib(init.obj)' in function 'defaultDeallocate'
2>LINK : warning LNK4217: symbol 'free' defined in 'libucrtd.lib(free.obj)' is imported by 'Engine.lib(null_init.obj)' in function '_glfwConnectNull'
2>LINK : warning LNK4217: symbol 'malloc' defined in 'libucrtd.lib(malloc.obj)' is imported by 'Engine.lib(init.obj)' in function 'defaultAllocate'
2>LINK : warning LNK4217: symbol 'strtol' defined in 'libucrtd.lib(strtox.obj)' is imported by 'Engine.lib(init.obj)' in function '_glfwParseUriList'
2>LINK : warning LNK4217: symbol '__stdio_common_vsprintf' defined in 'libucrtd.lib(output.obj)' is imported by 'Engine.lib(init.obj)' in function '_glfwInputError'
2>LINK : warning LNK4217: symbol '__stdio_common_vsprintf' defined in 'libucrtd.lib(output.obj)' is imported by 'Engine.lib(win32_joystick.obj)' in function '_glfwDetectJoystickConnectionWin32'
2>LINK : warning LNK4217: symbol 'strcspn' defined in 'libucrtd.lib(strcspn.obj)' is imported by 'Engine.lib(input.obj)' in function 'glfwUpdateGamepadMappings'
2>LINK : warning LNK4217: symbol 'strtoul' defined in 'libucrtd.lib(strtox.obj)' is imported by 'Engine.lib(input.obj)' in function 'parseMapping'
2>LINK : warning LNK4217: symbol 'qsort' defined in 'libucrtd.lib(qsort.obj)' is imported by 'Engine.lib(monitor.obj)' in function 'refreshVideoModes'
2>LINK : warning LNK4217: symbol 'qsort' defined in 'libucrtd.lib(qsort.obj)' is imported by 'Engine.lib(win32_joystick.obj)' in function 'deviceCallback'
2>Engine.lib(init.obj) : error LNK2019: unresolved external symbol __imp_strtok referenced in function _glfwParseUriList
2>Engine.lib(init.obj) : error LNK2019: unresolved external symbol __imp_realloc referenced in function defaultReallocate
2>Engine.lib(window.obj) : error LNK2019: unresolved external symbol __imp_strncpy referenced in function glfwWindowHintString
2>Engine.lib(input.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>Engine.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>Engine.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>Engine.lib(input.obj) : error LNK2019: unresolved external symbol __imp_strspn referenced in function glfwUpdateGamepadMappings
2>Engine.lib(input.obj) : error LNK2019: unresolved external symbol __imp_fmaxf referenced in function glfwGetGamepadState
2>Engine.lib(input.obj) : error LNK2019: unresolved external symbol __imp_fminf referenced in function glfwGetGamepadState
2>Engine.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_fminf
2>Engine.lib(null_monitor.obj) : error LNK2001: unresolved external symbol __imp_fminf
2>Engine.lib(context.obj) : error LNK2019: unresolved external symbol __imp___stdio_common_vsscanf referenced in function sscanf
2>..\bin\Debug-windows-x86_64\Launcher\Launcher.exe : fatal error LNK1120: 7 unresolved externals
2>Done building project "Launcher.vcxproj" -- FAILED.

This is my current premake script (premake 5):

workspace "A_Seaside_Resort"
    configurations { "Debug", "Release" }
    architecture "x64"
    startproject "Launcher"

    outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}" 

project "Engine"
    location "Engine"
    kind "StaticLib"
    language "C++"
    cppdialect "C++20"
    staticruntime "on"

    targetdir ("bin/" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

    files { "%{prj.name}/Source/**.h", "%{prj.name}/Source/**.hpp", "%{prj.name}/Source/**.cpp" }

    defines { "GLEW_STATIC" }

    includedirs
    {
        "ThirdParty/GLFW/include"
    }

    libdirs
    {
        "ThirdParty/GLFW/lib-vc2022"
    }

    links
    {
        "glfw3",
        "opengl32",
        "gdi32",      -- For GLFW on Windows
        "user32",     -- For GLFW on Windows
        "kernel32",   -- For GLFW on Windows
        "ole32"       -- For GLFW on Windows
    }

    filter "system:windows"
        systemversion "latest"

    filter "configurations:Debug"
        runtime "Debug"
        symbols "on"

    filter "configurations:Release"
        runtime "Release"
        optimize "on"

project "Game"
    location "Game"
    kind "StaticLib"
    language "C++"
    cppdialect "C++20"
    staticruntime "On"

    --targetname ("%{prj.name}")
    targetdir ("bin/" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

    files { "%{prj.name}/Source/**.h", "%{prj.name}/Source/**.hpp", "%{prj.name}/Source/**.cpp" }

    includedirs
    {
        "Engine/Source"
    }

    links
    {
        "Engine"
    }

    filter "system:windows"
        systemversion "latest"
    
    filter "configurations:Debug"
        defines "Debug"
        symbols "on"

    filter "configurations:Release"
        defines "Release"
        optimize "on"

project "Launcher"
    location "Launcher"
    kind "ConsoleApp"
    language "C++"
    cppdialect "C++20"
    staticruntime "on"

    targetdir ("bin/" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

    files { "%{prj.name}/Source/**.h", "%{prj.name}/Source/**.hpp", "%{prj.name}/Source/**.cpp" }

    includedirs
    {       
        "Engine/Source",
        "Game/Source"
    }

    links
    {
        "Engine",
        "Game"
    }

    filter "system:windows"
        systemversion "latest"
    
    filter "configurations:Debug"
        defines "Debug"
        symbols "on"

    filter "configurations:Release"
        defines "Release"
        optimize "on"

I'm new to using Premake, so I'm not entirely sure what I'm doing wrong. I've noticed that some developers integrate GLFW directly as a project within their solution, but if possible I would like to avoid that since I'm planning on adding additional libraries in the future and want to avoid the bloat.


Solution

  • It seems that linking with the multi-threaded version of GLFW (glfw3_mt) instead of the standard version (glfw3) resolved the problem.

    -- Link against the multi-threaded version of GLFW
    links
    {
        "glfw3_mt"
    }