c++ - glfw3 编译 undefined reference

标签 c++ opengl gcc undefined-reference glfw

我在编译使用 glfw3 库的程序时遇到问题。

尝试使用 make 进行编译时,我得到了 undefined reference 的错误列表,但我的类被编译成 .o 文件,只有最终的可执行文件没有创建。

标准输出:

g++ -Wall -g -c main.cpp -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi 

g++ -Wall -g -c error.cpp -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi

g++ -Wall -g -c sWindow.cpp -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi 

g++ -Wall -g -o ecl main.o error.o sWindow.o -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi

标准错误:

/usr/local/lib/libglfw3.a(context.c.o): In function `parseGLVersion':
context.c:(.text+0x53): undefined reference to `glGetString'
/usr/local/lib/libglfw3.a(context.c.o): In function `_glfwRefreshContextAttribs':
context.c:(.text+0x907): undefined reference to `glGetIntegerv'
context.c:(.text+0x98a): undefined reference to `glGetIntegerv'
context.c:(.text+0x9df): undefined reference to `glGetIntegerv'
context.c:(.text+0xa32): undefined reference to `glGetIntegerv'
/usr/local/lib/libglfw3.a(context.c.o): In function `glfwExtensionSupported':
context.c:(.text+0xd59): undefined reference to `glGetString'
context.c:(.text+0xd93): undefined reference to `glGetIntegerv'
/usr/local/lib/libglfw3.a(window.c.o): In function `glfwCreateWindow':
window.c:(.text+0x6f8): undefined reference to `glClear'
/usr/local/lib/libglfw3.a(x11_time.c.o): In function `getRawTime':
x11_time.c:(.text+0x20): undefined reference to `clock_gettime'
/usr/local/lib/libglfw3.a(x11_time.c.o): In function `_glfwInitTimer':
x11_time.c:(.text+0x72): undefined reference to `clock_gettime'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `getFBConfigAttrib':
glx_context.c:(.text+0x51): undefined reference to `glXGetFBConfigAttrib'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `chooseFBConfig':
glx_context.c:(.text+0x7e): undefined reference to `glXGetClientString'
glx_context.c:(.text+0x104): undefined reference to `glXGetFBConfigs'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `createLegacyContext':
glx_context.c:(.text+0x43f): undefined reference to `glXCreateNewContext'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwInitContextAPI':
glx_context.c:(.text+0x48f): undefined reference to `glXQueryExtension'
glx_context.c:(.text+0x4ca): undefined reference to `glXQueryVersion'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwCreateContext':
glx_context.c:(.text+0x791): undefined reference to `glXGetVisualFromFBConfig'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwDestroyContext':
glx_context.c:(.text+0xd09): undefined reference to `glXDestroyContext'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwPlatformMakeContextCurrent':
glx_context.c:(.text+0xd55): undefined reference to `glXMakeCurrent'
glx_context.c:(.text+0xd70): undefined reference to `glXMakeCurrent'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwPlatformSwapBuffers':
glx_context.c:(.text+0xdc2): undefined reference to `glXSwapBuffers'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwPlatformExtensionSupported':
glx_context.c:(.text+0xe69): undefined reference to `glXQueryExtensionsString'
/usr/local/lib/libglfw3.a(glx_context.c.o): In function `_glfwPlatformGetProcAddress':
glx_context.c:(.text+0xeb1): undefined reference to `glXGetProcAddressARB'
collect2: ld returned 1 exit status
make: *** [ecl] Error 1

这是我的生成文件:

CXX = g++
CXXFLAGS = -Wall -g
LIBS = -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi
DEPENDENCIES = main.o error.o sWindow.o

# ****************************************************
# Targets needed to bring the executable up to date

ecl: $(DEPENDENCIES)
    $(CXX) $(CXXFLAGS) -o ecl $(DEPENDENCIES) $(LIBS)

main.o: main.cpp
    $(CXX) $(CXXFLAGS) -c main.cpp $(LIBS)

error.o: error.cpp
    $(CXX) $(CXXFLAGS) -c error.cpp $(LIBS)

sWindow.o: sWindow.cpp
    $(CXX) $(CXXFLAGS) -c sWindow.cpp $(LIBS) 

clean: 
    rm -f *.o main
    echo "Clean done"

您是否知道导致此问题的原因以及解决方法?

最佳答案

按照这个顺序做:

-lglfw3 -lGLU -lGL

而不是这个:

-lGL -lGLU -lglfw3

GCC 对您在链接阶段指定库的顺序非常敏感。例如,如果 libA.a 依赖于 libB.a,那么您必须按以下方式调用链接器:

gcc ... -lA -lB ...

在您的情况下,libglfw3.a 显然依赖于 libGL.a,因此您会收到错误。

关于c++ - glfw3 编译 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362063/

相关文章:

c++ - OpenGL/GLSL - 使用缓冲区对象来获取统一的数组值

c++ - 如何完全绕过 `error: no match for ‘operator==’`?

c - 文件 linux/compiler.h 中定义的预处理器宏的目的是什么?

c - 了解 map 文件,优化大小

c++ - 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?

c++ - 这个字符串赋值有什么问题?

c# - 如何制作脚本/程序以使应用程序始终运行?

c++ - GTEST : Identify before and after a SetUp and TearDown respectively

java - 即使有插值也固定了时间步长卡顿?

opengl - OpenGL 中内存管理的困惑