使用 OpenGL 函数时,make 文件出现链接器错误

Linker error with make file when using OpenGL functions

提问人:Vincent Han 提问时间:3/4/2023 最后编辑:genpfaultVincent Han 更新时间:3/4/2023 访问量:71

问:

我在程序中使用 openGL 库时遇到了一些问题。首先,我确实正确地实现了所有相关的东西:

#define GL_SILENCE_DEPRECATION
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

而且,在我的这个程序中,我的任务是绘制一堆矩形,我在编译时遇到了一些问题。这是我使用make文件进行编译时的错误消息:

  Undefined symbols for architecture arm64:
  "_glBegin", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
  "_glClear", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
      display() in mondrian-a92186.o
  "_glClearColor", referenced from:
      _main in mondrian-a92186.o
  "_glColor3f", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
  "_glEnd", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
  "_glFlush", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
      display() in mondrian-a92186.o
  "_glLineWidth", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
  "_glLoadIdentity", referenced from:
      display() in mondrian-a92186.o
  "_glMatrixMode", referenced from:
      display() in mondrian-a92186.o
  "_glScalef", referenced from:
      display() in mondrian-a92186.o
  "_glTranslatef", referenced from:
      display() in mondrian-a92186.o
  "_glVertex2i", referenced from:
      drawRegionsRec(treeNode*) in mondrian-a92186.o
  "_glutCreateWindow", referenced from:
      _main in mondrian-a92186.o
  "_glutDisplayFunc", referenced from:
      _main in mondrian-a92186.o
  "_glutInit", referenced from:
      _main in mondrian-a92186.o
  "_glutInitDisplayMode", referenced from:
      _main in mondrian-a92186.o
  "_glutInitWindowPosition", referenced from:
      _main in mondrian-a92186.o
  "_glutInitWindowSize", referenced from:
      _main in mondrian-a92186.o
  "_glutMainLoop", referenced from:
      _main in mondrian-a92186.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mondrian] Error 1

从本质上讲,我调用的每个 OpenGL 函数都是无法识别的,然后我得到一个链接器错误。

我确保我的 makefile 也是正确的。

PLATFORM = $(shell uname)


## Compilation flags
##comment out one or the other 
##debugging 
CFLAGS = -g 
##release
#CFLAGS = -O3 -DNDEBUG
LDFLAGS=

CFLAGS+= -Wall

ifeq ($(PLATFORM),Darwin)
## Mac OS X
CFLAGS += -m64  -Wno-deprecated
INCLUDEPATH=-I/system/usr/local/include 
LDFLAGS+= -m64 -lc -framework AGL -framework OpenGL -framework GLUT -framework Foundation
else
## Linux
CFLAGS += -m64
INCLUDEPATH  = -I/usr/include/GL/ 
LIBPATH = -L/usr/lib64 -L/usr/X11R6/lib
LDFLAGS+=  -lGL -lglut -lrt -lGLU -lX11 -lm  -lXmu -lXext -lXi
endif


CC = g++ -O3 -Wall $(INCLUDEPATH)


PROGS = mondrian

default: $(PROGS)

mondrian: mondrian.o geom.o rtimer.o
    $(CC) -o $@ mondrian.o geom.o rtimer.o $(LDFLAGS)

mondrian.o: mondrian.cpp  geom.h rtimer.h
    $(CC) -c $(CFLAGS)   mondrian.cpp  -o $@

如何解决这个问题?

C++ OpenGL Makefile 链接器错误 过剩

评论

1赞 MadScientist 3/4/2023
首先,你在这里谈论的错误是链接器错误,所以你的注释实现了所有相关的东西,正确地显示包含行等,与这个问题无关;头文件由编译器使用,而不是由链接器使用。
2赞 MadScientist 3/4/2023
其次,在报告错误时,请显示调用的链接器命令行,而不仅仅是错误。如果您遇到链接器错误,则很可能是链接器命令行错误,如果您向我们展示它,则更容易看到问题。例如,在您的 makefile 中,您有适用于 MacOS 和 Linux 的链接器命令,但您的问题中没有告诉我们您是在 MacOS 上运行还是在 Linux 上运行,因此我们不知道将使用哪个链接器命令。
0赞 MadScientist 3/4/2023
最后,从错误中可以清楚地看出,它们不是从您显示的 makefile 生成的,因为它们说:但是您向我们展示的 makefile 中没有。... in mondrian-a92186.o ...mondrian-a92186

答: 暂无答案