How to compile Box2D in Linux?

后端 未结 3 627
暖寄归人
暖寄归人 2020-12-21 06:27

Compiling the Box2d Tesbed is supposed to be simple:

from iforce2d:

Download the Box2D source code archive from here. If you want to use the t

3条回答
  •  清歌不尽
    2020-12-21 07:13

    Ubuntu 17.10 Testbed

    The major annoyance is that you currently need premake5, which is yet in alpha and therefore not in Ubuntu:

    cd
    git clone https://github.com/premake/premake-core
    cd premake-core
    git checkout v5.0.0-alpha12
    make -f Bootstrap.mak linux
    
    cd
    git clone https://github.com/erincatto/Box2D
    cd Box2D
    git checkout f655c603ba9d83f07fc566d38d2654ba35739102
    cd Box2D
    ~/premake-core/bin/release/premake5 gmake
    cd Build/gmake
    make
    # Must be run from there because of a ttf font is at that reative path. GDB told me that. :-)
    cd ../../Testbed
    ../Build/gmake/bin/Debug/Testbed
    

    This builts the .a static library under Build/gmake/bin/Debug.

    https://github.com/erincatto/Box2D/issues/387#issuecomment-219168623 gives some insight on the chaotic history of the build system.

    First CMake was used, but Erin though premake was better, then premake lost support and the project stuck to Visual Studio + Xcode config files, then the premake project came back from the dead and was reinstated. When will they switch to CMake, which is infinitely portable, and will be forever supported? :-)

    Shared library

    The steps are the exact same, but first clean up the old static binaries:

    git clean -xdf :/
    

    and then before running premake5, apply the following patch to Box2D:

    diff --git a/Box2D/premake5.lua b/Box2D/premake5.lua
    index b937866..f666651 100644
    --- a/Box2D/premake5.lua
    +++ b/Box2D/premake5.lua
    @@ -23,7 +23,7 @@ workspace "Box2D"
            buildoptions { "-std=c++11" }
    
     project "Box2D"
    -   kind "StaticLib"
    +   kind "SharedLib"
        language "C++"
        files { "Box2D/**.h", "Box2D/**.cpp" }
        includedirs { "." }
    

    The testbed still runs as before, but if you move the shared library it stops working as expected.

    TODO clean system-wide installation. Manually copying the .so and headers into appropriate paths should work... but not much fun.

    CMake revived

    So simple, so much better.

    Box2D/Box2D/CMakeLists.txt:

    cmake_minimum_required(VERSION 3.0)
    set (CMAKE_CXX_STANDARD 11)
    file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "Box2D/*.cpp")
    include_directories(${CMAKE_SOURCE_DIR})
    add_library(Box2D SHARED ${SOURCES})
    
    target_include_directories(Box2D PUBLIC ${CMAKE_SOURCE_DIR})
    set(HELLO_SOURCES HelloWorld/HelloWorld.cpp)
    add_executable(hello ${HELLO_SOURCES})
    target_link_libraries(hello PRIVATE Box2D)
    

    Then:

    cd Box2D/Box2D
    mkdir build
    cd build
    cmake ..
    make
    ./hello
    

    For now, I'll be tracking Box2D as a submodule and using this method for my projects I think, here is an example: https://github.com/cirosantilli/sdl-box2d

    find compile static library

    This might also be useful if you don't care about OS, and don't want to install premake:

    cd Box2D/Box2D/Box2D
    find . -iname '*.cpp' | xargs g++ -c -I../
    ar rcs libBox2D.a *.o
    g++ -I.. ../HelloWorld/HelloWorld.cpp libBox2D.a
    ./a.out
    

提交回复
热议问题