DEV Community

Cover image for SDL2 on macOS Sonoma installation with VSCode.
Pradyumna Sahu
Pradyumna Sahu

Posted on • Edited on

SDL2 on macOS Sonoma installation with VSCode.

How to install SDL2 on macOS sonoma with visual studio code is here. No Xcode installation is required. only a programing IDE is required. Here we gonna use Visual Studio code community version from Microsoft. It is a popular IDE choice among developers.

Install Homebrew.

Homebrew is a great package manager for unix operating systems. It is reliable. (don't worry, it's safe until used properly.) click here to go directly to home-brew's official download site. or simply open terminal and copy-paste this command - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)".
now follow the on-screen commands for a successful installation.

Install SDL2
It's the time for our package. type cls int terminal and hit return. Now type brew install sdl2 and relax after hitting return. After successful installation, type cls again. Now type sdl2-config --version and check what version it's showing for the latest.

Time to code
make a new folder and a new file named main.cpp. g++ compiler will be used to compile the code now.

lets open a blank window now. copy paste the code in that main.cpp file from following code -

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

const int WIDTH = 1000, HEIGHT = 800;

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

    SDL_Window *window = SDL_CreateWindow( "this is my", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );

    if ( NULL == window ){
        std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
        return 1;
    }
    SDL_Event windowEvent;
    bool running = true;
    while (running)
    {
        if ( SDL_PollEvent( &windowEvent ) )
        {
            if ( SDL_QUIT == windowEvent.type ){
               running = false;
               break; 
            }
        }
    }

    SDL_DestroyWindow( window );
    SDL_Quit( );

    return EXIT_SUCCESS;
}
Enter fullscreen mode Exit fullscreen mode

Compile and let the magic happen
open up terminal and type the following code - sdl2-config โ€”a program that prints out the right flags. This should happen as part of the build process. the command prints an argument for linking the sdl2 library. copy that line.

now type this command for compilation -
g++ main.cpp -o main {paste the copied argument here}

for my case it was like this. -
g++ main.cpp -o main -L/usr/local/lib -lSDL2
this should make the work done.๐Ÿ‘ฝ

now type ./main and see the blackness of black window๐ŸชŸ.

Top comments (1)

Collapse
 
nightsquid7 profile image
Nightsquid7

Thanks for posting!