# Can't figure out where these errors are comming from.

## Recommended Posts

I am getting the following errors from my sdl experiment project. I just can't find where they are comming from. AnimationExperiment warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library AnimationExperiment error LNK2019: unresolved external symbol _SDL_main referenced in function _main AnimationExperiment fatal error LNK1120: 1 unresolved externals here is the code
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

#include <iostream>
#include <SDL.h>

using namespace std;
// animation struct
struct CAnimation
{
int frameCount;
int x;
int y;
int w;
int h;
};

// sprite class
class CSprite
{
private:
CAnimation animation;
int xpos;
int ypos;

public:
void SpriteInit();
void ChangeFrame();
void DrawFrame(SDL_Surface *screen, SDL_Surface *frame);
};

// Initializes the sprite
void CSprite::SpriteInit()
{
CSprite::animation.frameCount = 0;
CSprite::animation.x = 0;
CSprite::animation.y = 0;
CSprite::animation.w = 63;
CSprite::animation.h = 63;
CSprite::xpos = 300;
CSprite::ypos = 300;
}

// Changes the frame
void CSprite::ChangeFrame()
{
CSprite::animation.frameCount++;
CSprite::animation.x += 63;
}

// Draw the frame
void CSprite::DrawFrame(SDL_Surface *screen, SDL_Surface *frame)
{
SDL_Rect dest;
dest.x = CSprite::xpos;
dest.y = CSprite::ypos;
SDL_Rect source;
source.x = CSprite::animation.x;
source.y = CSprite::animation.y;
source.w = CSprite::animation.w;
source.h = CSprite::animation.h;

SDL_BlitSurface(frame, &source, screen, &dest);
SDL_Flip(screen);
}

int main(char argc, char *argv[])
{
CSprite sprite;

// set up sdl
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "Error in initalizing SDL: " << SDL_GetError() << "\n";
exit(1);
}
atexit(SDL_Quit);

// create surfaces
SDL_Surface *screen;
SDL_Surface *frame;
SDL_Surface *background;

screen = SDL_SetVideoMode(600, 600, 24, SDL_HWSURFACE|SDL_DOUBLEBUF);
// make sure screen was made
if(screen == NULL)
{
cout << "Could not create screen: " << SDL_GetError() << "\n";
exit(1);
}

// draw the background
SDL_Rect backdest;
backdest.x = 0;
backdest.y = 0;
SDL_BlitSurface(background, NULL, screen, &backdest);

// game loop
bool done = false;
while(done == false)
{
// for events
SDL_Event event;

// event loop
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
done = true;
}

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done = true;
}
}
}

sprite.SpriteInit();
sprite.DrawFrame(screen, frame);
SDL_Delay(1000);
sprite.ChangeFrame();
}
return 0;
}


##### Share on other sites
Did you try setting /NODEFAULTLIB:library in the linker options? Guessing here... but it sounds like you have a library that already statically links in msvcrt.lib... which is different to what is trying to be linked in here by default.

So, if you set /NODEFAULTLIB:library... it won't do the link...

Just a guess...

##### Share on other sites
nvm what was here b4 I am still at these errors.

AnimationExperiment warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

AnimationExperiment error LNK2019: unresolved external symbol _SDL_main referenced in function _main

AnimationExperiment fatal error LNK1120: 1 unresolved externals

I should not have to disable default libraries because all my other sdl apps work file with the default libraries enabled.

[Edited by - TheNobleOne on December 2, 2004 5:44:22 PM]

##### Share on other sites
It sounds like you're still linking to something (lib or dll) that uses the multi-threaded debug libs. If you haven't done a clean/rebuild, try that (if you have incremental linking on msvc doesn't always catch changes to libs). Otherwise either chance you project to use the mtd libs as well, or track down what's using it.

##### Share on other sites
Ok I am back to the first 3 errors again. turns out I am suppose to use the multithreaded debug dll switch. However, I can't find why I have 3 those errors.

##### Share on other sites
The warning should have disappeared when you switched your project to use the multithreaded DLLs. Are you sure you did it properly?

The two errors are actually one problem being reported twice; it's a tricksy result of the way SDL does it's platform-independent code. Let me explain...

Because the entry point on a Windows app needs to be called 'WinMain,' as opposed to other platforms that usually just want 'main,' SDL hides the difference by providing WinMain/main for whichever platform you're using and having it call through to your own function (which is *always* called 'main'). That way you can just write 'main,' compile it on either platform, and SDL takes care of the incompatability in names.

However, you can't actually call it 'main' because (on many platforms) SDL is already providing a function with that name on non-Windows platforms [smile]

So, what it does is to provide a header file - sdl_main.h - which, when included, replaces the word 'main' with '_SDL_main' - so your function which appears to be called main() actually reaches the compiler as _SDL_main(). It doesn't have a problem with that; a name's a name, as far as it's concerned. SDL's own WinMain/main function thus includes a call to _SDL_main().

The problem is that you've not included the sdl_main.h header here, so your main() function isn't getting renamed. When the linker tries to connect the SDL library (which has a call to _SDL_main()) to your code (which isn't providing an _SDL_main()) function, it complains. That's what 'unresolved external' means - a function that the SDL library makes to outside of itself (i.e. an 'external' call) couldn't be found around anywhere else.

Including sdl_main.h should fix the problem; or, if you want, you could rename your main function to _SDL_main. Including sdl_main.h is the recommended approach, though.

##### Share on other sites
humm I added the #include "sdl_main.h" and I still got those errors.

##### Share on other sites
You changed:

#include <iostream>#include <SDL.h>using namespace std;

to

#include <iostream>#include <SDL.h>#include <SDL_main.h>using namespace std;

and still get exactly the same errors?

##### Share on other sites
yes exactly the same errors.

##### Share on other sites
from what i've seen from everyone else's code, i thought you included SDL files like this:
#include <SDL/SDL.h>#include <SDL/SDL_main.h>

or is that for something else?

##### Share on other sites
it depends on where you have the headers stored. mine are stored so I need no sub-directories

##### Share on other sites
Have you linked sdl.lib and sdlmain.lib?

Also, I sometimes get that first warning in SDL projects, even though I've set my project to use multithreaded DLLs. I haven't actually had any problems though.

Edit: It turns out I'm blind. You did link both libraries. Are both libraries actually copied into the right place? I would assume so, since there are no errors finding them.

##### Share on other sites
AHA!

You're gonna love this...

change
int main(char argc, char *argv[])

to
int main(int argc, char *argv[])

You were calling an int main() that it didn't recognize.

##### Share on other sites
thanks a bunch that fixed it lol.

I don't know how I missed that lol.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978516

• 10
• 10
• 12
• 22
• 13