1

I have a workspace inside which there are two projects "Physx2D" and "SandBox". First one is built to a shared library and second into a console app. SandBox requires the Physx2D library and is set as start project.

I am using glfw3 library and glad for opengl functions.

The problem is that the project builds and runs successfully on windows but fails in linux. Going into more detail, the Physx2D library builds successfully. Issue came after compilation of SandBox. It fails to link and throws a warning and multiple link errors, all of them stating "undefined reference to "glad_******" in functions inside Physx2D library. e.g.

warning:

/usr/bin/ld: ../bin-int/Debug-linux-x86_64/SandBox/main.o warning: relocation against 'glad_glClearColor' in read-only section '.text._ZN7Physx2D6Window10FillScreenENS_5vec4IfEE[_ZN7Physx2D6Window10FillScreenENS_5tvec4IfEE]'

errors:

/usr/bin/ld: ../bin-int/Debug-linux-x86_64/SandBox/CA_gpu.o : in function 'Physx2D::Shader::use()':
..project_directory_of_sandbox ../Physx2D/src/renderer/shader.h:24: undefined reference to 'glad_glUseProgram'

multiple errors following same pattern are thrown in during linking.

I am using premake to generate project makefiles for gmake2.

Here is my premake script.

workspace "Physx2D"
    architecture "x64"

    configurations {Debug","Release"}

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

    project "Physx2D"
        location "Physx2D/"
        kind "SharedLib"
        language "C++"
        cppdialect "C++20"
        staticruntime "off"

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

        pchheader "pch.h"
        pchsource "%{prj.name}/src/pch.cpp"

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

        includedirs {
            "%{prj.name}/libraries/include",
            "%{prj.name}/src"
        }

        libdirs {"%{prj.name}/libraries/lib_%{cfg.system}"}

        filter "system:windows"
            systemversion "latest"
            links {"glfw3", "opengl32"}
            postbuildcommands {"{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/SandBox"}
            defines {"PHSX2D_BUILD_DLL","PHSX2D_PLATFORM_WINDOWS"}
        
        filter "system:linux"
            systemversion "latest"
            buildoptions { "-fvisibility=hidden"}
            links {"glfw3", "GL"}
            postbuildcommands {"{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/SandBox"}
            defines { "PHSX2D_BUILD_DLL", "PHSX2D_PLATFORM_LINUX"}

        filter "configurations:Debug"
            defines {"PHSX2D_DEBUG", "PHSX2D_ASSERT_ENABLE"}
            symbols "on"

        filter "configurations:Release"
            defines {"PHSX2D_RELEASE", "PHSX2D_ASSERT_ENABLE"}
            optimize "on"

        filter "files:**/libraries/src/**.*"
            flags { "NoPCH" }

    project "SandBox"
        location "SandBox"
        kind "ConsoleApp"
        language "C++"
        cppdialect "C++20"
        staticruntime "off"

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

        files {
            "%{prj.name}/src/**.*",
            "%{prj.name}/res/**.*",
            "%{prj.name}/applications/**.*"
        }

        includedirs {
            "Physx2D/libraries/include",
            "Physx2D/src"
        }

        filter "system:windows"
            systemversion "latest"
            links {"Physx2D"}
            defines {"PHSX2D_PLATFORM_WINDOWS"}
    
    filter "system:linux"
            systemversion "latest"
            links {"Physx2D"}
            defines {"PHSX2D_PLATFORM_LINUX"}
        
    filter "configurations:Debug"
            defines {"PHSX2D_DEBUG", "PHSX2D_ASSERT_ENABLE"}
            symbols "on"

    filter "configurations:Release"
            defines {"PHSX2D_RELEASE", "PHSX2D_ASSERT_ENABLE"}
            optimize "on"

I do not think that the problem has anything to do with the project setup. After a quick look at the problems and few searches later, I came to know the issue is that the definitions of functions declared in glad.h are not found during linking. I have glad.c inside the Physx2D project inside a folder which I have included in project files in premake script. Also, if it is the problem why it did not create any issues while building the Physx2D.

If it is not the problem then what is and how to solve it.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Seon Il
  • 43
  • 8
  • I don't like that dupe target. It doesn't help much there, if at all. – HolyBlackCat Jul 15 '23 at 09:25
  • 1
    The error message is very clear: "undefined reference". [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) explains how to solve such issues in general – Rabbid76 Jul 15 '23 at 09:30
  • 1
    @Rabbid76 That question is a bit of a pet peeve of mine. It's akin to using "list of C++ books" as a duplicate target. I kind of understand using it as a dupe target when the error is immediately obvious (failure to link a source file), but this one doesn't look obvious to me (unless I'm too sleepy to see it), since OP says it does work on Windows. *"many reasons to close a question like this"* If some debugging info is missing, I'd tell OP so they can provide it. – HolyBlackCat Jul 15 '23 at 09:33
  • @HolyBlackCat So we have a difference of opinion. In my opinion, this question is a duplicate and must be closed. In any case, this question must be closed because of the lack of information. Either it will lead to endless discussions in the comments until someone can answer it, or it will remain unanswered forever. [How long should we wait for a poster to clarify a question before closing?](https://meta.stackoverflow.com/questions/260263/how-long-should-we-wait-for-a-poster-to-clarify-a-question-before-closing) – Rabbid76 Jul 15 '23 at 09:38
  • I have two machines, windows 10 and debain linux - POP os. I ran this code in both machines. It without any errors worked on windows but for linux it keeps on throwing this error. If it gave this error in both machines then it would be different thing.. But thats not the case. There are no other errors. There is only one warning "relocation against 'glad_glClearColor' in read only section '.text.****' – Seon Il Jul 15 '23 at 10:09
  • You should edit this error message into the question itself. I would also try compiling a dummy shared library manually to see if it works, or reducing Glad to a [mcve] to pinpoint the problem. Googing this error gave me this: https://stackoverflow.com/q/65407610/2752075 – HolyBlackCat Jul 15 '23 at 10:13
  • okay i'l add it – Seon Il Jul 15 '23 at 10:18
  • @HolyBlackCat is there anything about order of compilation that I should be concerned about? – Seon Il Jul 16 '23 at 12:16
  • 1
    "is there anything about order of compilation" Yes there is, see [this](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) (which might be the real dupe). – n. m. could be an AI Jul 22 '23 at 06:30

0 Answers0