I'm trying to use Box2D lib with SDL lib. NOTE that I only get the errors below after I link in SDL too. I think i link everything accordingly but in the end when I write up some code i get errors like:

1>------ Build started: Project: box2d test2, Configuration: Debug Win32 ------
1> main.cpp
1>Box2D.lib(b2PolygonShape.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in main.obj
1>Box2D.lib(b2BlockAllocator.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in main.obj
1>Box2D.lib(b2Settings.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in main.obj
1>SDLmain.lib(SDL_win32_main.obj) : error LNK2005: _main already defined in main.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external symbol _SDL_main referenced in function _main
1>c:\users\deividas\documents\visual studio 2010\Projects\box2d test2\Debug\box2d test2.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This only happens when I start using the lib, not when I only include it. For now I'm just using code like this:

#include <Box2D\Box2D.h>

int main() {

    b2PolygonShape groundBox;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundBox;
    boxShapeDef.density = 2.0f;
    boxShapeDef.restitution = 0.5f;

	return 0;
}

Taken from: http://www.dreamincode.net/forums/topic/261881-sdl-with-box2d/ .
I googled something but did not exactly understood what to do, help would be appreciated.

Dani AI

Generated

Short summary and root causes (two separate problems)

Two different issues are showing up in the log: the LNK2038 / LNK4098 messages come from mixing CRT/iterator-debug settings between your app and Box2D (debug vs release or different /MD vs /MDd etc.), while the SDL errors (LNK2005 / LNK2019 referring to _main and _SDL_main) are caused by SDL’s entry-point wrapper expecting your main to be renamed to SDL_main unless SDL’s headers/macros are handled correctly. (stackoverflow.com)

How to fix the CRT / iterator-debug mismatch

  • Make sure Box2D.lib and the executable are built with the same configuration (both Debug or both Release) and the same Runtime Library setting: Project → Properties → C/C++ → Code Generation → Runtime Library (e.g. /MDd for Debug, /MD for Release). Rebuild Box2D from source in the matching configuration if needed.
  • Verify Additional Library Directories / Additional Dependencies point to the matching Debug/Release folder (it’s easy to accidentally link the Debug .lib into a Release build). Clean the solution and rebuild. (learn.microsoft.com)

How to fix the SDL main conflict

SDL provides a platform-specific main wrapper; either let SDL rename your main to SDL_main by including SDL.h, or opt out by defining SDL_MAIN_HANDLED before including SDL.h (or use SDL_SetMainReady). Example:

#define SDL_MAIN_HANDLED
#include <SDL.h>

int main(int argc, char* argv[])
{
    // initialize SDL, game loop...
    return 0;
}

Link the correct SDLmain/SDL (SDL2main + SDL2 for SDL2) and match subsystem choices (Console vs Windows) as appropriate. (wiki.libsdl.org)

Quick checklist / cautions

  • Do not ignore LNK4098 warnings by default; they signal CRT mismatches—fix the Runtime Library settings instead of using /NODEFAULTLIB unless you understand the consequences. (stackoverflow.com)
  • As a last resort you can recompile dependencies with matching flags or use the iterator macros/flags (_HAS_ITERATOR_DEBUGGING/_ITERATOR_DEBUG_LEVEL or _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH) temporarily, but rebuilding with matching settings is the correct solution. (stackoverflow.com)

Notes tied to the thread: ’s prompt to verify libraries is on target—double-check which .lib files are actually being pulled. should confirm whether the Box2D .lib is the Debug build and whether SDL headers are included before defining main.

If you get a linking error, make sure your functions names are exactly same as their prototypes. so, see if you might misspelled something

I'm pretty sure i would not get an error like this if I would misspell a function, thanks for the reply though.

so if this the case, did you add the library reference to the project?

I linked them ofcourse but maybe I did something wrong since it doesnt work. I linked them like I usually link libs. If do this in visual studio 08 I get the same message but in warning form so it works, but it still bothers me though. Im using vs10 btw.

yea sometime going between an old VS to a a newer one, can give you a long list to fix before you can run the application. good luck though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.