英文:
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
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 { "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
答案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 "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`](https://premake.github.io/docs/files/)
[`includedirs`](https://premake.github.io/docs/includedirs/)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论