我今天想在 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
我在 Makefile 中尝试了很多配置以使其工作,但没有任何效果。
问题实际上是 libbox2d.a,我在 Windows 上构建了 box2d,并且我的代码使用新的 libbox2d.a 编译得很好。