c++windowscompiler-errorsg++box2d

Can't resolve undefined reference to box2D C++


I wanted today to compile my c++ code on windows, I get an undefined reference to all box2D part of my code.

Here's my 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

Also here's my tree structure :

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

Here's the error that I get :

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

I tried a lot of configuration in my Makefile to make it work but nothing worked.


Solution

  • The problem was effectively the libbox2d.a, I built box2d on windows and my code compiled well with the new libbox2d.a.