Box2D Library in Visual Studio (C++)

Note: This tutorial is for very beginners, so, you might find some details redundant.

Downloading the necessary stuff:

First of all open the official website of Box2D http://www.box2d.org/, which should look like the image below:

click Download (as shown in the above image),  and you will be sent to http://code.google.com/p/box2d/:

click Download (as shown in the image above), and you will reach:

Download the zip-file of the latest version of Box2D. At the time of making this tutorial the latest version was v2.1.2, so, I downloaded it (shown in image above). After the download is complete extract the folder from the zip file somewhere (I extracted it on the desktop).

Now, go to http://www.cmake.org/:

and click the download button (show in above image), which will take you to:

Download zip-file of the latest version of cmake (at the time the tutorial was made, the latest version was v2.8.4 as shown in the image above).

Extract the zip-file somewhere (I extracted it on the desktop).

Now open the cmake extracted folder, goto “bin”, and run “cmake-gui.exe” (highlighted in the image above). You should see the program as shown in the image below:

Building the library:

You need to set the “Box2D” directory from the “Browse Source” button, and “Box2D/Build” directory from the “Browse Build”. In my case, the extracted directory “Box2D_v2.1.2″ was on the desktop, so the directories set were as follows:

After setting the directories, click “Configure” button. You will see the dialog as follows:

