Search code examples
c++buildpremake

Including ImGui, GLFW, and GLEW in Premake5


I am writing a C++ program using premake5 as my build system and I cannot understand how to link these libraries together. I want to use ImGui using GLFW and GLEW, but as I port my program over from cmake to premake, I find myself having trouble getting the libraries to link. I am getting LNK2019 and LNK2001 errors for the opengl functions I am using in my program. I know the problem is that the definitions for these functions are not being linked to my program. But as far as I understand premake, I should be doing just that.

Following the example of other premake projects I have found on GitHub, I am making individual projects for each of my dependencies. These projects are forked from the version of the library I am using and added to my git repo via submodules. The premake files for each of the dependencies are as follows:

premake5.lua (GLEW)

project "GLEW"
    kind "StaticLib"
    language "C"

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

    files { "include/GL/*.h", "premake5.lua"}
    
    filter "configurations:Debug"
        runtime "Debug"
        symbols "on"

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

premake5.lua (GLFW)

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

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

    files { "include/**.h" }


    filter "configurations:Debug"
            runtime "Debug"
            symbols "on"

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

premake5.lua (ImGui)

project "ImGui"
    kind "StaticLib"
    language "C++"

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

    files {
        "imconfig.h",
        "imgui.h",
        "imgui.cpp",
        "imgui_draw.cpp",
        "imgui_internal.h",
        "imgui_widgets.cpp",
        "imstb_rectpack.h",
        "imstb_textedit.h",
        "imstb_truetype.h",
        "imgui_demo.cpp",
        "premake5.lua"
    }

    filter "configurations:Debug"
        runtime "Debug"
        symbols "on"

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

Then, for my main application, I have this premake file.

premake5.lua (MyApp)

workspace "MyApp"
    configurations { "Debug", "Release" }
    platforms { "Win32", "Win64" }

project "MyApp"

    kind "ConsoleApp"
    language "C++"

    outputdir = "out/%cfg.{buildcfg}"

    targetdir (outputdir)

    IMGUI_LIB = "lib/imgui"
    IMGUI_LIB_BE = IMGUI_LIB .. "/backends"

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


    includedirs {
        IMGUI_LIB,
        IMGUI_LIB_BE,
        "lib/glm",
        "lib/glew/include",
        "lib/glfw/include"
    }

    libdirs { "lib/glew/lib/Release/x64"}

    include "lib/imgui"
    include "lib/glfw"
    include "lib/glew"

    links {"opengl32", "GLFW", "GLEW", "ImGui"}

    
     
    filter "configurations:Debug"
      defines { "DEBUG" }
      symbols "On"

    filter "configurations:Release"
      defines { "NDEBUG" }
      optimize "On"

If anyone knows what I am missing or if there is any information that would make the issue easier to diagnose, please let me know. I greatly appreciate any help anyone can provide.


Solution

  • Looking over your code, it seems that you are linking "opengl32", "GLFW", "GLEW", "ImGui" to your GLEW target.

    Your project node includes the scripts, and then links to the libraries, which probably activates each project, and leaves the last one activated:

    project "MyApp"
        -- some stuff here
        include "lib/imgui"
        include "lib/glfw"
        include "lib/glew"
        links {"opengl32", "GLFW", "GLEW", "ImGui"}
    

    Change that to something like:

    include "lib/imgui"
    include "lib/glfw"
    include "lib/glew"
    
    project "MyApp"
        -- some stuff here
        links {"opengl32", "GLFW", "GLEW", "ImGui"}