特定配置下运行项目时出现链接错误

huangapple go评论87阅读模式
英文:

Linking errors when running project with specific configuration

问题

我当前在使用Premake5.lua运行我的项目时遇到了链接错误的问题,特定配置下会出现问题。以下是我Premake5.lua文件中的相关配置:

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

但是,当我将配置更改为以下内容时,一切似乎正常运行:

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

是否有人能解释为什么会发生这种情况,以及如何解决这个问题?

这是完整的配置代码:


-- 包括Conan生成脚本
include("conanbuildinfo.premake.lua")
-- 清理工作区的脚本
include("cleanWorkspace.lua")

-- 主工作区
workspace "ImageEditor"
    -- 导入Conan生成配置
    conan_basic_setup()

    -- 项目
    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"

     -- 测试
    project "ImageEditorTest"
        kind "ConsoleApp"
        language "C++"
        targetdir "bin/%{cfg.buildcfg}"
        objdir "bin/%{cfg.buildcfg}/obj/"
        location "tests"
        debugdir "app"
        linkoptions { conan_exelinkflags }
        links { "ImageEditor" }
    
        -- 添加测试文件
        files { "tests/*.cpp" }
        includedirs { "./src","./bindings" }

        -- 也可以通过定义来配置Catch
	    defines "CATCH_CPP11_OR_GREATER"

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

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

以及我的文件夹结构:

.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

编辑
我是新手,如果有人能提供关于如何管理项目并为其创建单元测试的信息,我将不胜感激。谢谢。

这些是我遇到的错误:

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,class std::allocator > 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

英文:

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 { &quot;src/**.cpp&quot;, &quot;src/**.hpp&quot; }

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

files { &quot;**.cpp&quot;, &quot;**.hpp&quot; }

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(&quot;conanbuildinfo.premake.lua&quot;)
-- Script for cleaning workspace
include(&quot;cleanWorkspace.lua&quot;)



-- Main Workspace
workspace &quot;ImageEditor&quot;
    -- Import conan generate config
    conan_basic_setup()

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

        linkoptions { conan_exelinkflags }

        files { &quot;src/**.cpp&quot;, &quot;src/**.hpp&quot; }
        includedirs { &quot;./bindings&quot; }

        filter &quot;configurations:Debug&quot;
		defines { &quot;DEBUG&quot; }
		symbols &quot;On&quot;

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

        -- We can also configure Catch through defines
	    defines &quot;CATCH_CPP11_OR_GREATER&quot;

        filter &quot;configurations:Debug&quot;
            defines { &quot;DEBUG&quot; }
            symbols &quot;On&quot;

        filter &quot;configurations:Release&quot;
            defines { &quot;NDEBUG&quot; }
            optimize &quot;On&quot;

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

答案1

得分: 1

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

仅添加来自src目录的cpp文件和头文件。

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

另外添加:

  • 来自"bindings"目录的文件(预期的文件)
  • 来自"tests"目录的文件(你不想要的文件)
includedirs { "./bindings" }

不是添加来自该目录的文件,而是允许#include在该目录中搜索。

因此,你应该有:

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

或者单行写为

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

文档链接:
files
includedirs


<details>
<summary>英文:</summary>

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


adds only cpp files and header files from src directory.

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

add in addition
- files from &quot;bindings&quot; (the ones you expect)
- files from &quot;tests&quot; (that you don&#39;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`](https://premake.github.io/docs/files/)
[`includedirs`](https://premake.github.io/docs/includedirs/)
</details>

huangapple
  • 本文由 发表于 2023年3月7日 19:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定