link error, pls help

inweiyu
Posts: 2
Joined: Sat Jan 24, 2009 12:36 am

link error, pls help

Post by inweiyu »

I think I have compiled bullet correctly in my Linux(Kubuntu)

Demos works fine

But I cannot write my own programm, there is always a link error

simple makefile like this:

Code: Select all

CXX := g++
CFLAGS :=
INCLUDE := -I/usr/local/include/bullet #I have copied the headfiles here
LIBS := -L /data/src/bullet/bullet-2.73/src -lbulletdynamics -lbulletcollision -lbulletmath -lbulletsoftbody

all: HelloWorld

HelloWorld: HelloWorld.o
    $(CXX) $(CFLAGS) $(INCLUDE) $(LIBS) $(OPENGL_LIBS) -o HelloWorld HelloWorld.o

HelloWorld.o: HelloWorld.cpp
    $(CXX) $(CFLAGS) $(INCLUDE) -c HelloWorld.cpp

clean:
    rm *.o
error like this:

Code: Select all

g++  -I/usr/local/include/bullet -c HelloWorld.cpp                                          
g++  -I/usr/local/include/bullet -L /data/src/bullet/bullet-2.73/src -lbulletdynamics -lbulletcollision -lbulletmath -lbulletsoftbody  -lGL -lGLU -lglut -o HelloWorld HelloWorld.o                                             
HelloWorld.o: In function `main':                                                                               
HelloWorld.cpp:(.text+0x4a): undefined reference to `btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btDefaultCollisionConstructionInfo const&)'                                                               
HelloWorld.cpp:(.text+0x7a): undefined reference to `btCollisionDispatcher::btCollisionDispatcher(btCollisionConfiguration*)' 
......
Jonan
Posts: 10
Joined: Mon Oct 27, 2008 9:31 am

Re: link error, pls help

Post by Jonan »

You need to specify the object files before bullet's libs, so the Makefile rule should look like this:

Code: Select all

HelloWorld: HelloWorld.o
    $(CXX) $(CFLAGS) HelloWorld.o $(LIBS) $(OPENGL_LIBS) -o HelloWorld
inweiyu
Posts: 2
Joined: Sat Jan 24, 2009 12:36 am

Re: link error, pls help

Post by inweiyu »

Thanks for your help

I've got the problem, but i find this very curious

Code: Select all

-lBulletDynamics -lBulletCollision -lLinearMath >>>>> OK
-lLinearMath -lBulletDynamics -lBulletCollision >>>>> link error!!
Maybe this means by linking static libraries, the sequence plays also importand role.

can any master give more details about this? :) thanks
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: link error, pls help

Post by sparkprime »

Yes, when linking static libraries, only the required code is pulled into the generated executable. This is to keep the size of the executable as small as possible. If you specify the maths library first, most of it will not be imported because you're not using it. Then when the linker tries to import the dynamics library, it will not be able to find the maths symbols because they weren't imported.

In general link static libraries after everything else and in reverse order of dependency.

At work we have two static libraries foo.a and bar.a that are actually mutually dependent so we have to link them like this:

-lfoo -lbar -lfoo