如何将 Bullet 物理库添加到 C++ 程序中

How to add the Bullet physics library to a c++ program

我已经编译了 https://github.com/bulletphysics/bullet3 的库并测试了一些示例,但我似乎无法编译这个简单的程序。

#include <btBulletDynamicsCommon.h>

int main(){
}

我注意到头文件位于 build_cmake/src 目录中,所以我将它包含在 -I 中,这有效,但后来我遇到了链接错误,所以我找到了 .so 文件并将它们链接到-L 选项和一些消除了链接错误的 glob,但我仍然收到警告。以下命令给了我一个可执行文件:

g++ -Wall main.cpp -I bullet3/src/ -L bullet3/build_cmake/src/*/*.so

但有以下警告:

In file included from bullet3/src/btBulletDynamicsCommon.h:38:0,
                 from main.cpp:3:
bullet3/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h: In constructor ‘btSISolverSingleIterationData::btSISolverSingleIterationData(btAlignedObjectArray<btSolverBody>&, btConstraintArray&, btConstraintArray&, btConstraintArray&, btConstraintArray&, btAlignedObjectArray<int>&, btAlignedObjectArray<int>&, btAlignedObjectArray<int>&, btAlignedObjectArray<btTypedConstraint::btConstraintInfo1>&, btScalar (*&)(btSolverBody&, btSolverBody&, const btSolverConstraint&), btScalar (*&)(btSolverBody&, btSolverBody&, const btSolverConstraint&), btScalar (*&)(btSolverBody&, btSolverBody&, const btSolverConstraint&), btAlignedObjectArray<int>&, long unsigned int&, int&, int&)’:
bullet3/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h:49:29: warning: ‘btSISolverSingleIterationData::m_kinematicBodyUniqueIdToSolverBodyTable’ will be initialized after [-Wreorder]
  btAlignedObjectArray<int>& m_kinematicBodyUniqueIdToSolverBodyTable;
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bullet3/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h:44:17: warning:   ‘long unsigned int& btSISolverSingleIterationData::m_seed’ [-Wreorder]
  unsigned long& m_seed;
                 ^~~~~~
bullet3/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h:57:2: warning:   when initialized here [-Wreorder]
  btSISolverSingleIterationData(btAlignedObjectArray<btSolverBody>& tmpSolverBodyPool,
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

并且当我 运行 可执行文件时出现错误:

./a.out: error while loading shared libraries: libBullet3Dynamics.so.2.88: cannot open shared object file: No such file or directory

我试图将文件添加到我的 LD 路径:

export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Common/libBullet3Common.so.2.88:$LD_LIBRARY_PATH

但是得到了同样的错误。

我好像把这个复杂化了,但我似乎无法在网上找到任何有关如何编译这样的程序的示例...

编辑:

我正在使用 Debian Linux。

编辑 2:

ldd 的输出:

linux-vdso.so.1 (0x00007ffdecb04000)
libBullet3Common.so.2.88 => /home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Common/libBullet3Common.so.2.88 (0x00007fd0246e7000)
libBullet3Dynamics.so.2.88 => not found
libBullet3Geometry.so.2.88 => not found
libBullet3OpenCL_clew.so.2.88 => not found
libBulletCollision.so.2.88 => not found
libBulletDynamics.so.2.88 => not found
libBulletInverseDynamics.so.2.88 => not found
libBulletSoftBody.so.2.88 => not found
libLinearMath.so.2.88 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd024365000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd024061000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd023e4a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd023aab000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd024aee000)

看起来不错,但是 LD_LIBRARY_PATH 应该指向目录,而不是库文件本身。

所以尝试将 LD_LIBRARY_PATH 命令更改为:

export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Common/:$LD_LIBRARY_PATH

编辑后:

所以查看 ldd 的输出,它表明它找不到它需要的几个库。

确保这些都可以在您的 LD_LIBRARY_PATH

上找到

感谢 ddd4 帮助我处理导出内容。这些是我必须用来为将来遇到此问题的任何人设置正确路径的命令:

export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Common:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Dynamics:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3Geometry:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3OpenCL:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/Bullet3OpenCL_clew:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/BulletCollision:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/BulletDynamics:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/BulletInverseDynamics:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/BulletSoftBody:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/sam/code/bullet/bullet3/build_cmake/src/LinearMath:$LD_LIBRARY_PATH