I have a windows 11 pro x64 architecture setup, with SDL version 2.24.0 installed on it and I am using a mingw-w64 compiler(8.1.0).
I am trying to run a sample SDL code(sample_sdl_code.cpp) on vs code -
#include <iostream>
#include "./src/include/SDL2/SDL.h"
const int WIDTH = 640;
const int HEIGHT = 480;
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL Image Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
// Create a surface to hold the image
SDL_Surface* imageSurface = SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 24, 0, 0, 0, 0);
// Draw a sample image using RGB values
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
Uint8 r = 255 * (x / static_cast<double>(WIDTH));
Uint8 g = 255 * (y / static_cast<double>(HEIGHT));
Uint8 b = 255 * ((x + y) / static_cast<double>(WIDTH + HEIGHT));
Uint32 pixelColor = SDL_MapRGB(imageSurface->format, r, g, b);
// Set the pixel color on the surface
SDL_Rect pixelRect = { x, y, 1, 1 };
SDL_FillRect(imageSurface, &pixelRect, pixelColor);
}
}
// Create a texture from the surface
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
// Render the texture to the window
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
// Event loop to keep the window open
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
// Clean up resources
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
My project structure is -
| src
| include
| lib
| SDL2.dll
| sample_sdl_code.cpp
I have cross checked and I am using include,lib and SDL2.dll folders and files from the "x86_64-w64-mingw32" folder of the SDL library.
The thing is when I compile this using -
g++ -Lsrc/lib sample_sdl_code.cpp -o sdl_sample.exe -lmingw32 -lSDL2main -lSDL2
I get no errors whatsoever but when I try to run my executable this error comes up -
Program 'sdl_sample.exe' failed to run: Thespecified executable is not a valid application
for this OS platform.At line:1 char:1
+ ./sdl_sample.exe
+ ~~~~~~~~~~~~~~~~.
At line:1 char:1
+ ./sdl_sample.exe
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable:
(:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
Now I know this question is very similar to - to->post
But the person here is trying to use a mingw32 compiler and is trying to create an executable for a 32 bit architecture and also there are no concrete answers on that post, so thought posting a new question here.