Struggling to Use Self Built SDL 2

Hi all! I’ve read a lot around online, but I’m still having trouble with something that most people probably know how to do…please help!

I want to build a cross platform game with SDL 2, but I don’t want to use Xcode / Visual Studio / etc. (and I want to avoid using the Frameworks stuff if possible) Just a plain 'ol compiler and text editor! However, I can’t even compile my game (right now just trying to open a window). The compiler can’t find the necessary SDL files. So, here’s what I’m doing:

  1. I’m on a mac, OS X version 10.14.3
  2. I download the development source code from here: http://www.libsdl.org/download-2.0.php
  3. Then I follow the instructions for Mac OS X here: http://wiki.libsdl.org/Installation#Mac_OS_X
    a) I switch to the directory with the files, and make a folder named ‘build’
    b) I ‘cd’ into the build folder, and run ‘CC=/where/i/cloned/SDL/build-scripts/gcc-fat.sh …/configure ; make’
    • It gives me this message: make: *** No targets specified and no makefile found. Stop.
      c) If I drop the CC part, and just run ‘…/configure; make’, it seems to build correctly?
  4. Then I make a simple game.c file: https://gist.github.com/coltonhurst/3b1209240d514bbe360499a13bfbe0e4
  5. Here comes the tricky part. SDL seems to have built correctly… so how do I build the game? My file is in the build folder, where sdl2-config is. I try running things like: [ clang -o myGame game.c ‘sdl2-config --cflags --libs’ ] but I get this error: [ clang: error: no such file or directory: ‘sdl2-config --cflags --libs’ ]
  6. When I just doing: [ clang -o myGame game.c ], I get this: [ fatal error: ‘SDL.h’ file not found ]

Thank you for reading, and thanks for the help. I really appreciate it!

Try running the following,

clang main.cpp `sdl2-config --cflags` `sdl2-config --libs`

I do recommend using at least cmake or some other build system.
If your on a diff OS please let me know and I can assist further.

Thanks for the reply! How could I use cmake to build with SDL?

And when I run game.c 'sdl2-config --cflags' 'sdl2-config --libs', it still gives me the same error:

clang: error: no such file or directory: ‘sdl2-config --cflags’
clang: error: no such file or directory: ‘sdl2-config --libs’

How did you install the SDL2 ?
Also what OS do you have ?

If you look at my first comment, it will answer both of those questions.

Thanks for the reply!

If you use brew to install it, it should work like I posted.

brew install sdl2

The run

clang main.c `sdl2-config --cflags` `sdl2-config --libs`

Ok… so here’s what I did:

  1. brew install sdl2
  2. Leaving my game.c in the same aforementioned directory, I run game.c 'sdl2-config --cflags' 'sdl2-config --libs'

Unfortunately, I’m getting the same result:

clang: error: no such file or directory: ‘sdl2-config --cflags’
clang: error: no such file or directory: ‘sdl2-config --libs’

Do I need to move game.c or something?

Ok try this,

Create a new file in your desktop call main.c

#include <SDL.h>

int main() {
    SDL_Window* window;
    return 0;
}

From the desktop run

clang main.c `sdl2-config --cflags` `sdl2-config --libs`

I don’t know if this is up-to-date (with Apple changing shit all the time), but http://lazyfoo.net/tutorials/SDL/01_hello_SDL/mac/index.php might be helpful :slight_smile:

Apple are really good at breaking things with their latest updates :rofl:

Apparently sdl2-config can’t be found because it’s not in $PATH, which might be expected when using Frameworks (not sure, I’m not a Mac user)?
But I guess when using a Framework one shouldn’t need sdl2-config, but “just” tell the compiler what framework to link against (and maybe where it can be found if it’s not in a standard path). At least that’s my understanding of how they’re supposed to work…

OTOH, maybe the problem is using the wrong kind of quotes around the sdl2-config command: it should be backticks (`), not single quotes ()… unfortunately, discourse fucks up the formatting of those, even in code-blocks blocks (at least in preview)…

1 Like

Ye I did notice that but I was hoping he would copy and paste my example exactly.
He should be able to run sdl2-config --cflags --libs in the terminal to find the path of the header and lib.

The path should be automagically configured if you use brew install.

2 Likes

Okay all, thanks for the help.

2 things… I do need the backticks `, I was still using '.
Also, I use fish as my terminal shell, but it doesn’t work in fish, I had to switch to bash.

So, when building in the /build folder or on the desktop, it works both ways (probably because of the homebrew install).

game.c compiles correctly, but I don’t see any window. Is that because there’s something wrong with my game.c file? (https://gist.github.com/coltonhurst/3b1209240d514bbe360499a13bfbe0e4)

(I’m trying to run it with ./a.out)

Just out of curiosity, I’ll uninstall sdl installed with brew, and see if this method works using what I have in the /build folder.

Oh… and @Daniel_Gibson, I guess to answer some of your previous comments… I wanted to avoid using Frameworks & Xcode. Thank you for posting the lazy foo tutorials, they are very helpful.

#include "SDL.h"
#include <stdio.h>

int main(int argc, char* argv[]) {
    bool quit = false;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);

    while (!quit) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

You can use frameworks from commandline as well, I think -F/path/to/directory lets you set a directory to look for them and -framework SDL2 tells clang to use it.

BTW, if backticks are not working in fish, in bash an alternative form is $(sdl2-config bla), maybe there is something similar in fish? I think “Command substitution” is the term you should be looking for

@Daniel_Gibson and @Smiles

Thanks! I’ll try that code, and look into what’s going on with fish.

So… when attempting to compile without homebrew, it still doesn’t work. What is homebrew doing that I’m doing incorrectly? I downloaded the libs, built them, and added the /build folder (where the build for SDL is) to my path. I’ve tried executing the clang command for my file on the desktop, and within the build folder, and I’m still getting the error: ‘SDL.h’ file not found?

Homebrew installs packages to their own directory and then symlinks their files into /usr/local.

Just a quick note with the code I posted, there are no bool type in the C language so you will need to change it to a char or something similar.

Or make the file a .cpp file. This will use the cpp compiler.

@Smiles can I find out what files they symlink? I just tried to symlink sdl2-config, and it unfortunately didn’t work.

cd /usr/local/lib/
ls -la

example
libSDL2-2.0.0.dylib -> …/Cellar/sdl2/2.0.9/lib/libSDL2-2.0.0.dylib

http://manpages.ubuntu.com/manpages/xenial/man1/sdl2-config.1.html

sdl2-config is a tool that is used to configure and determine the compiler and linker flags that should be used to compile and link programs, libraries, and plugins that use
SDL. It is also used internally by the m4 macros that are included with SDL.

Why are you avoiding IDE’s / build systems ?
Is this a learning experience ?