Box2D Forums

It is currently Sun May 19, 2013 12:05 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Sep 23, 2008 11:51 am 
Offline

Joined: Tue Sep 23, 2008 11:46 am
Posts: 8
Hi,

I just learned GDI in C++ and the first thing I want to make is a simple physics sandbox. I have chosen Box2D for this because it seems lightweigth and easy :)
However, I'm new to including other projects in my project, so this is what I did to include Box2D:

I copied the Source directory from the zip to my project directory (Visual Studio 2008/Projects/Physics)
Then I make a file called "Box2D.h" and put this in it:

Code:
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef BOX2D_H
#define BOX2D_H

/**
\mainpage Box2D API Documentation

\section intro_sec Getting Started

For tutorials please see http://www.box2d.org/manual.html

For discussion please visit http://www.box2d.org/forum
*/

// These include files constitute the main Box2D API

#include "../Source/Common/b2Settings.h"

#include "../Source/Collision/Shapes/b2CircleShape.h"
#include "../Source/Collision/Shapes/b2PolygonShape.h"
#include "../Source/Collision/b2BroadPhase.h"
#include "../Source/Dynamics/b2WorldCallbacks.h"
#include "../Source/Dynamics/b2World.h"
#include "../Source/Dynamics/b2Body.h"

#include "../Source/Dynamics/Contacts/b2Contact.h"

#include "../Source/Dynamics/Joints/b2DistanceJoint.h"
#include "../Source/Dynamics/Joints/b2MouseJoint.h"
#include "../Source/Dynamics/Joints/b2PrismaticJoint.h"
#include "../Source/Dynamics/Joints/b2RevoluteJoint.h"
#include "../Source/Dynamics/Joints/b2PulleyJoint.h"
#include "../Source/Dynamics/Joints/b2GearJoint.h"

#endif


And then I added this to my main.cpp file:

Code:
#include "Box2D.h"


Code:
//World
b2AABB worldAABB;
worldAABB.lowerBound.Set(-500.0f, -350.0f);
worldAABB.upperBound.Set(500.0f, 350.0f);
b2Vec2 gravity(0.0f, -10.0f);
bool doSleep = true;
b2World world(worldAABB, gravity, doSleep);


However, when I add the last line it stops compiling with this error:

Code:
error LNK2001: unresolved external symbol "public: __thiscall b2World::~b2World(void)" (??1b2World@@QAE@XZ)
error LNK2001: unresolved external symbol "public: __thiscall b2World::b2World(struct b2AABB const &,struct b2Vec2 const &,bool)" (??0b2World@@QAE@ABUb2AABB@@ABUb2Vec2@@_N@Z)


I have no idea what this means and I want to use Box2D so badly :( Does anyone have a solution?


Top
 Profile  
 
PostPosted: Tue Sep 23, 2008 12:26 pm 
Offline

Joined: Wed Dec 19, 2007 1:19 pm
Posts: 34
Unresolved external symbol means that the compiler knows there's a function/method named like the one you are trying to call, but the linker can't find where the function/method is defined, ie where the "contents" of it are.

Box2D is compiled into a "statically linked library", meaning that when Box2D is compiled a .lib file is created. This .lib does nothing on its own, it needs to be linked into another project (yours) which calls functions/methods inside it. The .lib will become a part of the project's final .exe so you won't need the .lib after that. (Another way is to use a Dynamically Linked Library, but Box2D isn't configured for that.)

The compiler and linker will however need to know what's inside the library when compiling a project which uses it. For this we use a header (Box2D.h) containing all the definitions of the functions/methods/etc in the library.
This is also included in the sources (include folder) so you shouldn't have to create it yourself. It doesn't need to be included into your project, neither does the sources, it's usually easier to just keep them in an easy to remember location.

If you don't have a .lib file you need to open the Box2D solution and compile the Engine project in it. This should generate the .lib. (The TestBed, freeglut, glui and HelloWorld projects are not required by Box2D itself.)

All you have to do to your source to include Box2D is #include "Box2D.h" in your main.cpp.

To get it to actually compile/link you need to tell the compiler where it can find the header, and the linker where it can find the library. You can either do this globally in Visual Studio's preferences, or for an individual project at it's (very similar) preference window. Visual Studio won't however know exactly which .lib file it should include from the path you specified, so you need to add box2d.lib (just the name, not path) as a dependancy or "additional library" in your project's linker settings. VS will then know that it should start looking for box2d.lib when linking, find it in the include/library path you specified, and notice that it contains the definitions of the functions/methods from Box2D.h.


Sorry if this is all a bit confusing at first, I tried to keep it fairly simple but yet detailed enough to not leave you with lots of unanswered questions at every step...


Top
 Profile  
 
PostPosted: Tue Sep 23, 2008 12:34 pm 
Offline

Joined: Tue Sep 23, 2008 11:46 am
Posts: 8
Nevermind, I got it all working thanks to you :D


Last edited by Overv on Tue Sep 23, 2008 12:56 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Sep 23, 2008 12:50 pm 
Offline

Joined: Wed Dec 19, 2007 1:19 pm
Posts: 34
Click C/C++ to set the Compiler path (to where the Box2D header is), somewhere at the top there's a list to which you can add entries. I'm on Linux and don't remember exactly what it looks like.

Click Linker to set the paths for the linker (folder where the .lib is), works the same as the list mentioned above.
There is also a field for "additional dependancies" or similar there. Just type in "box2d.lib".


Top
 Profile  
 
PostPosted: Sat Jun 05, 2010 12:36 am 
Offline

Joined: Sun May 23, 2010 12:15 am
Posts: 3
Hi! I just wanted to say thanks to TwoD, your post was very helpful to me!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group