Choose the version of visual studio you are using (In my case, it is Visual C++ Express 2010 (v 10) , and click “Finish” button.

After configuration is done, you will see selection in red:

click the “Configure” button once again (and red selection will turn white):

Now, click “Generate” button,

After generating is done, goto extracted box2D build folder (in my case it is “Box2D_v2.1.2/Box2D/Build/” on the desktop):

open Box2D.sln from there (shown in above figure).

A dialog box will come up:

choose your version of Visual Studio for C++ (I use Visual C++ Express 2010), and click “OK” button.

Visual studio will open the Box2D solution.

Now, from the explorer, right-click the Box2D project and click Build (shown in above image).  This will start building the Box2D project library.

After the building is complete (shown in above image), close current solution.

Creating new project:

To close the solution, go to “File” menu and click “Close Solution” (shown in above image).

Now, create a new project:

To create a new project, go to File>New>Project…

Select Win32 Console Application, type the name of the project (I typed “Box2D_Test”, shown above) and click OK. The reason I chose Win32 Console Application is just to show the the library in action, so you can, then, choose according to your own project.

Now, click Next.

In the additional options, check “Empty project”, and click Finish.

A new Win32 Empty Console Project has been created.

Now, right click the project in the solution explorer, and go to “Add” and click “Existing Items”. This opens up a browsing window with the current folder as being the location of your project files.

Setting up the library:

Create a new folder there, and name it “lib” where we will be putting Box2d library files.

Now copy the library files from the build folder (in my case, the files are located at “Box2D_v2.1.2/Box2D/Build/” on the desktop).

Now, come back to the Visual Studio where we had our browsing window left.

and paste the library files in the lib folder (shown in above image).

Now, go to the Box2D folder (in my case, it is located at “Box2D_v2.1.2/Box2D/” on the desktop),

and copy the “Box2D” folder in there.

Now, go back to the Visual Studio where we left,

and paste the copied “Box2D” folder in the directory where “lib” is saved.

Now, we will use pre-made, HelloWorld program which is located at the HelloWorld folder in Box2D_v2.1.2/Box2D directory.

Copy the “HelloWorld.cpp” file and paste it in the folder where we have our project files:

Select the “HelloWorld.cpp” and click “Add” button. The “HelloWorld.cpp” file is now visible in the solution explorer.

Now, we need to setup the library. Right-click the project and then click properties (shown in above image). This will bring up properties window:

Go to Configuration Properties > C/C++ > General. Type “./” in the Additional Include Directories Textbox.

Now, go to Configuration Properties > Linker > General, 

and then, type “./lib” in the Additional Library Directories Textbox.

Now go to Configuration Properties > Linker > Input,

Click the drop-down button of the Addition Dependencies Textbox (if you can’t see the button, assume the button is there and click the button-area, the button should be visible). Now, click <Edit…> which will bring up a window:

Type “Box2D.lib” in the blank text-area (as shown in the above image) and click OK.

Additional Dependencies Textbox, now, should have changed to “Box2D.lib; %(AdditionalDependencies)”. Click OK.

Testing the library:

Now, open “HelloWorld.cpp” from the solution explorer.

To open “HelloWorld.cpp”, double click the file “HelloWorld.cpp” in the solution explorer.

Now, we want to run our project.

Go to “Debug” menu and click “Start with Debugging”.

A dialog box would come up:

Click Yes.

After the project building is complete,

if you see a win32 console window (like the in the image below)

congrats, you are done!! You have successfully set up the Box2D library in Visual Studio (C++).

Note: This tutorial shows building the library and the program in debug mode. You may want to build them in release mode.

39 Responses to Box2D Library in Visual Studio (C++)

  1. iamcreasy says:

    Thanks you soo much! without this tutorial no one knows how long it might take me to setup box2d with VS.

    Cheers!

  2. Anonymous says:

    nice one! just one thing though i didnt get what you achieved by copying the source for box2d in your project folder?

  3. Anonymous says:

    oh ok that was a noob question i guess, thanks for the tutorial, its kick ass, planning to put some programming turtorial for using box2d with opengl , glut , ogre anything? Or can you point to some, people can learn how to hook up box2d physics to the rendering world?

    • aneelkkhatri says:

      Sure…, I would if time allows :)
      Have you tried Allegro library? It’s really easy to get started for game programming, so you will become familiar with the flow of games.

      Cheers!

  4. Anonymous says:

    Brilliant work! Easy to follow no mistakes ;)

  5. asda says:

    nice tutorial, but do i have to do this every time?

    • aneelkkhatri says:

      Nope! Once you have built the library, you can use it in other projects as well. Follow the steps that demonstrate copying the files .lib files and the Box2D folder in the project folder, and the project setting.
      However, if you don’t want to do even this for every project you create, you can put those files in the respective directories of Visual Studio, but this won’t make your project portable.

  6. bladelock says:

    Hello!!! Your tutorial has helped me a lot. I just have one question out of curiosity.

    during the parts when we need to add “./”, why do we need to add the period before the slash?
    what does the period signify?

    Cheers.

  7. Worked well :)
    One thing though… Arent you supposed to configure the release build as well though? I followed the same steps to configuring it, taking the care of renaming the debug library (and references) to Box2D_d.lib (and the pdb too) and all seems to be working well

  8. Stephen says:

    Hello
    Can you please help???
    I had troubles installing Box2D with my Visual Studio 10 application.
    I followed your tutorial to the word but at the end when I tried to run the Helloworld application all the Box2D commands/code was not recognisable.
    The only problem I encountered was when I open the box Box2D.sln file I did not get the Choose Application dialog box, but I continued through the tutorial and everything else went fine.

  9. Stephen says:

    Worked it out! I did’nt add the correct files.
    All is working well.
    Well done on the tutorial easy to follow.
    Thanks

  10. Sandi says:

    Thank you for the tutorial! The only difficulty I had was reading the ./ It was hard to tell at first if it was ../ or ./ or / but managed to get it!

  11. i love you. i’ve been searching for tutorial for 4 hours and you showed me everything i needed. love from ukraine <33

  12. jairou says:

    I did this installation on a computer with Windows Vista Home Premium (Service Pack 2) and it was ok but I tried to do it on a Windows 7 Home Premium (Service Pack 1), both of them with Visual Studio 10 Express Edition and it didn’t work I got this message: “Error in configuration process, project files may be invalid”

  13. Juan Pablo Giron says:

    Hello I am Juan Pablo From Colombia, I´d know whether there something tutorial that combines Qt with this amazing library??.. thank you. I need create a application similar to Angry Birds the game.

  14. Alberto says:

    great tutorial!

  15. Pingback: Setting everything up – Part 2 « The Autoblog

  16. Hi Thanks for the tutorial been very easy to follow, just one problem i had to download a service pack 1 for visual studios to get it all together, can you recommend any more tutorials which are as easy to use. Thanks Dennis

  17. Valentin says:

    last picture doesnt work :P

  18. Pingback: C++ Visual Studio 2012 Getting Box2D and SFML to work together | BlogoSfera

  19. Awesome blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your design. Bless you

  20. yxbenh says:

    Cheers for the awesome guide, I wasn’t too sure how to get Box2D working with Visual Studio. Now I do :D. Now, what’s the purpose of doing the CMake stuff?

    I opened the solution file with Visual Studio 2012 and just did a rebuild and then followed the part of your guide and created a new project and got the HelloWorld to compile and work.

    I’m also having some trouble find the Release folder after compiling it using Release configuration.

  21. tarot says:

    Hello, its pleasant piece of writing regarding media print,
    we all know media is a impressive source of data.

  22. I got this website from my friend who told me
    concerning this web page and now this time I am browsing this web page and reading very informative posts
    here.

  23. selver says:

    At the time of this comment, the newest Box2D is v2.2.1.
    The upper field in cmake_gui.exe should be “…\Box2D_v2.2.1″
    Just note for others, the folder in the upper field has to contains “CMakeLists.txt”, because I stuck here.
    Thank aneelkkhatri

  24. Muzna Khan says:

    It was a great tutorial :) but i dont have the .pdb file in BOX2D_v2.1.2<BOX2D<BUILD<BOX2D<DEBUG folder. It only has the .lib folder can you tell me which step or thing i am lacking.
    which i build the HelloWorld program , the screen doesnt gets on hold and i can see only some stuff written on the output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: