1

I had the issue undefined reference to SDL_Init'` and more them with same typo.

This is my code

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

#define WIDTH 1280
#define HEIGHT 720

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

    // variable declarations
    SDL_Window *win = NULL;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *img = NULL;

    // Initialize SDL.
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        return 1;

    // create the window and renderer
    // note that the renderer is accelerated
    win = SDL_CreateWindow("Image Loading", 0, 78, WIDTH, HEIGHT, 0);
    renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

    // load our image
    img = IMG_LoadTexture(renderer, "C:/Users/BoostMyTool/Desktop/image.jpg");
    if (img == nullptr)
    {
        std::cout << "IMG_LoadTexture Error: " << SDL_GetError() << "\n";
        return 1;
    }

    int w, h;                                  // texture width & height
    SDL_QueryTexture(img, NULL, NULL, &w, &h); // get the width and height of the texture
    // put the location where we want the texture to be drawn into a rectangle
    // I'm also scaling the texture 2x simply by setting the width and height
    SDL_Rect texr;
    texr.x = 0;
    texr.y = 0;
    texr.w = w;
    texr.h = h;

    unsigned int lastUpdateTime = 0;

    // main loop
    while (1)
    {

        // event handling
        SDL_Event e;
        if (SDL_PollEvent(&e))
        {
            if (e.type == SDL_QUIT)
                break;
            else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
                break;
        }

        // paint the image once every 30ms, i.e. 33 images per second
        if (lastUpdateTime + 30 < SDL_GetTicks())
        {
            lastUpdateTime = SDL_GetTicks();

            // clear the screen
            SDL_RenderClear(renderer);
            // copy the texture to the rendering context
            SDL_RenderCopy(renderer, img, NULL, &texr);
            // flip the backbuffer
            // this means that everything that we prepared behind the screens is actually shown
            SDL_RenderPresent(renderer);
        }
    }

    SDL_DestroyTexture(img);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);

    return 0;
}

This is my makefile code

execute: main programme doexe
main:
    g++ -I "./include/SDL2" -c ./src/main.cpp -o ./lib/main.o
programme:
    g++ ./lib/main.o -o ./bin/programme
doexe:
    ./bin/programme.exe

When I write mingw32-make to the cmd it gaves an error which is

g++ -I "./include/SDL2" -c ./src/main.cpp -o ./lib/main.o
g++ ./lib/main.o -o ./bin/programme
./lib/main.o:main.cpp:(.text+0x27): undefined reference to `SDL_Init'
./lib/main.o:main.cpp:(.text+0x6c): undefined reference to `SDL_CreateWindow'
./lib/main.o:main.cpp:(.text+0x8a): undefined reference to `SDL_CreateRenderer'
./lib/main.o:main.cpp:(.text+0xa0): undefined reference to `IMG_LoadTexture'
./lib/main.o:main.cpp:(.text+0xae): undefined reference to `SDL_GetError'
./lib/main.o:main.cpp:(.text+0x113): undefined reference to `SDL_QueryTexture'
./lib/main.o:main.cpp:(.text+0x13f): undefined reference to `SDL_PollEvent'
./lib/main.o:main.cpp:(.text+0x16f): undefined reference to `SDL_GetTicks'
./lib/main.o:main.cpp:(.text+0x17d): undefined reference to `SDL_GetTicks'
./lib/main.o:main.cpp:(.text+0x18b): undefined reference to `SDL_RenderClear'
./lib/main.o:main.cpp:(.text+0x1ac): undefined reference to `SDL_RenderCopy'
./lib/main.o:main.cpp:(.text+0x1b7): undefined reference to `SDL_RenderPresent'
./lib/main.o:main.cpp:(.text+0x1cb): undefined reference to `SDL_DestroyTexture'
./lib/main.o:main.cpp:(.text+0x1d6): undefined reference to `SDL_DestroyRenderer'
./lib/main.o:main.cpp:(.text+0x1e1): undefined reference to `SDL_DestroyWindow'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
Makefile:5: recipe for target 'programme' failed
mingw32-make: *** [programme] Error 1

I tried to get my code step by step .o then .exe and execute .exe

I tried to add -lmylib to my begining of my makefile commands but it caused ** cannot find <SDL.h> or it didn't done anyting

I am going to look for a mingw64.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 4
    You're not linking against the required library. This can be done by passing `-lmylib` to your LDFLAGS. – SimonC Nov 28 '22 at 15:00
  • There are quite a few libraries you need to link against for SDL depending on your code. You set the include directory but did not add a link directory or link to any libraries. – drescherjm Nov 28 '22 at 15:07
  • 1
    It's a very common newbie misunderstanding, almost always to use a third party library, you need to add the header files to your build (newbies understand that part) and you need to add the libraries to your build (newbies don't understand that part). – john Nov 28 '22 at 15:10

0 Answers0