c++pthreadsglfwpremake

Undefined reference using GLFW static in a shared library


I'm building a project with two major components: Game and Engine. Engine is a shared library calling most of the graphical call. That's why it's supposed to include GLFW. Then Game must include Engine. Here is the filesystem schema:

enter image description here

To build everything, i'm using premake5. Don't bother the premake folder, it's for storage purpose. Here is the premake5.lua on top of my folder:

workspace "Game"
    architecture "x64"

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

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

-- Include directories relative to root folder (solution)

include "Engine/GLFW"

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

    targetdir ("bin/" .. outputdir .. "/")
    objdir ("bin-int/" .. outputdir .. "/")

    pchheader "Egpch.h"
    pchsource "Egpch.cpp"

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

    includedirs
    {
        "%{prj.name}/",
        "%{prj.name}/src",
        "%{prj.name}/spdlog/include",
        "Engine/GLFW/include"
    }

    links
    {
        "GLFW"
    }

    filter "system:linux"
        cppdialect "C++17"
        staticruntime "On"
        links { 
            "GL"
        }

    filter "system:windows"
        cppdialect "C++17"
        staticruntime "On"
        systemversion "latest"
        links { "OpenGL32" }
        defines
        {
            "EG_PLATFORM_WINDOWS",
            "EG_BUILD_DLL";
        }


    filter "configurations:Debug"
        defines "EG_DEBUG"
        symbols "On"
    
    filter "configurations:Release"
        defines "EG_RELEASE"
        optimize "On"

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

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

    targetdir ("bin/" .. outputdir .. "/")
    objdir ("bin-int/" .. outputdir .. "/")

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

    includedirs
    {
        "Engine/spdlog/include",
        "Engine/",
        "Engine/src",
    }

    links
    {
        "Engine"
    }

    filter "system:linux"
        cppdialect "C++17"
        staticruntime "On"

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

        defines
        {
            "EG_PLATFORM_WINDOWS",
        }

    filter "configurations:Debug"
        defines "EG_DEBUG"
        symbols "On"
    
    filter "configurations:Release"
        defines "EG_RELEASE"
        optimize "On"

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

And i've also a premake file in my GLFW folder:

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:windows"
        systemversion "latest"
        staticruntime "On"

        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 "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 "configurations:Debug"
        runtime "Debug"
        symbols "On"
    
    filter "configurations:Release"
        runtime "Release"
        optimize "on"

And i can't find why i have these undefined reference when linking Game:

==== Building GLFW (debug) ====
==== Building Engine (debug) ====
Linking Engine
==== Building Game (debug) ====
Linking Game
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformLoadModule'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_key_create'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_getspecific'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwSelectPlatform'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_key_delete'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformFreeModule'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformGetModuleSymbol'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:91: ../bin/Debug-linux-x86_64/Game] Error 1
make: *** [Makefile:51: Game] Error 2

If someone has an idea?

---EDIT:--- So i found that i made a dumb mistake in the premake inside my GLFW folder. Forget some necessary files which explain the undefined reference of all _glfw call. I've still the undefined reference for the pthread library. I'm looking to it.


Solution

  • I find thanks to kiner_shah hint that the problem come from the library. In fact, i'm using GLFW as a static library, so all the dependent library doesn't come with it. I needed to add these libraries for linux:

    links { "glfw", "Xrandr", "Xi", "GLU", "GL", "X11", "dl", "pthread", "stdc++fs", }