使用makefile编译时的链接器错误

时间:2015-02-23 10:52:38

标签: c makefile

我正在使用ubuntu 14.04 LTS并正在开发一个着色示例程序。我有以下makefile:

CC=gcc
CFLAGS=-Wall -I/usr/X11R6/include
LDFLAGS=-pthread -L/usr/X11R6/lib -lm -lGL -lglut
OBJS=main.o scene.o shader.o

glsl_lighting:  $(OBJS)
    $(CC) $(LDFLAGS) $(OBJS) -o glsl_lighting

clean:
    rm -f glsl_lighting
    rm -f $(OBJS)

main.o: main.c
scene.o: scene.c
shader.o: shader.c

主文件的前几行是:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "opengl.h"

这给出了以下错误:

gcc -pthread -L/usr/X11R6/lib -lm -lGL -lglut main.o scene.o shader.o -o glsl_lighting
main.o: In function `handleKeyPress':
main.c:(.text+0x27): undefined reference to `glutDestroyWindow'
main.o: In function `setPerspective':
main.c:(.text+0x65): undefined reference to `tanf'
main.c:(.text+0x144): undefined reference to `glMultMatrixf'
main.o: In function `main':
main.c:(.text+0x168): undefined reference to `glutInit'
main.c:(.text+0x172): undefined reference to `glutInitDisplayMode'
main.c:(.text+0x185): undefined reference to `glutInitWindowSize'
main.c:(.text+0x18f): undefined reference to `glutCreateWindow'
main.c:(.text+0x19f): undefined reference to `glutDisplayFunc'
main.c:(.text+0x1a9): undefined reference to `glutIdleFunc'
main.c:(.text+0x1b3): undefined reference to `glutKeyboardFunc'
main.c:(.text+0x1c4): undefined reference to `glClearColor'
main.c:(.text+0x1d1): undefined reference to `glClearDepth'
main.c:(.text+0x1db): undefined reference to `glDisable'
main.c:(.text+0x1e5): undefined reference to `glEnable'
main.c:(.text+0x1ef): undefined reference to `glDepthFunc'
main.c:(.text+0x1f9): undefined reference to `glEnable'
main.c:(.text+0x203): undefined reference to `glFrontFace'
main.c:(.text+0x20d): undefined reference to `glCullFace'
main.c:(.text+0x217): undefined reference to `glMatrixMode'
main.c:(.text+0x21c): undefined reference to `glLoadIdentity'
main.c:(.text+0x25c): undefined reference to `glMatrixMode'
main.c:(.text+0x266): undefined reference to `glutMainLoop'
scene.o: In function `createCylinder':
scene.c:(.text+0xf6): undefined reference to `cosf'
scene.c:(.text+0x147): undefined reference to `sinf'
scene.c:(.text+0x179): undefined reference to `cosf'
scene.c:(.text+0x1ca): undefined reference to `sinf'
scene.c:(.text+0x201): undefined reference to `cosf'
scene.c:(.text+0x252): undefined reference to `sinf'
scene.c:(.text+0x31b): undefined reference to `glGenBuffers'
scene.c:(.text+0x332): undefined reference to `glBindBuffer'
scene.c:(.text+0x358): undefined reference to `glBufferData'
scene.c:(.text+0x36e): undefined reference to `glEnableClientState'
scene.c:(.text+0x378): undefined reference to `glEnableClientState'
scene.c:(.text+0x394): undefined reference to `glVertexPointer'
scene.c:(.text+0x3ab): undefined reference to `glNormalPointer'
scene.o: In function `sceneInit':
scene.c:(.text+0x3c4): undefined reference to `glCreateProgram'
scene.c:(.text+0x40a): undefined reference to `glLinkProgram'
scene.c:(.text+0x425): undefined reference to `glGetProgramiv'
scene.c:(.text+0x44b): undefined reference to `glGetProgramiv'
scene.c:(.text+0x479): undefined reference to `glGetProgramInfoLog'
scene.c:(.text+0x4b4): undefined reference to `glDeleteProgram'
scene.c:(.text+0x4d5): undefined reference to `glGetUniformLocation'
scene.c:(.text+0x4f2): undefined reference to `glGetUniformLocation'
scene.c:(.text+0x50f): undefined reference to `glGetUniformLocation'
scene.c:(.text+0x5c5): undefined reference to `glLoadIdentity'
scene.c:(.text+0x606): undefined reference to `glTranslatef'
scene.o: In function `sceneRender':
scene.c:(.text+0x61a): undefined reference to `glClear'
scene.c:(.text+0x62c): undefined reference to `glUseProgram'
scene.c:(.text+0x648): undefined reference to `glUniform3fv'
scene.c:(.text+0x664): undefined reference to `glUniform3fv'
scene.c:(.text+0x680): undefined reference to `glUniform3fv'
scene.c:(.text+0x697): undefined reference to `glDrawArrays'
scene.c:(.text+0x6a6): undefined reference to `glUseProgram'
scene.c:(.text+0x6b7): undefined reference to `glPushMatrix'
scene.c:(.text+0x710): undefined reference to `glTranslatef'
scene.c:(.text+0x731): undefined reference to `glColor3fv'
scene.c:(.text+0x753): undefined reference to `glutSolidSphere'
scene.c:(.text+0x758): undefined reference to `glPopMatrix'
scene.c:(.text+0x76b): undefined reference to `glutSwapBuffers'
scene.o: In function `sceneCycle':
scene.c:(.text+0x8c4): undefined reference to `cosf'
scene.c:(.text+0x8f1): undefined reference to `cosf'
scene.c:(.text+0x906): undefined reference to `sinf'
scene.c:(.text+0x933): undefined reference to `sinf'
scene.c:(.text+0x957): undefined reference to `glutPostRedisplay'
shader.o: In function `shaderCompileFromFile':
shader.c:(.text+0x213): undefined reference to `glCreateShader'
shader.c:(.text+0x241): undefined reference to `glShaderSource'
shader.c:(.text+0x250): undefined reference to `glCompileShader'
shader.c:(.text+0x274): undefined reference to `glGetShaderiv'
shader.c:(.text+0x297): undefined reference to `glGetShaderiv'
shader.c:(.text+0x2c2): undefined reference to `glGetShaderInfoLog'
shader.c:(.text+0x2fe): undefined reference to `glDeleteShader'
shader.o: In function `shaderAttachFromFile':
shader.c:(.text+0x34a): undefined reference to `glAttachShader'
shader.c:(.text+0x359): undefined reference to `glDeleteShader'
collect2: error: ld returned 1 exit status

我确实已经安装了过剩和mesa,并且已经阅读了一些其他问题,你应该按照makefile的顺序进行操作但是在这种情况下如何?

1 个答案:

答案 0 :(得分:2)

您需要将$(LDFLAGS)(库)放在(对象)文件名之后,例如

$(CC)  $(OBJS) -o glsl_lighting $(LDFLAGS)

根据online gcc manual链接选项

  

[...] 在您编写此选项的命令中,它会有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,foo.o -lz bar.o在文件z之后但foo.o之前搜索库bar.o。如果bar.o引用z中的函数,则可能无法加载这些函数。

因此,使用库函数的目标文件应该出现在库本身之前。