Premake linkingLinking into SharedLib 不工作

Premake linkingLinking into SharedLib not working

我目前正在尝试学习 C++,并认为做一些 OpenGL 魔术是个好主意。 我看到了一个 Premake5 教程并继续学习,除了我尝试自己链接一个库(GLFW)。 为 Visual Studio 2017 生成项目文件时,我指定的库不知为何没有正确链接到项目中。 我收到很多 LINK 错误,例如:

glfw3.lib(monitor.c.obj) : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp__strdup" in Funktion "_glfwAllocMonitor".

glfw3.lib(win32_window.c.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp__strdup".

...

这是我的 premake5.lua 文件的样子:

workspace "MojoEngine"
architecture "x64"

configurations
{
    "Debug",
    "Release",
    "Dist"
}

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

project "MojoEngine"
location "MojoEngine"
kind "SharedLib"
language "C++"

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

librarydir = "%{prj.name}/libraries/"

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

includedirs
{
    librarydir .. "GLFW/include",
    "%{prj.name}/vendor/spdlog/include"
}

libdirs
{
    librarydir .. "GLFW/lib"
}

links
{
    "glfw3",
    "glfw3dll"
}

filter "system:windows"
    cppdialect "C++17"
    staticruntime "On"
    systemversion "latest"

    defines
    {
        "ME_PLATFORM_WINDOWS",
        "ME_BUILD_DLL"
    }

    postbuildcommands
    {
        ("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. 
    "/Sandbox"),
        ("{COPY} %{prj.name}/lib/GLFW/glfw3.dll ../bin/" .. outputdir .. 
    "/Sandbox")
    }

filter "configurations:Debug"
    defines "ME_DEBUG"
    symbols "On"

filter "configurations:Release"
    defines "ME_RELEASE"
    optimize "On"

filter "configurations:Dist"
    defines "ME_DIST"
    optimize "On"

project "Sandbox"
location "Sandbox"
kind "ConsoleApp"
language "C++"

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

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

includedirs
{
    "MojoEngine/vendor/spdlog/include",
    "MojoEngine/src"
}

links
{
    "MojoEngine"
}

filter "system:windows"
    cppdialect "C++17"
    staticruntime "On"
    systemversion "latest"

    defines
    {
        "ME_PLATFORM_WINDOWS",
    }

filter "configurations:Debug"
    defines "ME_DEBUG"
    symbols "On"

filter "configurations:Release"
    defines "ME_RELEASE"
    optimize "On"

filter "configurations:Dist"
    defines "ME_DIST"
    optimize "On"

查看 glfw 文档。那里说你必须 link 一些 windows 特定的库用于不同的编译器版本。

GLFW Documentation:

When linking a program under Windows that uses the static version of GLFW, you must link with opengl32. On some versions of MinGW, you must also explicitly link with gdi32, while other versions of MinGW include it in the set of default libraries along with other dependencies like user32 and kernel32. If you are using GLU, you must also link with glu32.

如果您使用 MinGW,您可能需要 link gdi32 到您的“MojoEngine”项目。

注意:你是动态的linking,所以你需要link opengl32。抱歉看错了。