tags:

views:

154

answers:

3

Hey, 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 dint install the package for the math3d coz 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 directory and did

#include "math3d.h"

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

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

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

I am totally stuck here and i dont know what to do and without this i cannot go forward.
please help me.

Thank you.

A: 

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.
Trent
so what am i supposed to do with this how can i solve it? can u explain?
yuneek
@yuneek, I am not familiar with OpenGL - I was simply pointing out some general C++ problems with the way you stated your question. It sounds like you need to link in another library or compile the necessary .cpp files along with your project.
Trent
A: 

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

Evän Vrooksövich
thank you very much sir, it worked. :)
yuneek
+1  A: 

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 :>

Kornel Kisielewicz
sir thank you for the help. lemme try to use the makefile thank you :)
yuneek
the added benefit is that you won't need to retype the whole command line every time, and just type `make` instead. Not to mention it will be easy to add further files.
Kornel Kisielewicz