开发者

I can't link my opengl program to math3d.h

I was writing an OpenGL program and it happens that I have a problem with linking with math3d.h.

I am using Ubuntu and g++.

The thing is that I didn't install the package for the math3d because I got the header file and a cpp file from the net with OpenGL superbible. So I just copied the header file and .cpp file to the local dire开发者_运维问答ctory and did

#include "math3d.h"

But the thing is that I used to use switches to link the other header files like gl.h, glu.h ,glut.h by giving.

g++ test.cpp -lGL -lGLU -lglut.  

But I don't know what to give for math3d. I get an error saying undefined reference to the functions. This error I used to get when I don't give -lGL etc. for the functions in those respective libraries.

I am totally stuck here and I don't know what to do and without this I cannot go forward.


You don't link header files. You include them, and then link the object files produced by the *.cpp files together.

Short answer

g++ test.cpp math3d.cpp -lGL -lGLU -lglut

... and it works.

Long Answer

What you are lacking is any kind of build system (read up on Makefile). You need to first build the math3d.cpp, then your test program.

Sample Makefile:

CC=g++
CFLAGS=-c -Wall
LDFLAGS=-lGL -lGLU -lglut
SOURCES=test.cpp math3d.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

Well, y'know, this one might even work :>


Need to compile math3d.cpp as well: g++ test.cpp math3d.cpp -lGL -lGLU -lglut


There are a couple of problems with your question:

  • You don't link to header files - you include header files, you link object files or libraries
  • Besides including the appropriate header files you need to link in the library - you said you copied the .cpp file from the 'local directory' does this mean you added one of the .cpp files from the OpenGL project into your project? This can work if you make sure you get all the .cpp files you need (the functions you use that are declared in 'math3d.h' may be implemented across a number of .cpp files). However, it is MUCH better to build openGL as a library and link against that. You may want to consult the OpenGL documentation to see how to build it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