c++linuxembedded-linuxglfwpremake

Premake undefined reference error on linux


Unfortunately I'm having trouble with GLFW on Ubuntu Linux, yet on Windows it works perfectly.

I'm running premak5 gmake and make(this is where I get the compiler error).

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.

My file structure:

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:

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:

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:

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"

Solution

  • The problem is caused by missing files in the GLFW Premake.lua file. Compiler can't find modules because you did not include it.

    These modules defined in glfw/src/posix_module.c

    `_glfwPlatformLoadModule'
    `_glfwPlatformGetModuleSymbol'
    `_glfwPlatformFreeModule'
    

    And this module is related to glfw/src/internal.h and glfw/src/platform.h files

    `_glfwSelectPlatform'
    

    After adding these files in your glfw's premake file under the linux filter, you are goot to go.

    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.