DEV Community

Ng Chen Hon
Ng Chen Hon

Posted on

Compiling Box2D from source for Codeblock using mingw and cmake

Compiling Box2D Static Lib

1. Getting the sauce

Get the latest Box2D release from github link as the zip form.

Unzip it and put into any directory you like.

2. Modifying some stuff

Open Box2D > CMakeLists.txt, as below :

alt text

Then remove this few lines including glui, freeglut and test bed into the compilation, so that you won't need to add them in, as below :

alt text

Then save it.

Then head to Box2D > Box2D > CMakeLists.txt, add this line on top :

cmake_minimum_required(VERSION 3.10)

Enter fullscreen mode Exit fullscreen mode

You can change to your version of cmake also.

alt text

3. Configure Cmake and generating

Open cmake-gui and set the source and target path, following your box2d directory :
alt text

Then, click Configure.

Select CodeBlocks - MinGW Makefiles .
alt text

Click okay, then you might see something like this and some errors :
alt text

Just ignore them and click Configure again. You should see it getting configuration done like this :

alt text

If its done, just click Generate there. A Generating done will be logged out.

4. Compiling

Then go to Box2D > Build,

Spawn a terminal there and run mingw32-make

alt text

Wait until it finished running.

Then go to Box2D > Build > Box2D, you will find libBox2D.a there, as below :

alt text

5. Using it with CodeBlock

  1. Put libBox2D.a into MinGW > lib.
  2. Copy Box2D > Box2D, the Box2D folder inside root Box2D directory into `MinGW > include'.
  3. Then in CodeBlock, open Settings > Compiler > Linker Settings > Add Library and Select MinGW > lib > libBox2D.a.

6. Setup is done :)

Test it out, here is a test cpp from my uni course gist.

7. Why am I doing this

I am just merely trying to challenge myself to compile it and not following instructions.

8. Test ?

I tested the compiled lib with a uni provided cpp along with sfml, somehow it works without glui and freeglut.

Enter fullscreen mode Exit fullscreen mode

Discussion (0)