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

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

Linking errors when running project with specific configuration

问题

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

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

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

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

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

这是完整的配置代码:

  1. -- 包括Conan生成脚本
  2. include("conanbuildinfo.premake.lua")
  3. -- 清理工作区的脚本
  4. include("cleanWorkspace.lua")
  5. -- 主工作区
  6. workspace "ImageEditor"
  7. -- 导入Conan生成配置
  8. conan_basic_setup()
  9. -- 项目
  10. project "ImageEditorApp"
  11. kind "ConsoleApp"
  12. language "C++"
  13. targetdir "bin/%{cfg.buildcfg}"
  14. objdir "bin/%{cfg.buildcfg}/obj/"
  15. location "src"
  16. debugdir "app"
  17. linkoptions { conan_exelinkflags }
  18. files { "src/**.cpp", "src/**.hpp" }
  19. includedirs { "./bindings" }
  20. filter "configurations:Debug"
  21. defines { "DEBUG" }
  22. symbols "On"
  23. filter "configurations:Release"
  24. defines { "NDEBUG" }
  25. optimize "On"
  26. -- 测试
  27. project "ImageEditorTest"
  28. kind "ConsoleApp"
  29. language "C++"
  30. targetdir "bin/%{cfg.buildcfg}"
  31. objdir "bin/%{cfg.buildcfg}/obj/"
  32. location "tests"
  33. debugdir "app"
  34. linkoptions { conan_exelinkflags }
  35. links { "ImageEditor" }
  36. -- 添加测试文件
  37. files { "tests/*.cpp" }
  38. includedirs { "./src","./bindings" }
  39. -- 也可以通过定义来配置Catch
  40. defines "CATCH_CPP11_OR_GREATER"
  41. filter "configurations:Debug"
  42. defines { "DEBUG" }
  43. symbols "On"
  44. filter "configurations:Release"
  45. defines { "NDEBUG" }
  46. optimize "On"

