-1

I am making a C++ project using SFML, ImGui and box2d. The first two of them work fine, but when I include box2d and use it, i get about 20 LNK2019 errors.

Maybe I should use another library? Or I have made any errors?

What am I using

  • Visual Studio 2017
  • box2d 2.4.1 (latest)

Additional Dependencies: Additional Include Directories:

Code

#include <box2d/box2d.h>

int main() {
        b2World world(b2Vec2(0, -10));
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, -10.0f);
    b2Body* groundBody = world.CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    groundBox.SetAsBox(50.0f, 10.0f);
    groundBody->CreateFixture(&groundBox, 0.0f);

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0.0f, 4.0f);
    b2Body* body = world.CreateBody(&bodyDef);
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(1.0f, 1.0f);
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body->CreateFixture(&fixtureDef);
}

What I tried

  • I used cmake-gui and Visual Studio 2017 to build box2d, ecerything was fine at this step and I didnt get any errors
  • Then I copied box2d.lib from bin folder of box2d build into my projects lib folder
  • I also copied include folder into my projects include` folder
  • I set include and lib folder in my projects settings
  • I repeated the Hello Box2D example from box2ds site

What actually happened

When I compiled the program I got 20 LNK2019 errors.

What I expected

I expected the program to compile and work.

  • 1
    what are the errors? The generic error code is almost useless, you need to read the whole error message. And how do you link? – 463035818_is_not_an_ai Aug 23 '23 at 06:44
  • 2
    It's almost never a good idea to copy files. Instead leave the files where they are and tell your build system where to find them. Sorry, no idea how to do that in this specific case, just a general piece of advice. – john Aug 23 '23 at 06:47
  • ot: `box2d` defines everything in the global namespace? Thats not good, but if it is like that and you want to use the library, not much you can do about it. – 463035818_is_not_an_ai Aug 23 '23 at 06:48
  • From your description, it sounds like you didn't specify "box2d.lib" as a linker input. (It does not automatically include all files in the directory you specify.) – molbdnilo Aug 23 '23 at 06:48
  • your copying and setting the include path is about including the headers, but you need to link the library. Including works, otherwise you'd get compiler errors. Linker errors are about failing to link. I don't know Visual Studio, but somehow you need to tell it to link the library – 463035818_is_not_an_ai Aug 23 '23 at 06:49
  • If this is a Visual Studio problem then it's easy, add the directory containing the library to Linker/General/Additional Library Directories (this is **instead** of copying the library). and then add the library name itself (with extension) to Linker/Input/Additional Dependencies – john Aug 23 '23 at 06:51

0 Answers0