CabaIT CabaIT - 2 months ago 20
C++ Question

Use Bullet in my c++ project in Linux

I want to use the Bullet library only for collision detection between two convex hulls. My problem is how to link the library in my project and specify that in the CMakeLists.txt.

I don't really understand how to install the Bullet library. I cloned the repo in a folder, created a build folder, compiled it with cmake and installed it. I tried to link the library to my project by adding in the CMakeLists.txt:

find_package( Bullet REQUIRED )
include_directories(${BULLET_INCLUDE_DIR} )

LINK_LIBRARIES(
BulletDynamics BulletCollision
)


This works fine also when I add the headers in my projects. But when I use some function of the Bullet library, I get the following compilation error:

/usr/bin/ld: /usr/local/lib/libBulletDynamics.a(btTypedConstraint.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC


I tried to google it in order to resolve the problem but I was not able to resolve it (My knowledge of CMake and this stuff is very basic).
Also I found somewhere that the library should be included in the project and then compiled, so my project structure is this one:

/trunk
CMakeLists.txt
Findmy_project.cmake
/bin
/build
/src
/examples
CMakeLists.txt
test.cpp
my_algorithm.cpp
my_algorithm.h
CMakeLists.txt


where
test.cpp
is a source file that uses a class defined in
my_algorithm.h
, but I have no idea how to link the Bullet library to the project now.

So my question is in summary: How do I have to link, and where to install, the Bullet library to use it in my c++ project?

Answer

You need to compile Bullet library with -fPIC, as linker's error message says.

Either configure this library with CMake variable BUILD_SHARED_LIBS set:

cmake -DBUILD_SHARED_LIBS=on <source-dir>

or with CMake variable CMAKE_POSITION_INDEPENDENT_CODE set:

cmake -DCMAKE_POSITION_INDEPENDENT_CODE=on <source-dir>

The first way you will get shared libraries, which always have -fPIC flag enabled, the second way you will compile libraries statically, but with given flag explicitely set.

Comments