以及我的文件夹结构:

  1. .gitignore
  2. cleanWorkspace.lua
  3. conan.lock
  4. conanbuildinfo.premake.lua
  5. conanbuildinfo.txt
  6. conanfile.txt
  7. conaninfo.txt
  8. conan_imports_manifest.txt
  9. graph_info.json
  10. ImageEditor.sln
  11. LICENSE
  12. Makefile
  13. premake5.lua
  14. README.md
  15. +---app
  16. | Catch2.dll
  17. | Catch2Main.dll
  18. | glew32.dll
  19. | glfw3.dll
  20. | imgui.dll
  21. | imgui.ini
  22. +---bin
  23. | \---Debug
  24. | | ImageEditorApp.pdb
  25. | |
  26. | \---obj
  27. | \---Debug
  28. | \---ImageEditorApp
  29. | | ImageEditorApp.exe.recipe
  30. | | ImageEditorApp.log
  31. | | imgui_impl_glfw.obj
  32. | | imgui_impl_opengl3.obj
  33. | | main.obj
  34. | | test_window.obj
  35. | | vc143.idb
  36. | | vc143.pdb
  37. | | Window.obj
  38. | |
  39. | \---ImageEditorApp.tlog
  40. | CL.command.1.tlog
  41. | CL.read.1.tlog
  42. | CL.write.1.tlog
  43. | ImageEditorApp.lastbuildstate
  44. | link-cvtres.read.1.tlog
  45. | link-cvtres.write.1.tlog
  46. | link-rc.read.1.tlog
  47. | link-rc.write.1.tlog
  48. | link.32112-cvtres.read.1.tlog
  49. | link.32112-cvtres.write.1.tlog
  50. | link.32112-rc.read.1.tlog
  51. | link.32112-rc.write.1.tlog
  52. | link.32112.read.1.tlog
  53. | link.32112.read.2.tlog
  54. | link.32112.write.1.tlog
  55. | link.command.1.tlog
  56. | link.read.1.tlog
  57. | link.read.2.tlog
  58. | link.write.1.tlog
  59. | unsuccessfulbuild
  60. |
  61. +---bindings
  62. | imgui_impl_glfw.cpp
  63. | imgui_impl_glfw.h
  64. | imgui_impl_opengl3.cpp
  65. | imgui_impl_opengl3.h
  66. | imgui_impl_opengl3_loader.h
  67. |
  68. +---src
  69. | | ImageEditorApp.vcxproj
  70. | | ImageEditorApp.vcxproj.filters
  71. | | ImageEditorApp.vcxproj.user
  72. | | main.cpp
  73. | |
  74. | \---view
  75. | Window.cpp
  76. | Window.h
  77. |
  78. +---tests
  79. | ImageEditorTest.vcxproj
  80. | ImageEditorTest.vcxproj.user
  81. | test_window.cpp
  82. |
  83. \---vendor
  84. \---premake
  85. premake5.exe
  86. 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:

  1. -- Include conan generate script
  2. include(&quot;conanbuildinfo.premake.lua&quot;)
  3. -- Script for cleaning workspace
  4. include(&quot;cleanWorkspace.lua&quot;)
  5. -- Main Workspace
  6. workspace &quot;ImageEditor&quot;
  7. -- Import conan generate config
  8. conan_basic_setup()
  9. -- Project
  10. project &quot;ImageEditorApp&quot;
  11. kind &quot;ConsoleApp&quot;
  12. language &quot;C++&quot;
  13. targetdir &quot;bin/%{cfg.buildcfg}&quot;
  14. objdir &quot;bin/%{cfg.buildcfg}/obj/&quot;
  15. location &quot;src&quot;
  16. debugdir &quot;app&quot;
  17. linkoptions { conan_exelinkflags }
  18. files { &quot;src/**.cpp&quot;, &quot;src/**.hpp&quot; }
  19. includedirs { &quot;./bindings&quot; }
  20. filter &quot;configurations:Debug&quot;
  21. defines { &quot;DEBUG&quot; }
  22. symbols &quot;On&quot;
  23. filter &quot;configurations:Release&quot;
  24. defines { &quot;NDEBUG&quot; }
  25. optimize &quot;On&quot;
  26. -- Tests
  27. project &quot;ImageEditorTest&quot;
  28. kind &quot;ConsoleApp&quot;
  29. language &quot;C++&quot;
  30. targetdir &quot;bin/%{cfg.buildcfg}&quot;
  31. objdir &quot;bin/%{cfg.buildcfg}/obj/&quot;
  32. location &quot;tests&quot;
  33. debugdir &quot;app&quot;
  34. linkoptions { conan_exelinkflags }
  35. links { &quot;ImageEditor&quot; }
  36. -- Add test files
  37. files { &quot;tests/*.cpp&quot; }
  38. includedirs { &quot;./src&quot;,&quot;./bindings&quot; }
  39. -- We can also configure Catch through defines
  40. defines &quot;CATCH_CPP11_OR_GREATER&quot;
  41. filter &quot;configurations:Debug&quot;
  42. defines { &quot;DEBUG&quot; }
  43. symbols &quot;On&quot;
  44. filter &quot;configurations:Release&quot;
  45. defines { &quot;NDEBUG&quot; }
  46. optimize &quot;On&quot;

and my folder structure:

  1. .gitignore
  2. | cleanWorkspace.lua
  3. | conan.lock
  4. | conanbuildinfo.premake.lua
  5. | conanbuildinfo.txt
  6. | conanfile.txt
  7. | conaninfo.txt
  8. | conan_imports_manifest.txt
  9. | graph_info.json
  10. | ImageEditor.sln
  11. | LICENSE
  12. | Makefile
  13. | premake5.lua
  14. | README.md
  15. |
  16. +---app
  17. | Catch2.dll
  18. | Catch2Main.dll
  19. | glew32.dll
  20. | glfw3.dll
  21. | imgui.dll
  22. | imgui.ini
  23. |
  24. +---bin
  25. | \---Debug
  26. | | ImageEditorApp.pdb
  27. | |
  28. | \---obj
  29. | \---Debug
  30. | \---ImageEditorApp
  31. | | ImageEditorApp.exe.recipe
  32. | | ImageEditorApp.log
  33. | | imgui_impl_glfw.obj
  34. | | imgui_impl_opengl3.obj
  35. | | main.obj
  36. | | test_window.obj
  37. | | vc143.idb
  38. | | vc143.pdb
  39. | | Window.obj
  40. | |
  41. | \---ImageEditorApp.tlog
  42. | CL.command.1.tlog
  43. | CL.read.1.tlog
  44. | CL.write.1.tlog
  45. | ImageEditorApp.lastbuildstate
  46. | link-cvtres.read.1.tlog
  47. | link-cvtres.write.1.tlog
  48. | link-rc.read.1.tlog
  49. | link-rc.write.1.tlog
  50. | link.32112-cvtres.read.1.tlog
  51. | link.32112-cvtres.write.1.tlog
  52. | link.32112-rc.read.1.tlog
  53. | link.32112-rc.write.1.tlog
  54. | link.32112.read.1.tlog
  55. | link.32112.read.2.tlog
  56. | link.32112.write.1.tlog
  57. | link.command.1.tlog
  58. | link.read.1.tlog
  59. | link.read.2.tlog
  60. | link.write.1.tlog
  61. | unsuccessfulbuild
  62. |
  63. +---bindings
  64. | imgui_impl_glfw.cpp
  65. | imgui_impl_glfw.h
  66. | imgui_impl_opengl3.cpp
  67. | imgui_impl_opengl3.h
  68. | imgui_impl_opengl3_loader.h
  69. |
  70. +---src
  71. | | ImageEditorApp.vcxproj
  72. | | ImageEditorApp.vcxproj.filters
  73. | | ImageEditorApp.vcxproj.user
  74. | | main.cpp
  75. | |
  76. | \---view
  77. | Window.cpp
  78. | Window.h
  79. |
  80. +---tests
  81. | ImageEditorTest.vcxproj
  82. | ImageEditorTest.vcxproj.user
  83. | test_window.cpp
  84. |
  85. \---vendor
  86. \---premake
  87. premake5.exe
  88. 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. 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)
  2. 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)
  3. 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)
  4. 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)
  5. 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)
  6. 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)
  7. 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)
  8. 1&gt;..\bin\Debug\ImageEditorApp.exe : fatal error LNK1120: 7 unresolved externals

答案1

得分: 1

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

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

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

另外添加:

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

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

因此,你应该有:

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

或者单行写为

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

文档链接:
files
includedirs

  1. <details>
  2. <summary>英文:</summary>

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

  1. adds only cpp files and header files from src directory.

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

  1. add in addition
  2. - files from &quot;bindings&quot; (the ones you expect)
  3. - files from &quot;tests&quot; (that you don&#39;t want)

includedirs { "./bindings" }

  1. is not to add files from that directory, but to allow `#include` to search on that directory.
  2. So you should have

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

  1. or in single line

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

  1. Doc:
  2. [`files`](https://premake.github.io/docs/files/)
  3. [`includedirs`](https://premake.github.io/docs/includedirs/)
  4. </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:

确定