命令行有效,Makefile 中的相同命令不起作用

commandline works, same command in Makefile doesn't

本文关键字:命令 不起作用 有效 Makefile 命令行      更新时间:2023-10-16

我完成了本教程,了解如何安装Mesa(OpenGL)。一切都按照描述工作。但是当我尝试将运行良好的构建命令放入 makefile 中时,它没有。

这是我的制作文件:

all:
    gcc -lglut -IGL -IGLEW -IGLU main.c -o OpenGLExample

如果我输入 make,我会得到:

gcc -lglut -IGL -IGLEW -IGLU main.c -o OpenGLExample
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glClearColor'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glClear'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glColor3f'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glOrtho'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glBegin'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glVertex2f'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glVertex2f'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glVertex2f'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glVertex2f'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glEnd'
/tmp/ccKVrQAu.o:main.c:function renderFunction: error: undefined reference to 'glFlush'
collect2: ld returned 1 exit status
make: *** [all] Error 1

我使用的是 Ubuntu 而不是教程中的 Mint。

我做错了什么?

您没有将 OpenGl 库传递给链接器。您应该键入l而不是I

all:
    gcc main.c -lglut -lGL -lGLEW -lGLU  -o OpenGLExample

您还应该传递编译器可以找到 OpenGL 头文件的目录,这可以通过 -I 来完成,并且可能还有一个选项(或多个选项),指定链接器应该在哪里查找库(-L)