使用 Premake 将 GLFW 与静态库链接

问题描述 投票:0回答:1

我使用 Premake 来管理项目的构建配置,该项目由两个静态库(“Engine”和“Game”)和一个可执行文件(“Launcher”)组成。但是当我尝试将 GLFW 集成/链接到我的引擎库时,我遇到了链接错误。

我应该提到我正在尝试静态链接 GLFW。我还在 Windows 平台上使用 Visual Studio 2022。

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.

这是我当前的预制脚本(预制 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"

我刚开始使用 Premake,所以我不完全确定我做错了什么。我注意到一些开发人员将 GLFW 直接作为项目集成到他们的解决方案中,但如果可能的话,我想避免这种情况,因为我计划在将来添加其他库并希望避免膨胀。

glfw static-linking premake
1个回答
0
投票

看来链接多线程版本的 GLFW (glfw3_mt) 而不是标准版本 (glfw3) 解决了问题。

-- Link against the multi-threaded version of GLFW
links
{
    "glfw3_mt"
}
© www.soinside.com 2019 - 2024. All rights reserved.