I'm trying to learn how to use raylib with physac, however I'm quite new to using a c++ compiler and after a few hours trying to get a minimal example working, I came here seeking your help.

here is my main.cpp file:

#include "raylib.h"
#include "physac.h"

int main()
    const int WINDOW_WIDTH = 800;
    const int WINDOW_HEIGHT = 400;

    InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "example");

    while (!WindowShouldClose())

    return 0;

when trying to run the file, I get the following errors:

undefined reference to `InitPhysics'
undefined reference to `UpdatePhysics'
undefined reference to `ClosePhysics'

this leads me to believe I'm missing some sort of library, here's my g++ command:

g++ source/*.cpp -o bin/final.exe -O2 -Wall -Wno-missing-braces -I include/ -L lib/ -lraylib -lopengl32 -lgdi32 -lwinmm

My include folder only contains physac.h and raylib.h and my lib folder contains libraylib.a. My guess is that I need some other lib file or some other flag on the compiler, but I can't figure out what.

  • 2
    \$\begingroup\$ Check the example, the author seems to #define PHYSAC_IMPLEMENTATION before he #include "physac.h". I agree though, that it's an odd way to design a library. \$\endgroup\$
    – Vaillancourt
    Sep 12 '21 at 2:09

Ok, I figured it out and I'll post the answer here in case anyone needs it.

The implementation of physac is not on any library, it is directly within the .h file itself, it's just commented out (actually, it's behind an #if defined, but the #if defined is not true unless you edit the header file).

If you were to try and uncomment the implementation, every time you include the header file, you would redefine the implementation, which would cause an error, so what I did was copy the implementation portion of the header into a physac.cpp file and put that into my source folder.

Not sure if that's the best way to do it, but it is for sure the way that worked for me.


Ok, so as user @Vaillancourt explained, the correct way to do this is to #define PHYSAC_IMPLEMENTATION before including physac.h, however it's important to do so only on one file.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .