Bullet Physics
Loading...
Searching...
No Matches
Building

Intro

https://stackoverflow.com/questions/10358745/how-to-use-libraries

Linux

Using Bullet Directly

If you want to use bullet for the purpose of installing it on your computer from source, or to try out the examples through the example browser, then you want to build the project directly.

A simple way to do this is with cmake, clone the bullet repository, cd into the root of it and run

mkdir my_build
cmake -S . -B my_build
cd my_build
cmake --build .

Using Bullet as a Library

Alternatively you might want to use Bullet as a library in your own programs, for example if you're desinging a game and you want to add physics.

Start in an empty directory and create the directory external_libraries or something along those lines, inside of this directory add bullet as a submodule, if you're not yet familiar with how submodules work, to sum it up they allow you to add another directories as versioned packages and git will not store the files directly. If you don't want to use submodules now, just clone the project here.

Note that bal.cpp is using https://github.com/bulletphysics/bullet3/blob/master/examples/HelloWorld/HelloWorld.cpp as it's source code.

For simplicity we will assume you are running linux and using cmake, in this case we just have to set up the following CMakeLists.txt file like so:

cmake_minimum_required(VERSION 3.10)
project(bullet_as_library)
include_directories(external_libraries/bullet3/src) # (1)
add_subdirectory(external_libraries/bullet3) # (2)
add_executable(bullet_as_library bal.cpp)
target_link_libraries(bullet_as_library BulletDynamics BulletCollision LinearMath) # (3)

This won't be a tutorial on how cmake works, but we'll mention a few things:

  1. adds the src directory as the include directory, this means that when you write something like #include "btBulletDynamicsCommon.h" cpp knows where to find this file.
  2. tells cmake to also build bullet when we build this directory
  3. tells the linker to link the following cmake targets against our project

If you're curious about how I knew what the names were for those libraries, look at the structure of the src file in Bullet, you'll find directories with similar names, each of which contain a CMakeLists.txt file and inside you'll find a line like this ADD_LIBRARY(BulletDynamics ${BulletDynamics_SRCS} ${BulletDynamics_HDRS}) which defines that as a cmake target for us to reference later on. Also note that the order matters for some reason.

Todo
I believe it has to do with this: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc, but when we know the real reason add the explanation to the end of this paragraph.

With that set up, you can now move on to the basics section, in the root of your project dir you can now run cmake -S . -B build and cmake --build build to compile your code.

Building What You need

You might have noticed that during your build process we built many things, including tests and examples, there are a few reasons you might not want to do this.

First off, while the above example works fine, if you're bringing bullet into an existing codebase, you may run into errors when compiling the examples along with your code because bullet provides third party libraries in the examples, which might overwrite packages that you have, for example I had a glfw project that used glad, and in the examples it also provided an instance of glad, but I got an error saying it that the gladLoader function call was undefined.

Moreover, there is no reason to build examples or to do tests if your goal is to simply use bullet as a third party library as it unessesarily complicates the build and takes longer.

At the moment when we run add_submodule(external_libraries/bullet3/src) it will recursively add any other directories that also contain CMakeLists.txt in them, and if you take a look at the code base you will find many such instances. Therefore an easy way to not build these cmake targets is by renaming the CMakeLists.txt to something like ignore_CMakeLists.txt, then go into the examples and test directory and do that to both CMakeLists.txt you encounter, when you build the library alongside your project you will not build the extraneous targets.

Specifically if you're using the cmake file provided above, then you can do the following:

  • To disable the building of examples rename bullet3/examples/CMakeLists.txt to bullet3/examples/CMakeLists.txt_ignored
  • To disable the building of tests rename bullet3/test/CMakeLists.txt to bullet3/test/CMakeLists.txt_ignored

    Todo
    This method of doing things is a little bit ad hoc, maybe we can find a better way of doing it by changing the cmake file instead. If anyone has any ideas let us know