Building on Linux

Ugh. Been fighting with this for at least an hour now, but I think I've finally found a solution.

First install cmake and grab the latest version of Box2D. Then create a file called CMakeLists.txt in box2d-read-only/Box2D/Box2D. Then cd in there and cmake .;make

Code:
cmake_minimum_required(VERSION 2.6)
project(Box2D)
file(GLOB_RECURSE COLLISION ./Collision/*.cpp)
file(GLOB_RECURSE COMMON ./Common/*.cpp)
file(GLOB_RECURSE DYNAMICS ./Dynamics/*.cpp)
include_directories(../)
add_library(Box2D STATIC ${COLLISION} ${COMMON} ${DYNAMICS})


Took me forever to figure out how to do recursive globs, but then cmake generates a few of its own .cpp files for some reason, which get included in the glob unless you choose the folders specifically... so I don't know how to do this with a single glob.

Anywho, this should generate libBox2D.a, which you can theoretically use in your own projects, except I haven't got that far yet.

Edit: Yeah, it works great.
Added some install commands to the cmake file: it will create a make install target, which copies the header files and the compiled library to the appropiate locations. Also, if pkg-config is found, a .pc file will be added, so box2d can easily be linked via `pkg-config --cflags --libs box2d`.

Code:
cmake_minimum_required(VERSION 2.6)

project(Box2D)

set(SRCDIR ../../Box2D)

file(GLOB_RECURSE SRC ${SRCDIR}/*.cpp)

include_directories(${SRCDIR}/..)

add_library(box2d STATIC ${SRC})

install(DIRECTORY ${SRCDIR}/ DESTINATION include/Box2D
         FILES_MATCHING PATTERN "*.h"
    PATTERN ".*" EXCLUDE
    PATTERN "Box2D.h" EXCLUDE
    )

install(FILES ${SRCDIR}/Box2D.h DESTINATION include)

install(TARGETS box2d ARCHIVE DESTINATION lib)

find_package(PkgConfig)

if(PKG_CONFIG_FOUND)

   configure_file(box2d.pc.in box2d.pc @ONLY )
   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/box2d.pc DESTINATION $ENV{PKG_CONFIG_PATH})
   
endif(PKG_CONFIG_FOUND)


Usage:

  • Extract the attached file to Box2d/Build, it has a cmake directory with CMakeLists.txt and box2d.pc.in in it.
  • Inside this directory, run cmake .
  • Then make
  • Then (as root) make install

I only had the chance to test it on my Arch machine, but it should work in any other distros.
Much better. I wanted to add something like that, but didn't know how.

Original thread: https://box2d.org/forum/viewtopic.php?t=3534