Got Linker Error

Started by
7 comments, last by mike4 12 years, 6 months ago
Hi everybody, I'm curently learning SDL with c++, ( was going for directdraw but i changed my mind ). I'v been following a very nice tutorial which gave the source code at the end. I like to see what it do before i read it for the second time to help me understand how's the code working but it doens't work Here's the code

#include <stdio.h>
#include <stdlib.h>

#include <SDL.h>

void Slock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}

void Sulock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }
}

void DrawPixel(SDL_Surface *screen, int x, int y,
                                    Uint8 R, Uint8 G, Uint8 B)
{
  Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  switch (screen->format->BytesPerPixel)
  {
    case 1: // Assuming 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
      }
      break;
    case 2: // Probably 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
      }
      break;
    case 3: // Slow 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = color;
          bufp[1] = color >> 8;
          bufp[2] = color >> 16;
        } else {
          bufp[2] = color;
          bufp[1] = color >> 8;
          bufp[0] = color >> 16;
        }
      }
      break;
    case 4: // Probably 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
      }
      break;
  }
}

void DrawScene(SDL_Surface *screen)
{
  Slock(screen);
  for(int x=0;x<640;x++)
  {
    for(int y=0;y<480;y++)
    {
      DrawPixel(screen, x,y,y/2,y/2,x/3);
    }
  }
  Sulock(screen);
  SDL_Flip(screen);
}

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

  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);

  SDL_Surface *screen;
  screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
  if ( screen == NULL )
  {
    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    exit(1);
  }
  int done=0;

  while(done == 0)
  {
    SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }

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

    DrawScene(screen);
  }

  return 0;
}



And here are the linking error i get Linking... main.obj : error LNK2019: unresolved external symbol _SDL_LockSurface referenced in function "void __cdecl Slock(struct SDL_Surface *)" (?Slock@@YAXPAUSDL_Surface@@@Z) main.obj : error LNK2019: unresolved external symbol _SDL_UnlockSurface referenced in function "void __cdecl Sulock(struct SDL_Surface *)" (?Sulock@@YAXPAUSDL_Surface@@@Z) main.obj : error LNK2019: unresolved external symbol _SDL_MapRGB referenced in function "void __cdecl DrawPixel(struct SDL_Surface *,int,int,unsigned char,unsigned char,unsigned char)" (?DrawPixel@@YAXPAUSDL_Surface@@HHEEE@Z) main.obj : error LNK2019: unresolved external symbol _SDL_Flip referenced in function "void __cdecl DrawScene(struct SDL_Surface *)" (?DrawScene@@YAXPAUSDL_Surface@@@Z) main.obj : error LNK2019: unresolved external symbol _SDL_PollEvent referenced in function _SDL_main main.obj : error LNK2019: unresolved external symbol _SDL_SetVideoMode referenced in function _SDL_main main.obj : error LNK2019: unresolved external symbol _SDL_Quit referenced in function _SDL_main main.obj : error LNK2019: unresolved external symbol _SDL_GetError referenced in function _SDL_main main.obj : error LNK2019: unresolved external symbol _SDL_Init referenced in function _SDL_main LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup Debug/SDL_2.exe : fatal error LNK1120: 10 unresolved externals Anyone can help me fix those error ( and tell me how to avoid it in the futur so i won't ask again ;) ) thank. EDIT : Great... almost forgot to say i use VS.net 2003
Advertisement
are you linking to SDL.lib and SDLmain.lib ?
LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup

*frowns* This doesn't look good. What options did you use to create your VS project?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
It is a Win32 CONSOLE app ( empty )
I did add

#include <SDL.lib>
#include <SDLmain.lib>

but now it's say no such files or directory, and i'v set up my VS to go get the lib in the SDL directory and i'm sure it's ok.
Ok. Bear with me, this is a train of thought. I notice that the linker reports errors in a function named SDL_main (the leading underscore is part of how the linker sees function names: this is the naming convention for C functions), which is not present in your code. That means it is possible that SDL.h defines a main macro that translates to SDL_main. This would explain why the linker cannot find main (mainCRTStartup is the C runtime function that does call your program's main after having set things like the standard file descriptors up). I also think I remember there is a SDLmain library which is intended to act as a wrapper for your code, by providing a main function and calling SDL_main. (And reading Drakkcon's post, apparently I'm correct [smile]).

So, to placate the first batch of linker errors, you do need to link to SDL.lib. Otherwise, you are trying to use functions the linker doesn't have an implementation for. And to placate the second batch of errors, you'll need to link SDLmain.lib

In other words: "what Drakkcon said".

Edit - these are library files, not header files. You can't #include them. The best way (in my opinion) to link them is to add them to the list of 'source' files for your project. I.e. the treelist where you have your main.cpp file listed should also contain SDL.lib and SDLmain.lib (find where they are on your system).

You can also do #pragma comment(lib,"SDL.lib") but that's not as good (again, in my opinion) because it relies on a pragma, which are compiler-specific.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Okay, I'v linked the 2 lib. Now i get this ( awww )


Linking...
msvcrt.lib(MSVCR71.dll) : error LNK2005: _exit already defined in LIBCD.lib(crt0dat.obj)
msvcrt.lib(MSVCR71.dll) : error LNK2005: _strncpy already defined in LIBCD.lib(strncpy.obj)
msvcrt.lib(MSVCR71.dll) : error LNK2005: _fclose already defined in LIBCD.lib(fclose.obj)
msvcrt.lib(MSVCR71.dll) : error LNK2005: __isctype already defined in LIBCD.lib(isctype.obj)
LIBCD.lib(crt0init.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
Debug/SDL_2.exe : fatal error LNK1169: one or more multiply defined symbols found

beleive me or no, it's working :)
Thank for you help guys.
main.obj: -1: error LNK2019: unresolved external symbol “_SDL_GetError” in function “” int __cdecl LoadGLTex (char *, int) “(LoadGLTex @ @ @ Z YAHPADH?)”.

I’ve added the libs and headers so why i get this error with qtcreator? funny it works fine in VS and Eclipse.
Many thanks
Michael

This topic is closed to new replies.

Advertisement