0

command line input :

g++ sdlm.cpp -IC:\SDL2-2.0.10\include -LC:\SDL2-2.0.10\lib\x64 -lmingw32 -lSDL2main -lSDL2

main file:

#include <iostream>
#include <SDL.h>
using namespace std;
#undef main
bool run=true;
SDL_Surface * image;
SDL_Surface * winsur;
SDL_Event *e ;
SDL_Renderer *ren;
int main(int argc, char* argv[])
{
  if(!SDL_Init(SDL_INIT_EVERYTHING))
  {
    cout<<"working"<<endl;
    cout<<SDL_Init(SDL_INIT_EVERYTHING)<<endl;
    SDL_Window * win;
    SDL_Renderer * ren;
    SDL_Surface *sur;
    win=SDL_CreateWindow("window",20,20,1280,600,0);
    ren=SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    winsur=SDL_GetWindowSurface(win);
    if(ren==NULL)
    cout<<"failed to render";
    SDL_Event *e;
    e=new SDL_Event;
    bool run=true;
    image=SDL_LoadBMP("chessboard.bmp");
    if(NULL==image)
    {
      cout<<"cannot load the surface"<<SDL_GetError()<<endl;
      return 0;
    }
      while(run==true)
      {
        SDL_PollEvent(e);
      if(e->type==SDL_QUIT)
      {
        break;
      }
      SDL_BlitSurface(image,NULL,winsur,NULL);
      SDL_UpdateWindowSurface(win);
      SDL_PumpEvents();
      }
      cout<<"exited loop";
  }
  else
  {
    cout<<"Something is wrong"<<SDL_GetError()<<endl;
    cout<<SDL_Init(SDL_INIT_EVERYTHING)<<endl;
  }
  SDL_FreeSurface(image);
  SDL_FreeSurface(winsur);
    return 0;
}

I keep getting the Error:-

sdlm.cpp:(.text+0x1f): undefined reference to SDL_Init
sdlm.cpp:(.text+0x5d): undefined reference to SDL_Init
sdlm.cpp:(.text+0xb2): undefined reference to SDL_CreateWindow
sdlm.cpp:(.text+0xd0): undefined reference to SDL_CreateRenderer
[...]

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • 1
    You probably have libraries for wrong architecture. What's in your `lib\x64` directory? Are there any other places where you have `libSDL2.dll.a`? – keltar Mar 08 '20 at 13:00

2 Answers2

0

You have a bunch of linker errors. You need to link all the object files you compile together to produce the final executable.

You may want to use a build system like CMake, SCons or GNU Make to express dependencies in your code, rather than trying to do everything by hand. But if you insist on doing it "by hand", remember that compilation of a C++ program consists of several phases; first is preprocessing, then comes compilation, then assembling (usually done as part of the previous phase by your compiler) and then the final phase is linking all the compiled objects together.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Preprocessing , compiling and assembling , aren't they all handled by the gcc/g++ itself ? – Animesh Pathak Mar 07 '20 at 15:19
  • @AnimeshPathak • yes, gcc/g++ are toolchain drivers and the preprocessing, compiling, assembling, and linking are handled by them. But you need to provide all the relevant files to the gcc/g++ toolchain drivers. – Eljay Mar 07 '20 at 15:22
  • @AnimeshPathak Often, yes. Sometimes, no. It depends on how you invoke the compiler. In this case you obviously forgot to link something (since you have unresolved symbols). – Jesper Juhl Mar 07 '20 at 15:23
-1

You first of all need to check that all the file directories are mentioned in your command. If the error persists, a nifty trick which shouldn't really be used is to take out -l SDL2main and add at the top of your main.cpp the line #define SDL_MAIN_HANDLED. Also, you are mixing 32-bit and 64-bit: -l mingw32 isn't really compatible with ...\lib\x64 if it's actually 64-bit. Choose one system and stick to that.

lenerdv
  • 177
  • 13
  • I was told you have to include -lming32, doesnt matter if you are using 64 or 32 bit . – Animesh Pathak Mar 08 '20 at 03:40
  • nope, `-l mingw32` tells it to use the 32-bit toolchain, which requires 32-bit libraries – lenerdv Mar 08 '20 at 09:17
  • @lenerdv it isn't. 32 bit is forced by `-m32`. `libmingw32` is just a library name, nothing special about it. – keltar Mar 08 '20 at 12:54
  • @keltar is it? afaik, `-m32` tells it to build a 32-bit program, `-l mingw32` tells it to use the 32-bit libraries – lenerdv Mar 08 '20 at 14:00
  • @lenerdv `-lmingw32` is "link with libmingw32". That's all that is. Thing is just called "mingw32" for whatever reason, there is a separate branch that is "mingw32-w64" which can target both 32 and 64bit windows. Confusing stuff, but it's everywhere in windows - win32, system32, ...; it never about architecture anymore. – keltar Mar 08 '20 at 14:32
  • @keltar I knew that 64-bit systems could run 32-bit programs as they are an 'upgrade', but not that this could bring so much confusion... thanks, anyway – lenerdv Mar 08 '20 at 14:38