Search code examples
c++testinglinker-errorsconanpremake

Linking errors when running project with specific configuration


I am currently facing an issue with linking errors when running my project using Premake5.lua with a specific configuration.

Here is the relevant configuration in my Premake5.lua file:

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

However, everything seems to work fine when I change the configuration to:

files { "**.cpp", "**.hpp" }

Can anyone explain why this is happening and how I can resolve this issue?

Here is the full configuration code:


-- Include conan generate script
include("conanbuildinfo.premake.lua")
-- Script for cleaning workspace
include("cleanWorkspace.lua")



-- Main Workspace
workspace "ImageEditor"
    -- Import conan generate config
    conan_basic_setup()

    -- Project
    project "ImageEditorApp"
        kind "ConsoleApp"
        language "C++"
        targetdir "bin/%{cfg.buildcfg}"
        objdir "bin/%{cfg.buildcfg}/obj/"
        location "src"
        debugdir "app"

        linkoptions { conan_exelinkflags }

        files { "src/**.cpp", "src/**.hpp" }
        includedirs { "./bindings" }

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

        filter "configurations:Release"
        defines { "NDEBUG" }
        optimize "On"
     -- Tests
    project "ImageEditorTest"
        kind "ConsoleApp"
        language "C++"
        targetdir "bin/%{cfg.buildcfg}"
        objdir "bin/%{cfg.buildcfg}/obj/"
        location "tests"
        debugdir "app"
        linkoptions { conan_exelinkflags }
        links { "ImageEditor" }
    
        -- Add test files
        files { "tests/*.cpp" }
        includedirs { "./src","./bindings" }

        -- We can also configure Catch through defines
        defines "CATCH_CPP11_OR_GREATER"

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

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

and my folder structure:

.gitignore
|   cleanWorkspace.lua
|   conan.lock
|   conanbuildinfo.premake.lua
|   conanbuildinfo.txt
|   conanfile.txt
|   conaninfo.txt
|   conan_imports_manifest.txt
|   graph_info.json
|   ImageEditor.sln
|   LICENSE
|   Makefile
|   premake5.lua
|   README.md
|
+---app
|       Catch2.dll
|       Catch2Main.dll
|       glew32.dll
|       glfw3.dll
|       imgui.dll
|       imgui.ini
|
+---bin
|   \---Debug
|       |   ImageEditorApp.pdb
|       |
|       \---obj
|           \---Debug
|               \---ImageEditorApp
|                   |   ImageEditorApp.exe.recipe
|                   |   ImageEditorApp.log
|                   |   imgui_impl_glfw.obj
|                   |   imgui_impl_opengl3.obj
|                   |   main.obj
|                   |   test_window.obj
|                   |   vc143.idb
|                   |   vc143.pdb
|                   |   Window.obj
|                   |
|                   \---ImageEditorApp.tlog
|                           CL.command.1.tlog
|                           CL.read.1.tlog
|                           CL.write.1.tlog
|                           ImageEditorApp.lastbuildstate
|                           link-cvtres.read.1.tlog
|                           link-cvtres.write.1.tlog
|                           link-rc.read.1.tlog
|                           link-rc.write.1.tlog
|                           link.32112-cvtres.read.1.tlog
|                           link.32112-cvtres.write.1.tlog
|                           link.32112-rc.read.1.tlog
|                           link.32112-rc.write.1.tlog
|                           link.32112.read.1.tlog
|                           link.32112.read.2.tlog
|                           link.32112.write.1.tlog
|                           link.command.1.tlog
|                           link.read.1.tlog
|                           link.read.2.tlog
|                           link.write.1.tlog
|                           unsuccessfulbuild
|
+---bindings
|       imgui_impl_glfw.cpp
|       imgui_impl_glfw.h
|       imgui_impl_opengl3.cpp
|       imgui_impl_opengl3.h
|       imgui_impl_opengl3_loader.h
|
+---src
|   |   ImageEditorApp.vcxproj
|   |   ImageEditorApp.vcxproj.filters
|   |   ImageEditorApp.vcxproj.user
|   |   main.cpp
|   |
|   \---view
|           Window.cpp
|           Window.h
|
+---tests
|       ImageEditorTest.vcxproj
|       ImageEditorTest.vcxproj.user
|       test_window.cpp
|
\---vendor
    \---premake
            premake5.exe
            premake5.LICENSE.txt

EDIT I new to these and if someone can provide info how this setup should look for managing project and create unit tests for it i would aprritate it . Thanks.

These are the errors i get:

1>Window.obj : error LNK2019: unresolved external symbol "bool __cdecl ImGui_ImplGlfw_InitForOpenGL(struct GLFWwindow *,bool)" (?ImGui_ImplGlfw_InitForOpenGL@@YA_NPEAUGLFWwindow@@_N@Z) referenced in function "public: __cdecl ImageEditor::UI::Window::Window(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,int)" (??0Window@UI@ImageEditor@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HH@Z)
1>Window.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui_ImplGlfw_Shutdown(void)" (?ImGui_ImplGlfw_Shutdown@@YAXXZ) referenced in function "public: virtual __cdecl ImageEditor::UI::Window::~Window(void)" (??1Window@UI@ImageEditor@@UEAA@XZ)
1>Window.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui_ImplGlfw_NewFrame(void)" (?ImGui_ImplGlfw_NewFrame@@YAXXZ) referenced in function "public: void __cdecl ImageEditor::UI::Window::run(void)" (?run@Window@UI@ImageEditor@@QEAAXXZ)
1>Window.obj : error LNK2019: unresolved external symbol "bool __cdecl ImGui_ImplOpenGL3_Init(char const *)" (?ImGui_ImplOpenGL3_Init@@YA_NPEBD@Z) referenced in function "public: __cdecl ImageEditor::UI::Window::Window(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,int)" (??0Window@UI@ImageEditor@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HH@Z)
1>Window.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui_ImplOpenGL3_Shutdown(void)" (?ImGui_ImplOpenGL3_Shutdown@@YAXXZ) referenced in function "public: virtual __cdecl ImageEditor::UI::Window::~Window(void)" (??1Window@UI@ImageEditor@@UEAA@XZ)
1>Window.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui_ImplOpenGL3_NewFrame(void)" (?ImGui_ImplOpenGL3_NewFrame@@YAXXZ) referenced in function "public: void __cdecl ImageEditor::UI::Window::run(void)" (?run@Window@UI@ImageEditor@@QEAAXXZ)
1>Window.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui_ImplOpenGL3_RenderDrawData(struct ImDrawData *)" (?ImGui_ImplOpenGL3_RenderDrawData@@YAXPEAUImDrawData@@@Z) referenced in function "public: void __cdecl ImageEditor::UI::Window::run(void)" (?run@Window@UI@ImageEditor@@QEAAXXZ)
1>..\bin\Debug\ImageEditorApp.exe : fatal error LNK1120: 7 unresolved externals

Solution

  • files { "src/**.cpp", "src/**.hpp" }
    

    adds only cpp files and header files from src directory.

    files { "**.cpp", "**.hpp" }
    

    add in addition

    • files from "bindings" (the ones you expect)
    • files from "tests" (that you don't want)
    includedirs { "./bindings" }
    

    is not to add files from that directory, but to allow #include to search on that directory.

    So you should have

    files { "src/**.cpp", "src/**.hpp" }
    files { "bindings/**.cpp", "bindings/**.hpp" }
    

    or in single line

    files { "src/**.cpp", "src/**.hpp", "bindings/**.cpp", "bindings/**.hpp" }
    

    Doc: files includedirs