使用 premake 和 vs2019 与 glfw 链接时遇到问题

trouble linking with glfw using premake and vs2019

我正在尝试使用 premake 5 构建一个简单的项目。在 win10 上使用 visual studio 2019。 Premake 对我来说是新的,但我从简单开始:唯一的依赖项是 glm(headers 唯一的库)、GLAD 和 GLFW。 我将 GLAD 和 GLFW 作为子项目包含在我的 premake 文件中。 项目生成顺利。

glm 已正确包含并可用。

构建时: GLAD 和 GLFW 正确构建到各自的 .lib 文件 但是“核心”应用程序因这些链接器错误而失败:

3>GLFW.lib(init.obj) : error LNK2019: unresolved external symbol _glfwSelectPlatform referenced in function glfwInit
3>GLFW.lib(vulkan.obj) : error LNK2019: unresolved external symbol _glfwPlatformLoadModule referenced in function _glfwInitVulkan
3>GLFW.lib(vulkan.obj) : error LNK2019: unresolved external symbol _glfwPlatformFreeModule referenced in function _glfwInitVulkan
3>GLFW.lib(vulkan.obj) : error LNK2019: unresolved external symbol _glfwPlatformGetModuleSymbol referenced in function _glfwInitVulkan

我一定是在构建 glfw 时缺少配置选项

这是负责构建 GLFW 的 premake lua 脚本:

project "GLFW"
kind "StaticLib"
language "C"

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

files
{
    "include/GLFW/glfw3.h",
    "include/GLFW/glfw3native.h",
    "src/glfw_config.h",
    "src/context.c",
    "src/init.c",
    "src/input.c",
    "src/monitor.c",
    "src/vulkan.c",
    "src/window.c"
}
filter "system:linux"
    pic "On"

    systemversion "latest"
    staticruntime "On"

    files
    {
        "src/x11_init.c",
        "src/x11_monitor.c",
        "src/x11_window.c",
        "src/xkb_unicode.c",
        "src/posix_time.c",
        "src/posix_thread.c",
        "src/glx_context.c",
        "src/egl_context.c",
        "src/osmesa_context.c",
        "src/linux_joystick.c"
    }

    defines
    {
        "_GLFW_X11"
        
    }

filter "system:windows"
    systemversion "latest"
    staticruntime "On"
    
    -- buildoptions{
    --     "/MT"
    -- }

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

    }

filter "configurations:Debug"
    runtime "Debug"
    symbols "On"

filter "configurations:Release"
    runtime "Release"
    optimize "On"

感谢 'Botje' 评论,我意识到预制脚本中缺少一堆文件。 (我从另一个项目得到这个文件并错误地认为它是正确的)

我在查看 glfw 项目源目录中的 CMakeLists.txt 时发现了丢失的文件。

这是 glfw 项目的新 lua 预制脚本:

project "GLFW"
kind "StaticLib"
language "C"

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

files
{
    "include/GLFW/glfw3.h",
    "include/GLFW/glfw3native.h",
    "src/internal.h",
    "src/platform.h",
    "src/mappings.h",
    "src/context.c",
    "src/init.c",
    "src/input.c",
    "src/monitor.c",
    "src/platform.c",
    "src/vulkan.c",
    "src/window.c",
    "src/egl_context.c",
    "src/osmesa_context.c",
    "src/null_platform.h",
    "src/null_joystick.h",
    "src/null_init.c",

    "src/null_monitor.c",
    "src/null_window.c",
    "src/null_joystick.c",

}
filter "system:linux"
    pic "On"

    systemversion "latest"
    staticruntime "On"

    files
    {
        "src/x11_init.c",
        "src/x11_monitor.c",
        "src/x11_window.c",
        "src/xkb_unicode.c",
        "src/posix_time.c",
        "src/posix_thread.c",
        "src/glx_context.c",
        "src/egl_context.c",
        "src/osmesa_context.c",
        "src/linux_joystick.c"
    }

    defines
    {
        "_GLFW_X11"
        
    }

filter "system:windows"
    systemversion "latest"
    staticruntime "On"
    
    -- buildoptions{
    --     "/MT"
    -- }

    files
    {
        "src/win32_init.c",
        "src/win32_module.c",
        "src/win32_joystick.c",
        "src/win32_monitor.c",
        "src/win32_time.h",
        "src/win32_time.c",
        "src/win32_thread.h",
        "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"

    }

filter "configurations:Debug"
    runtime "Debug"
    symbols "On"

filter "configurations:Release"
    runtime "Release"
    optimize "On"