编译多个C ++ OpenGL

时间:2012-05-31 16:46:15

标签: c++ opengl compilation header g++

我正在编写一个简单的C ++,opengl程序,而且我很难从目标文件中生成可执行文件。我可以使用:

创建每个.cpp的目标文件而不会出错
g++ -c filename.cpp  ---->   filename.o

每个.cpp创建一个对象,这使我假设它正在找到opengl头文件,但是当我尝试从对象创建可执行文件时,它会输出:

g++ -o OUTPUT Main.o Time.o Map.o Vehicle.o

Main.o: In function `init()':
Main.cpp:(.text+0x12a): undefined reference to `glEnable'
Main.cpp:(.text+0x134): undefined reference to `glShadeModel'
Main.cpp:(.text+0x14d): undefined reference to `glViewport'
Main.cpp:(.text+0x157): undefined reference to `glMatrixMode'
Main.cpp:(.text+0x15c): undefined reference to `glLoadIdentity'

我意识到g ++告诉我它无法在任何地方找到OpenGL函数,这意味着它没有链接到glu.h,gl.h等。但是为什么创建对象文件时没有类似的错误,但我无法制作我的可执行文件?对象文件不会遇到与.exe相同的链接错误吗?我试过-I和g ++ -o RUN Time.o Main.o Map.o Vehicle.o -L / usr / include / GL / -lGL -lGLU,我还验证了OpenGL目录的路径。

每个文件的标题如下:

libgl.h:

#ifndef LIBGL_H
#define LIBGL_H

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "Time.h"
#include "Link.h"

#endif

time.h中:

#ifndef TIME_H
#define TIME_H

extern float GLOBAL_TIME;

#endif

Map.h

#ifndef MAP_H
#define MAP_H
#include "libgl.h"
....
#endif

Vehicle.h:

#ifndef VEHICLE_H
#define VEHICLE_H
#include "libgl.h"
...
#endif

Main.cpp的:

#include "libgl.h"
#include "Map.h"
#include "Vehicle.h"
 ...

关于我的标题/链接和编译,我有些遗漏,如果有人有任何建议我会非常感激。

1 个答案:

答案 0 :(得分:3)

您需要链接OpenGL库:可能会将-lgl传递给最后一个g ++调用。

如果您的系统有pkg-config,则可以使用pkg-config --cflags --libs gl获取所有正确的标记。

为了将来参考,“未定义的引用”是一个链接器错误,它总是意味着同样的事情:你错过了一个目标文件或一个库。它只在链接时显示,而不是简单地编译(即使用-c