简体   繁体   English

在linux上预制未定义的引用错误

[英]Premake undefined reference error on linux

Unfortunately I'm having trouble with GLFW on Ubuntu Linux, yet on Windows it works perfectly.不幸的是,我在 Ubuntu Linux 上遇到了 GLFW 问题,但在 Windows 上它运行良好。

I'm running premak5 gmake and make(this is where I get the compiler error).我正在运行 premak5 gmake 和 make(这是我得到编译器错误的地方)。

This is error i am getting:这是我得到的错误:

Creating ../bin-int/Debug-linux-x86_64/UiApp
UiApp.cpp
Linking UiApp
    ../bin/Debug-linux-x86_64/UiEngine/libUiEngine.so: undefined reference to `_glfwPlatformLoadModule'
    ../bin/Debug-linux-x86_64/UiEngine/libUiEngine.so: undefined reference to `_glfwSelectPlatform'
    ../bin/Debug-linux-x86_64/UiEngine/libUiEngine.so: undefined reference to `_glfwPlatformFreeModule'
    ../bin/Debug-linux-x86_64/UiEngine/libUiEngine.so: undefined reference to `_glfwPlatformGetModuleSymbol'

I think it is related to linker error but i couldn't find any solid solution for this.我认为这与链接器错误有关,但我找不到任何可靠的解决方案。 I fixed other referance errors like pthread and dl by adding them to premake file.我通过将 pthread 和 dl 等其他引用错误添加到 premake 文件来修复它们。

My file structure:我的文件结构:

  • UIEngine(SharedLib) UIEngine(共享库)
    • GLFW GLFW
    • ImGui网贵
  • UiApp(ConsoleApp) UiApp(控制台应用程序)

Here is my premake files:这是我的预制文件:

Main Premake:主要预制:

workspace "UiModule"
    architecture "x86_64"
    startproject "UiApp"

    configurations
    {
        "Debug",
        "Release"
    }

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

IncludeDir = {}
IncludeDir["GLFW"] = "vendor/GLFW/include"
IncludeDir["imgui"] = "vendor/imgui"

include "UiEngine/vendor/GLFW"
include "UiEngine/vendor/imgui"

include "UiEngine"
include "UiApp"

UiEngine Premake: UiEngine Premake:

project "UiEngine"
    kind "SharedLib"
    language "C++"
    cppdialect "C++17"
    staticruntime "off"

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

    files
    {
        "src/**.h",
        "src/**.cpp",
    }

    defines
    {
        "_CRT_SECURE_NO_WARNINGS",
        "GLFW_INCLUDE_NONE"
    }

    includedirs
    {
        "src",
        "%{IncludeDir.GLFW}",
        "%{IncludeDir.imgui}"
    }

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

        links
        {
            "GLFW",
            "imgui",
            "opengl32.lib"
        }

        defines
        {
            "UI_PLATFORM_WINDOWS",
            "UI_BUILD_DLL"
        }

        postbuildcommands
        {
            ("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/UiApp")
        }
    
        
    filter "system:linux"
        cppdialect "C++17"
        staticruntime "On"
        systemversion "latest"

        links 
        {
            "GLFW",
            "imgui",
        }

        defines 
        {
            "_X11" 
        }
    
    filter "configurations:Debug"
        defines "HZ_DEBUG"
        runtime "Debug"
        symbols "on"

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

GLFW Premake: GLFW 预制:

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

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

includedirs { "include/" }

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"

UiApp Premake: UiApp 预制:

project "UiApp"
    kind "ConsoleApp"
    language "C++"

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

    files
    {
        "src/**.h",
        "src/**.cpp"
    }

    includedirs
    {
        "%{wks.location}/UiEngine/src",
        "%{IncludeDir.imgui}",
        "%{wks.location}/UiEngine/vendor/GLFW/include"
    }
    
    filter "system:linux"
        cppdialect "C++17"
        staticruntime "On"
        systemversion "latest"

        links
        {
            "dl",
            "pthread",
            "GLFW",
            "imgui",
            "UiEngine"
        }
    
        defines 
        {
            "_X11" 
        }

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

        defines
        {
            "UI_PLATFORM_WINDOWS"
        }

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

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

The problem is caused by missing files in the GLFW Premake.lua file.该问题是由 GLFW Premake.lua 文件中的文件丢失引起的。 Compiler can't find modules because you did not include it.编译器找不到模块,因为您没有包含它。

These modules defined in glfw/src/posix_module.c这些模块定义在 glfw/src/posix_module.c

`_glfwPlatformLoadModule'
`_glfwPlatformGetModuleSymbol'
`_glfwPlatformFreeModule'

And this module is related to glfw/src/internal.h and glfw/src/platform.h files并且这个模块与 glfw/src/internal.h 和 glfw/src/platform.h 文件有关

`_glfwSelectPlatform'

After adding these files in your glfw's premake file under the linux filter, you are goot to go.在 linux 过滤器下将这些文件添加到 glfw 的 premake 文件中后,您就可以开始了。

Final premake file will look like this;最终的预制文件将如下所示;

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_module.c", // Here
        "src/posix_thread.c",
        "src/glx_context.c",
        "src/egl_context.c",
        "src/osmesa_context.c",
    }

Also i am leaving related question here for other people.我也将相关问题留给其他人。

For other missing libraries, you can check glfw github repo.对于其他缺少的库,您可以查看 glfw github repo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM