Building
From Box2D Wiki
Contents |
Building Box2D
Note: this page will tell you how to compile the C++ version of Box2D. If you are trying to compile a port of the project, see the ports page for instructions specific to your version.
Once you have downloaded the Box2D source, the easiest way to build Box2D is to use an IDE and one of the project files in the Build directory, depending on your operating system:
- Windows: Microsoft Visual C++ (Visual C++ 2008 Express is available as a free download with registration) or Code::Blocks
- Linux: compiling with make, Code::Blocks
- Mac OS X: XCode
If you don't use one of these IDEs, you can still likely build the project using CMake scripts to generate makefiles. Under Windows, you can easily build the library itself (though not the testbed) with MinGW.
If you obtained the source files from the Subversion repository instead of a numbered release, some files may not be included correctly unless you use the Visual C++ project files, as the other project files are maintained as user submissions and do not get updated as often.
Visual C++ 2008 instructions
Step One: Build the Library
Extract the library. Go to Box2D_v2.0.1\Box2D\Build\VC8 and open the Visual Studio Solution file you find there. Then press F7 to build the solution. Check Box2D_v2.0.1\Box2D\Library, you should notice the library box2d_d.lib that was generated from building the solution.
Step Two: Set Directories
Click Tools>Options Projects and Solutions > VC++ Directories > Show directories for: Library files
Add a new folder and select your Box2D\Library folder.
Do the same for the include folder under the Include tab.
Step Three: Creating your project and link the library
File>New>Project>Visual C++> Win32 > Win32 Console Application Click the Empty Project check box and select Finish.
Now click Project>Properties>Configuration Properties>Linker>Input and click on Additional Dependencies
Type: 'box2d_d.lib'
Now right-click on Source Files and then Add Existing Item and add the HelloWorld.cpp file found in the example folder. Press F5! Congrats, your first Box2D project is up and running!
From now on you can skip steps 1 and 2 you only need to do step 3 whenever you make a new project.
Code::Blocks instructions
Step One: Open the Project
Go to Box2D\Build\CodeBlocks and execute the Box2D.workspace in the CodeBlocks program, which you will need to download and install.
Step Two: Compile the Project
Now you can compile the whole workspace to create the library (.a file). It's necessary to compile the whole workspace because of the dependencies (FreeGlut, GLUI), but you can individually compile any other project after having compiled the whole workspace once.
Step Three: Run the Examples
Now you can Run the examples projects (HelloWorld or TestBed)
After checking out the tests you can make sure that in Settings/Compiler and Debugger/Search Directories the include directory Box2D\Include is listed. Also, the Linker directory is set to Box2D\Library, like in VS2005/2008.
If you still have problems, feel free to post in the forums.
XCode instructions
Step One: Open Xcode Project
Extract and open the Box2d directory.
Open the Xcode project ./Build/Xcode/Box2d.xcodeproj in Xcode.
Step Two: Build Targets
Use the drop down menu box to select a build target.
Select Build > Build (command-B) to build.
You can see which targets have been built by looking in the 'Products' group. Targets which have not been built are in red.
Select and build all the targets one at a time.
How to Build all Targets in a Single Step
It is also possible to create an aggregate target which will group all the targets and build them together.
To create an aggregate target:
Choose Project > New Target.
Select Aggregate from the New Target Assistant.
To add targets to the aggregate target:
Select the new Aggregate Target
Select File > Get Info (command I)
In the General pane, add the other targets to Direct Dependancies
You can now build all the targets in a single step by building the aggregate target
Building for iPhone
Box2d and the sample project are C++. For information on integrating with Objective-C projects see Building for iPhone.
Mac Command Line
Here's what Mark Harrison did to get box2d to compile with the makefiles included with the distribution.
1. Install and build freeglut.
2. Make this patch. Substitute the /Users/mh/box2d with the path where you've installed.
3. Box2D will now build. Run under a Terminal window from X11.
4. I'm sure the hard-coded paths can be fixed to use relative paths. I'll work on that after I get a bit more into Box2D, so feel free to fix it up!
mh@superfly ~/box2d --> for i in `find . -name '*-orig'`;do echo ============ $i `dirname $i`/`basename $i -orig`;diff $i `dirname $i`/`basename $i -orig`;done ============ ./src/Box2D_v2.0.1/Box2D/Contrib/freeglut/Makefile-orig ./src/Box2D_v2.0.1/Box2D/Contrib/freeglut/Makefile 3c3 < CFLAGS= -g -O2 -DTARGET_HOST_UNIX_X11 -DHAVE_FCNTL_H -DHAVE_SYS_IOCTL_H --- > CFLAGS= -g -O2 -DTARGET_HOST_UNIX_X11 -DHAVE_FCNTL_H -DHAVE_SYS_IOCTL_H -I/Users/mh/box2d/src/Box2D_v2.0.1/Box2D/Contrib/freeglut -I/usr/X11R6/include ============ ./src/Box2D_v2.0.1/Box2D/Contrib/glui/Makefile-orig ./src/Box2D_v2.0.1/Box2D/Contrib/glui/Makefile 3c3 < CXXFLAGS= -g -O2 -I../freeglut -L../freeglut -DGLUI_FREEGLUT --- > CXXFLAGS= -I/usr/X11R6/include -I/Users/mh/box2d/src/Box2D_v2.0.1/Box2D/Contrib/freeglut -O2 -I../freeglut -L../freeglut -DGLUI_FREEGLUT ============ ./src/Box2D_v2.0.1/Box2D/Examples/TestBed/Makefile-orig ./src/Box2D_v2.0.1/Box2D/Examples/TestBed/Makefile 5c5 < CXXFLAGS= -g -O2 -I$(PROJECT)/Contrib -I$(PROJECT)/Include -DGLUI_FREEGLUT --- > CXXFLAGS= -g -O2 -I/usr/X11R6/include -I/Users/mh/box2d/src/Box2D_v2.0.1/Box2D/Contrib/freeglut -I$(PROJECT)/Contrib -I$(PROJECT)/Include -DGLUI_FREEGLUT 7c7 < LDFLAGS= -L$(PROJECT)/Contrib/freeglut -L$(PROJECT)/Contrib/glui \ --- > LDFLAGS= -L/usr/X11R6/lib -L$(PROJECT)/Contrib/freeglut -L$(PROJECT)/Contrib/glui -bind_at_load 41c41 < g++ -o $@ $^ $(LDFLAGS) -L$(PROJECT)/Source/Gen/float -lbox2d -lglui -lglut -lGLU -lGL --- > g++ -o $@ $^ $(LDFLAGS) -L/Users/mh/box2d/lib -L$(PROJECT)/Source/Gen/float -L/Users/mh/box2d/src/Box2D_v2.0.1/Box2D/Contrib/freeglut -lbox2d -lglui -lglut -lGLU -lGL -lX11 56c56 < g++ -rdynamic -o $@ $^ $(LDFLAGS) -L$(PROJECT)/Source/Gen/fixed -lbox2d -lglui -lglut -lGLU -lGL --- > g++ -o $@ $^ $(LDFLAGS) -L/Users/mh/box2d/lib -L$(PROJECT)/Source/Gen/fixed -lbox2d -lglui -lglut -lGLU -lGL -lX11 ============ ./src/freeglut-2.4.0/src/freeglut_joystick.c-orig ./src/freeglut-2.4.0/src/freeglut_joystick.c 1042d1041 < int i; 1063d1061 < int counter;
CMake instructions
To compile Box2D v2.0.1, you have to retreive this version of Box2D : http://www.box2d.org/forum/download/file.php?id=365
Extract it :
tar xfvj Box2D_v2.0.1-cmake.tar.bz2
Get into it and run 'cmake .':
$ cd Box2D_v2.0.1-cmake/Box2D/
$ cmake .
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
...
-- Found X11: /usr/lib/libX11.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/drayan/dev/Box2d/Box2D_v2.0.1-cmake/Box2D
Then run make:
$ make
Scanning dependencies of target Box2D
[ 2%] Building CXX object Source/CMakeFiles/Box2D.dir/Common/b2StackAllocator.cpp.o
...
[100%] Building CXX object Examples/CMakeFiles/framework.dir/TestBed/Tests/TestEntries.cpp.o
Linking CXX executable framework
[100%] Built target framework
The compilation is done. You can now execute the framework example :
$ cd Examples/
$ ./framework
Building Instructions
|










