G++ Undefined References

I am new to g++ (normally I program in Ada using Gnat) and the file that I am trying to compile is in the directory C:\bullet\src and uses files in that directory. However, even after I include the right file path it is still having undefined references.

Command set:
CD C:\bullet\src
g++ HelloWorld.cpp -IC:\bullet\src
Output:
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0xbc): undefined reference to `btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btDefaultCollisionConstructionInfo const&)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x140): undefined reference to `btCollisionDispatcher::btCollisionDispatcher(btCollisionConfiguration*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x1c5): undefined reference to `btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x242): undefined reference to `btSequentialImpulseConstraintSolver::btSequentialImpulseConstraintSolver()'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x2db): undefined reference to `btDiscreteDynamicsWorld::btDiscreteDynamicsWorld(btDispatcher*, btBroadphaseInterface*, btConstraintSolver*, btCollisionConfiguration*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x657): undefined reference to `btRigidBody::btRigidBody(btRigidBody::btRigidBodyConstructionInfo const&)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text+0x93d): undefined reference to `btRigidBody::btRigidBody(btRigidBody::btRigidBodyConstructionInfo const&)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btSphereShapedlEPv[btSphereShape::operator delete(void*)]+0xd): undefined reference to `btAlignedFreeInternal(void*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btSphereShapeC1Ef[btSphereShape::btSphereShape(float)]+0xd): undefined reference to `btConvexInternalShape::btConvexInternalShape()'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btSphereShapeC1Ef[btSphereShape::btSphereShape(float)]+0x16): undefined reference to `vtable for btSphereShape'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btSphereShapenwEj[btSphereShape::operator new(unsigned int)]+0x15): undefined reference to `btAlignedAllocInternal(unsigned int, int)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN17btCollisionObjectdlEPv[btCollisionObject::operator delete(void*)]+0xd): undefined reference to `btAlignedFreeInternal(void*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN17btCollisionObjectnwEj[btCollisionObject::operator new(unsigned int)]+0x15): undefined reference to `btAlignedAllocInternal(unsigned int, int)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btConvexShapedlEPv[btConvexShape::operator delete(void*)]+0xd): undefined reference to `btAlignedFreeInternal(void*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN10btBoxShapeC1ERK9btVector3[btBoxShape::btBoxShape(btVector3 const&)]+0xf): undefined reference to `btPolyhedralConvexShape::btPolyhedralConvexShape()'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN10btBoxShapeC1ERK9btVector3[btBoxShape::btBoxShape(btVector3 const&)]+0x18): undefined reference to `vtable for btBoxShape'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN13btConvexShapenwEj[btConvexShape::operator new(unsigned int)]+0x15): undefined reference to `btAlignedAllocInternal(unsigned int, int)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN18btAlignedAllocatorIP16btCollisionShapeLj16EE10deallocateEPS1_[btAlignedAllocator<btCollisionShape*, 16u>::deallocate(btCollisionShape**)]+0xd): undefined reference to `btAlignedFreeInternal(void*)'
C:\Temp/cc5vcez2.o:helloworld.cpp:(.text$_ZN18btAlignedAllocatorIP16btCollisionShapeLj16EE8allocateEiPPKS1_[btAlignedAllocator<btCollisionShape*, 16u>::allocate(int, btCollisionShape* const**)]+0x18): undefined reference to `btAlignedAllocInternal(unsigned int, int)'
Typically you don't need to post every error for a helloworld app (or any basic app), as usually it's just one error that is the problem.
Please post your code, will be much easier this way than us having to guess.

You say the folder contains files you need, are these object files or .cpp files or headers or what?

with header files any in the PATH or current directory will be included. with .cpp files and other source fiels you need to turn them into object files to link to the executable, you do this by using the -c flag.

eg, lets say I have these classes:

main.cpp class1.cpp class1.h class2.cpp class2.h

on the command line I would: g++ -c class1.cpp class2.cpp main.cpp

this creates object files.

then: g++ class1.o class2.o main.o

alternately you can omit the main:

1
2
3
4
g++ -c class1.cpp class2.cpp
g++ class1.o class2.o main.cpp
./a.exe // windows or
./a.out // linux 

Last edited on
I am trying to compile a HelloWorld program that used the bullet collision library. The makefile support for MinGW is not working as described so I was trying to just add the src path for the library to the command when I compiled the HelloWorld. When I used the Gnat compiler it would automatically compile all associated files into objects and relink them with the main, doesn't g++ do this?

there are about 40+ files associated with the "btBulletDynamicsCommon.h"
Here is the code if it helps...
#include "btBulletDynamicsCommon.h"
#include <stdio.h>

/// This is a Hello World program for running a basic Bullet physics simulation

int main(int argc, char** argv)
{

	int i;

	///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

	///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
	btCollisionDispatcher* dispatcher = new	btCollisionDispatcher(collisionConfiguration);

	///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
	btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();

	///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

	btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);

	dynamicsWorld->setGravity(btVector3(0,-10,0));

	///create a few basic rigid bodies
	btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));

	//keep track of the shapes, we release memory at exit.
	//make sure to re-use collision shapes among rigid bodies whenever possible!
	btAlignedObjectArray<btCollisionShape*> collisionShapes;

	collisionShapes.push_back(groundShape);

	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0,-56,0));

	{
		btScalar mass(0.);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			groundShape->calculateLocalInertia(mass,localInertia);

		//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
		btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
		btRigidBody* body = new btRigidBody(rbInfo);

		//add the body to the dynamics world
		dynamicsWorld->addRigidBody(body);
	}


	{
		//create a dynamic rigidbody

		//btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
		btCollisionShape* colShape = new btSphereShape(btScalar(1.));
		collisionShapes.push_back(colShape);

		/// Create Dynamic Objects
		btTransform startTransform;
		startTransform.setIdentity();

		btScalar	mass(1.f);

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			colShape->calculateLocalInertia(mass,localInertia);

			startTransform.setOrigin(btVector3(2,10,0));
		
			//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
			btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
			btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
			btRigidBody* body = new btRigidBody(rbInfo);

			dynamicsWorld->addRigidBody(body);
	}



/// Do some simulation



	for (i=0;i<100;i++)
	{
		dynamicsWorld->stepSimulation(1.f/60.f,10);
		
		//print positions of all objects
		for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
		{
			btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
			btRigidBody* body = btRigidBody::upcast(obj);
			if (body && body->getMotionState())
			{
				btTransform trans;
				body->getMotionState()->getWorldTransform(trans);
				printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
			}
		}
	}


	//cleanup in the reverse order of creation/initialization

	//remove the rigidbodies from the dynamics world and delete them
	for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
	{
		btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
		btRigidBody* body = btRigidBody::upcast(obj);
		if (body && body->getMotionState())
		{
			delete body->getMotionState();
		}
		dynamicsWorld->removeCollisionObject( obj );
		delete obj;
	}

	//delete collision shapes
	for (int j=0;j<collisionShapes.size();j++)
	{
		btCollisionShape* shape = collisionShapes[j];
		collisionShapes[j] = 0;
		delete shape;
	}

	//delete dynamics world
	delete dynamicsWorld;

	//delete solver
	delete solver;

	//delete broadphase
	delete overlappingPairCache;

	//delete dispatcher
	delete dispatcher;

	delete collisionConfiguration;

	//next line is optional: it will be cleared by the destructor when the array goes out of scope
	collisionShapes.clear();

}

Last edited on
Topic archived. No new replies allowed.