You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下编译C++代码遇Box2D未定义引用问题求助

Windows下编译C++代码Box2D未定义引用问题解决方案

今日在Windows系统下编译C++代码时,所有涉及Box2D的部分均出现未定义引用错误,以下是项目相关信息及报错:

项目信息

Makefile

linux: main.o
    g++ main.o -o main -O2 -Wall -lbox2d -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
    ./main

windows: 
    g++ -I../include -L../lib ../main.cpp -o main.exe -O2 -Wall -lraylib -lbox2d -lopengl32 -lgdi32 -lwinmm

main.o: ../main.cpp
    g++ -c ../main.cpp

cleanl:
    rm ./*.o ./main

cleanw:
    rm ./*.o ./main.exe

项目目录结构

Project/
├─ build/
│  ├─ Makefile
├─ include/
│  ├─ raylib.h
│  ├─ box2d/
│  │  ├─ *all box2D headers
├─ lib/
│  ├─ libbox2d.a
│  ├─ libraylib.a
├─ .gitignore
├─ main.cpp

报错信息

PS C:\Users\henri\Documents\Calyspo\build> g++ -I../include -L../lib ../main.cpp -o main.exe -O2 -Wall -lraylib -lbox2d -lopengl32 -lgdi32 -lwinmm
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x82): undefined reference to `b2World::b2World(b2Vec2 const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0xe0): undefined reference to `b2PolygonShape::b2PolygonShape()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x14f): undefined reference to `b2PolygonShape::SetAsBox(float, float)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x17e): undefined reference to `b2World::CreateBody(b2BodyDef const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x196): undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x1a3): undefined reference to `b2Body::SetFixedRotation(bool)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x1f1): undefined reference to `b2PolygonShape::SetAsBox(float, float)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x203): undefined reference to `b2World::CreateBody(b2BodyDef const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x210): undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x344): undefined reference to `b2World::Step(float, int, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x673): undefined reference to `b2World::~b2World()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\henri\AppData\Local\Temp\ccCw6djM.o:main.cpp:(.text.startup+0x6e2): undefined reference to `b2World::~b2World()'
collect2.exe: error: ld returned 1 exit status

解决方案

1. 调整链接库顺序

GCC链接时,库的顺序会影响符号解析,依赖其他库的库要放在前面。把-lbox2d移到-lraylib前面:

windows: 
    g++ -I../include -L../lib ../main.cpp -o main.exe -O2 -Wall -lbox2d -lraylib -lopengl32 -lgdi32 -lwinmm

2. 确认Box2D库的兼容性

  • 检查libbox2d.a是否是用**MinGW-w64(x86_64)**编译的,和你的编译器架构一致(你用的是x86_64-w64-mingw32)。如果是从其他平台(比如Linux)拷贝的库,会出现链接错误。
  • 若库不兼容,重新用MinGW-w64编译Box2D源码:
    1. 克隆Box2D仓库:git clone https://github.com/erincatto/Box2D.git
    2. 创建build目录并进入:mkdir build && cd build
    3. 用CMake配置:cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
    4. 编译:mingw32-make
    5. 把生成的libbox2d.a替换到项目的lib/目录下。

3. 确保头文件路径正确

确认main.cpp中Box2D的头文件引用是#include <box2d/box2d.h>(因为你的头文件在include/box2d/下),如果直接写#include <box2d.h>会找不到头文件,不过你的编译没报头文件错误,这一步可以作为验证。

4. 分步编译再链接(推荐)

像Linux目标那样,先编译成目标文件,再链接,避免一次性编译链接可能出现的问题:

windows: main.o
    g++ main.o -o main.exe -O2 -Wall -L../lib -lbox2d -lraylib -lopengl32 -lgdi32 -lwinmm

main.o: ../main.cpp
    g++ -c ../main.cpp -I../include -O2 -Wall

内容的提问来源于stack exchange,提问作者Squidos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 03:17:33