[英]Undefined reference using GLFW static in a shared library
我正在構建一個包含兩個主要組件的項目:游戲和引擎。 Engine 是一個調用大部分圖形調用的共享庫。 這就是為什么它應該包括 GLFW。 那么游戲必須包含引擎。 這是文件系統架構:
為了構建一切,我使用的是 premake5。 不要打擾 premake 文件夾,它是用於存儲目的。 這是我文件夾頂部的 premake5.lua:
workspace "Game"
architecture "x64"
configurations
{
"Debug",
"Release",
"Dist"
}
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"
-- Include directories relative to root folder (solution)
include "Engine/GLFW"
project "Engine"
location "Engine"
kind "SharedLib"
language "C++"
targetdir ("bin/" .. outputdir .. "/")
objdir ("bin-int/" .. outputdir .. "/")
pchheader "Egpch.h"
pchsource "Egpch.cpp"
files
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp"
}
includedirs
{
"%{prj.name}/",
"%{prj.name}/src",
"%{prj.name}/spdlog/include",
"Engine/GLFW/include"
}
links
{
"GLFW"
}
filter "system:linux"
cppdialect "C++17"
staticruntime "On"
links {
"GL"
}
filter "system:windows"
cppdialect "C++17"
staticruntime "On"
systemversion "latest"
links { "OpenGL32" }
defines
{
"EG_PLATFORM_WINDOWS",
"EG_BUILD_DLL";
}
filter "configurations:Debug"
defines "EG_DEBUG"
symbols "On"
filter "configurations:Release"
defines "EG_RELEASE"
optimize "On"
filter "configurations:Dist"
defines "EG_DIST"
optimize "On"
project "Game"
location "Game"
kind "ConsoleApp"
language "C++"
targetdir ("bin/" .. outputdir .. "/")
objdir ("bin-int/" .. outputdir .. "/")
files
{
"%{prj.name}/**.h",
"%{prj.name}/**.cpp"
}
includedirs
{
"Engine/spdlog/include",
"Engine/",
"Engine/src",
}
links
{
"Engine"
}
filter "system:linux"
cppdialect "C++17"
staticruntime "On"
filter "system:windows"
cppdialect "C++17"
staticruntime "On"
systemversion "latest"
defines
{
"EG_PLATFORM_WINDOWS",
}
filter "configurations:Debug"
defines "EG_DEBUG"
symbols "On"
filter "configurations:Release"
defines "EG_RELEASE"
optimize "On"
filter "configurations:Dist"
defines "EG_DIST"
optimize "On"
我的 GLFW 文件夾中還有一個預制文件:
project "GLFW"
kind "StaticLib"
language "C"
targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")
files
{
"include/GLFW/glfw3.h",
"include/GLFW/glfw3native.h",
"src/glfw_config.h",
"src/context.c",
"src/init.c",
"src/input.c",
"src/monitor.c",
"src/vulkan.c",
"src/window.c"
}
filter "system:windows"
systemversion "latest"
staticruntime "On"
files
{
"src/win32_init.c",
"src/win32_joystick.c",
"src/win32_monitor.c",
"src/win32_time.c",
"src/win32_thread.c",
"src/win32_window.c",
"src/wgl_context.c",
"src/egl_context.c",
"src/osmesa_context.c"
}
defines
{
"_GLFW_WIN32",
"_CRT_SECURE_NO_WARNINGS"
}
filter "system:linux"
pic "On"
systemversion "latest"
staticruntime "On"
files
{
"src/x11_init.c",
"src/x11_monitor.c",
"src/x11_window.c",
"src/xkb_unicode.c",
"src/posix_time.c",
"src/posix_thread.c",
"src/glx_context.c",
"src/egl_context.c",
"src/osmesa_context.c",
"src/linux_joystick.c"
}
defines
{
"_GLFW_X11"
}
filter "configurations:Debug"
runtime "Debug"
symbols "On"
filter "configurations:Release"
runtime "Release"
optimize "on"
而且我在鏈接游戲時找不到為什么我有這些未定義的引用:
==== Building GLFW (debug) ====
==== Building Engine (debug) ====
Linking Engine
==== Building Game (debug) ====
Linking Game
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformLoadModule'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_key_create'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_getspecific'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwSelectPlatform'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_key_delete'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformFreeModule'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `_glfwPlatformGetModuleSymbol'
/usr/bin/ld: ../bin/Debug-linux-x86_64/libEngine.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:91: ../bin/Debug-linux-x86_64/Game] Error 1
make: *** [Makefile:51: Game] Error 2
如果有人有想法?
---編輯:--- 所以我發現我在 GLFW 文件夾中的 premake 中犯了一個愚蠢的錯誤。 忘記一些必要的文件來解釋所有 _glfw 調用的未定義引用。 我仍然有 pthread 庫的未定義參考。 我正在尋找它。
感謝kiner_shah 提示問題來自圖書館。 事實上,我使用 GLFW 作為靜態庫,所以所有依賴庫都沒有附帶。 我需要為 linux 添加這些庫:
鏈接{“glfw”、“Xrandr”、“Xi”、“GLU”、“GL”、“X11”、“dl”、“pthread”、“stdc++fs”、}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.