Question

I am planning to use Box2D in my C++ project. I have downloaded the latest version (v2.3.0.7 when asking this question) of Box2D and built it successfully. I can run the Testbed without any errors.

Now, I am trying to build the HelloWorld.cpp example, which resides in HelloWorld folder in Box2D source files but I can't compile this example.

Below is my command for compiling:

g++ -g -Wall -L/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Build/Box2D -lBox2D -I/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D HelloWorld.cpp -o Hello

And this is the output I get:

/tmp/cc2U314E.o: In function `main':
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:37: undefined reference to `b2World::b2World(b2Vec2 const&)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:46: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:52: undefined reference to `b2PolygonShape::SetAsBox(float, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:55: undefined reference to `b2Body::CreateFixture(b2Shape const*, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:61: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:65: undefined reference to `b2PolygonShape::SetAsBox(float, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:78: undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:92: undefined reference to `b2World::Step(float, int, int)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:104: undefined reference to `b2World::~b2World()'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:104: undefined reference to `b2World::~b2World()'
/tmp/cc2U314E.o: In function `b2PolygonShape':
/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Box2D/Collision/Shapes/b2PolygonShape.h:87: undefined reference to `vtable for b2PolygonShape'
/tmp/cc2U314E.o: In function `~b2PolygonShape':
/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Box2D/Collision/Shapes/b2PolygonShape.h:28: undefined reference to `vtable for b2PolygonShape'
collect2: ld returned 1 exit status

I'm using

  • -L flag for searching for libBox2D.a
  • -l flag for linking Box2D
  • -I flag for pointing to the Box2D headers directory

What am I missing? Is there any specific library or command argument I am supposed to add?

Was it helpful?

Solution

Put the -lBox2D behind the HelloWorld.cpp

OTHER TIPS

If you have built your project properly the HelloWorld example will be automatically built for you. There's a CMakeLists.txt file in the example folder just run cmake CMakeLists.txt in the folder and then run make. Prior to this you must have built and installed Box2D library. Here is what I did:

I entered the Build directory within the Box2D and typed the following command:

cmake -DBOX2D_INSTALL=ON -DBOX2D_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX=/opt/Box2D ..

Then I went one level up to Box2D directory and ran make then sudo make install.

Then I entered the HelloWorld directory and it was automatically built. I didn't have to build it manually. Also this is the output from make that showed the example was built:

Linking CXX static library libBox2D.a [ 42%] Built target Box2D [ 43%] Building CXX object HelloWorld/CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top