如何在 Linux 中编译 Box2D?
编译 Box2d Tesbed 应该很简单:
Compiling the Box2d Tesbed is supposed to be simple:
来自 iforce2d:
从这里下载 Box2D 源代码存档.如果想一路使用终端,也可以这样做(如果wget不可用,使用yum安装):
Download the Box2D source code archive from here. If you want to use the terminal all the way, you could also do this (if wget is not available, use yum to install it):
wget http://box2d.googlecode.com/files/Box2D_v2.1.2.压缩包
使用以下命令解压并构建它.[...]
Use the following commands to unzip and build it. [...]
解压 Box2D_v2.1.2.zipcd Box2D_v2.1.2/Box2D/Buildcmake ..制作
(这些说明已经很旧了,我确实通过 git clone https://github.com/erincatto/Box2D.git
获得了我的源代码)
( These instructions are pretty old, I did get my source with git clone https://github.com/erincatto/Box2D.git
)
在新克隆的目录中从 Box2D/Build
运行 cmake ..
会导致多个错误:
Running cmake ..
from Box2D/Build
in the freshly cloned directory causes multiple errors :
CMake Error at Testbed/CMakeLists.txt:84 (add_executable):
Cannot find source file:
Framework/imgui.h
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error: Cannot determine link language for target "glfw".
CMake Error: CMake can not determine linker language for target: glfw
当然,make
失败了:
[ 42%] Building CXX object Box2D/CMakeFiles/Box2D.dir/Dynamics/b2Body.cpp.o
/home/cabri/Documents/Box2D/Box2D/Box2D/Dynamics/b2Body.cpp: In member function ‘void b2Body::DestroyFixture(b2Fixture*)’:
/home/cabri/Documents/Box2D/Box2D/Box2D/Dynamics/b2Body.cpp:216:17: error: ‘nullptr’ was not declared in this scope
if (fixture == nullptr)
^
Box2D/CMakeFiles/Box2D.dir/build.make:566: recipe for target 'Box2D/CMakeFiles/Box2D.dir/Dynamics/b2Body.cpp.o' failed
make[2]: *** [Box2D/CMakeFiles/Box2D.dir/Dynamics/b2Body.cpp.o] Error 1
CMakeFiles/Makefile2:85: recipe for target 'Box2D/CMakeFiles/Box2D.dir/all' failed
make[1]: *** [Box2D/CMakeFiles/Box2D.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
在这些网站上可以找到多个类似的问题,但都没有答案.我知道我可以安装 box2dsudo apt-get install libox2d
但我也想要测试平台.
Multiple similar questions can be found on these sites, but none has an answer. I know I can install box2d with
sudo apt-get install libox2d
but I'd like to have the testbed as well.
这怎么办?
推荐答案
如果您有来自 的最新提交Box2D repo 签出,可以通过在repository目录下运行这个git命令来恢复原来的CMake文件:
If you have the most recent commit from the Box2D repo checked out, you can restore the original CMake files by running this git command in the repository directory:
git checkout 05ee3c3c22af9ac1e5d88061d0b473f814c8210f^
Box2D/Box2D/Box2DConfig.cmake.in
Box2D/Box2D/CMakeLists.txt
Box2D/Box2D/UseBox2D.cmake
Box2D/CMakeLists.txt
Box2D/HelloWorld/CMakeLists.txt
Box2D/Testbed/CMakeLists.txt
Box2D/glew/CMakeLists.txt
Box2D/glfw/CMakeLists.txt
自从这次提交后 Box2D 已经开始使用 C++11 特性,所以你需要在 Box2D/CMakeLists.txt
中添加这一行:
Since Box2D has started using C++11 features since this commit, so you will need to add this line to Box2D/CMakeLists.txt
:
set (CMAKE_CXX_STANDARD 11)
如果你只想构建核心库,你就大功告成了;只需运行以下命令:
If you only want the core library built, you're done; just run the following commands:
mkdir build
cd build
cmake -D BOX2D_BUILD_EXAMPLES=OFF ../Box2D
测试平台
如果您想要测试台,事情会变得更复杂.Box2D 的 GLFW 副本的 CMakeLists 似乎完全损坏了.打开Box2D/CMakeLists.txt
,找到这两行:
add_subdirectory(glew)
add_subdirectory(glfw)
将它们替换为以下内容.这将导致构建使用您系统版本的库,因此您需要安装它们:
Replace them with the following. This will cause the build to use your system versions of the libraries, so you will need to have them installed:
find_package(GLEW REQUIRED)
if (GLEW_FOUND)
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})
endif()
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
打开Box2D/Testbed/CMakeLists.txt
.在文件顶部的set(Testbed_Framework_SRCS
下,删除4行引用imgui和RenderGL3.在该部分添加以下行:
Open Box2D/Testbed/CMakeLists.txt
. At the top of the file, under set(Testbed_Framework_SRCS
, remove the 4 lines referring to imgui and RenderGL3. Add the following lines in that section:
../imgui/imgui.h
../imgui/imgui.cpp
../imgui/imgui_draw.cpp
../imgui/imgui_impl_glfw_gl3.cpp
滚动到底部并将 glew
和 glfw
行替换为:
Scroll to the bottom and replace the glew
and glfw
lines with:
${GLEW_LIBRARIES}
${GLFW_STATIC_LIBRARIES}
最后替换
file(COPY ../Build/Data DESTINATION ..)
与
file(COPY ./Data DESTINATION .)
此时,完整的库和测试平台应该是可构建的:
At this point, the full library and testbed should be buildable:
mkdir build
cd build
cmake ../Box2D
相关文章