gpt4 book ai didi

c - 使用 makefile 编译时出现链接器错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:34 31 4
gpt4 key购买 nike

我正在使用 ubuntu 14.04 LTS,并且正在开发一个着色示例程序。我有以下生成文件:

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

我确实安装了 glut 和 mesa,并阅读了一些其他问题,您应该按照 makefile 的顺序尝试一些事情,但在这种情况下如何?

最佳答案

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

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

根据 online gcc manual用于链接选项

[...] It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

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

关于c - 使用 makefile 编译时出现链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28672055/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com