gpt4 book ai didi

c++ - 如何正确编译box2d helloworld

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:27 26 4
gpt4 key购买 nike

我回到了 c++(我已经好几年没用过它了)来学习 box2d API。

我确切地说我在 Linux 系统 (Ubuntu) 上并且我已经从 this guide 之后的源代码安装了 box2d(2.3.0) .

因此,我尝试编译包含 helloworld 代码的 box2d,但在 b2world 实例上出现错误。

undefined reference to b2world

There is the complete file .

我尝试了一个更基本的测试,但结果是一样的:

#include <iostream>
#include <Box2D/Box2D.h>

using namespace std;

int main()
{
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);

cout << "Hello world!" << endl;
return 0;
}

我没有错误,包括 box2d.h,使用 b2Vec2 也没有错误,所以我认为 box2d 在我的电脑上安装正确。

并且我已经验证了 b2World 是在 box2d.h 中指定的(它是原始的)。

我尝试使用 g++ 命令和 IDE Qt Creator 进行编译,但没有人成功。

非常感谢任何帮助或建议。

最佳答案

问题是我没有指定 box2D 库的路径。

因此,我按照以下步骤成功编译并执行了我的 hello world 小程序:

1 ) 将库复制并粘贴到我的项目文件夹中(我将其放入 lib 目录中)。2 ) 将 lib 目录的路径添加到 $LD_LIBRARY_PATH :

export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH

3)用g++编译并指定使用box2D库:

g++ main.cpp -o test -l Box2D

关于c++ - 如何正确编译box2d helloworld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com