M1 macOS下VSCode终端make编译OpenGL正常,常规终端报错
问题:M1 Mac上VSCode终端与常规终端编译OpenGL程序结果不一致
环境信息
- 操作系统:macOS Ventura 13.1
- 芯片:Apple M1
- 依赖:GLEW 2.2.0_1(通过Homebrew安装在
/opt/homebrew/Cellar/glew/2.2.0_1)
Makefile内容
CXX = clang++ CXXFLAGS = -I/opt/homebrew/Cellar/glew/2.2.0_1/include -L/opt/homebrew/Cellar/glew/2.2.0_1/lib -w LIBS = -lGLEW.2.2.0 -framework OpenGL -framework GLUT SOURCES = t2.cpp OBJ = $(SOURCES:.cpp=.o) TARGETS = $(SOURCES:.cpp=) all: $(TARGETS) %.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $< $(TARGETS): %: %.o $(CXX) $(CXXFLAGS) -o $@ $< $(LIBS) clean: rm -f $(OBJ) $(TARGETS) *.o .PHONY: all clean
源代码t2.cpp
#include <iostream> #include <GL/glew.h> #include <GLUT/glut.h> void resizeCB(int width, int height) { /* use the full view port */ glViewport(0, 0, width, height); /* use 2D orthographic parallel projection */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, width, 0.0, height); } typedef struct point2D { int x; int y; } Point2D; Point2D roof[3]; Point2D house[4]; void initGeom() { // initialize the roof roof[0].x = 270; roof[0].y = 340; roof[1].x = 450; roof[1].y = 340; roof[2].x = 360; roof[2].y = 400; house[0].x = 285; house[0].y = 270; house[1].x = 435; house[1].y = 270; house[2].x = 435; house[2].y = 340; house[3].x = 285; house[3].y = 340; } // clear the window and fill it with white colour // set up gl colour to white void renderCB() { // glClearColor(1.0, 1.0, 1.0, 0.0); // clearing up the canvas with the specific colour initGeom(); glClear(GL_COLOR_BUFFER_BIT); // swap the buffers (using double buffers glColor3f(0.0, 1.0, 0.0); glBegin(GL_TRIANGLES); for (int i = 0; i < 3; ++i) { glVertex2iv((GLint *)&roof[i]); } glEnd(); glColor3f(0.0, 0.0, 1.0); glBegin(GL_POLYGON); for (int i = 0; i < 4; ++i) { glVertex2iv((GLint *)&house[i]); } glEnd(); glFlush(); glutSwapBuffers(); // return; } int main(int argc, char **argv) { int winId; // initialize GLUT and pass it the command variables glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(1024, 768); glutInitWindowPosition(100, 100); winId = glutCreateWindow("Tutorial 02"); GLenum res = glewInit(); if (res != GLEW_OK) { printf("Error - %s \n", glewGetErrorString(res)); return (-1); } // print OpenGL information printf("Renderer: %s \n", glGetString(GL_RENDERER)); printf("OpenGL version: %s \n", glGetString(GL_VERSION)); printf("OpenGL shading language version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); glutReshapeFunc(resizeCB); glutDisplayFunc(renderCB); glutMainLoop(); return (0); }
编译结果差异
VSCode集成终端执行make
编译成功,输出:
clang++ -I/opt/homebrew/Cellar/glew/2.2.0_1/include -L/opt/homebrew/Cellar/glew/2.2.0_1/lib -w -c -o t2.o t2.cpp clang++ -I/opt/homebrew/Cellar/glew/2.2.0_1/include -L/opt/homebrew/Cellar/glew/2.2.0_1/lib -w -o t2 t2.o -lGLEW.2.2.0 -framework OpenGL -framework GLUT
常规终端执行make
链接错误,输出:
clang++ -I/opt/homebrew/Cellar/glew/2.2.0_1/include -L/opt/homebrew/Cellar/glew/2.2.0_1/lib -w -c -o t2.o t2.cpp clang++ -I/opt/homebrew/Cellar/glew/2.2.0_1/include -L/opt/homebrew/Cellar/glew/2.2.0_1/lib -w -o t2 t2.o -lGLEW.2.2.0 -framework OpenGL -framework GLUT Undefined symbols for architecture x86_64: "_glewGetErrorString", referenced from: _main in t2.o "_glewInit", referenced from: _main in t2.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [t2] Error 1
疑问与需求
重启电脑后问题消失,但无法理解两个终端的make执行结果差异。推测VSCode终端调用的是arm64架构的clang++,而常规终端调用的是x86_64架构的clang++,但Makefile未指定架构,默认应为ARM。希望找到问题根源,避免后续再出现此类问题。
内容的提问来源于stack exchange,提问作者dodeca12
相关产品推荐
相关产品推荐

