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.

Advertisements