Beginner SDL 2.0 Compile Error OS X

Hi, I’m new to SDL, and look forward to making games with it! Please help me get started!

I’ve been following the “Lazy Foo’ Productions” tutorials, but I can’t seem to get SDL working.

  1. The first tutorial I followed is this one. I downloaded the SDL 2.0 developmental libraries, and dragged SDL2.framework to my /Library/Frameworks folder.

  2. Then I followed the second tutorial

Then I try to compile with this: g++ window.cpp -I/Library/Frameworks/SDL2.framework/Versions/A/Headers/

However, I get an error saying “Undefined symbols for architecture x86_64” with a lot of text after. (I can add this if needed). At the end clang gives me an error: linker command failed with exit code 1

How can I compile a basic file and add the SDL 2.0 headers so everything will work? I know I can do things through homebrew, but I don’t really want to do that, as this should work. What do I need to do to get this to work? Please help! Thank you!

Here is my window.cpp file:

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

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char* args[])
{
	SDL_Window* window = NULL;

	SDL_Surface* screenSurface = NULL;

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
	}
	else
	{
		window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
		
		if (window == NULL) {
			printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
		}
		else
		{
			//Get window surface
			screenSurface = SDL_GetWindowSurface( window );
			//Fill the surface white
			SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
			//Update the surface
			SDL_UpdateWindowSurface( window );
			//Wait two seconds
			SDL_Delay( 2000 );
		}
		SDL_DestroyWindow(window);
		SDL_Quit();
	}

	return 0;

}

I can’t help you much further than this since I don’t have any Apple hardware, but it looks like you need to follow this tutorial so that you link to the framework during compilation. Libraries normally require two parts when using them, the headers, (which you’ve added already), and the library itself, which the tutorial I’ve linked above should help you do.

This is really helpful, thanks!

Do you (or does anyone) know how to add the library so I can successfully build without using XCode?

I’ll use xcode if I need to, my goal is finding a way that doesn’t use XCode or rely on anything besides the library and compiler so I can build straight from the command line.

Check near the bottom of: here, that has some of the command line options for g++, which you’re using.

Xcode has a command line interface.

@rtrussell that’s true, I guess I want to do it with just a compiler and plain text editor so I learn everything that’s going on, and I make sure I understand the compilation process. (As I can’t get anything to compile now, I obviously don’t fully understand it.)

@Blerg thanks for the link, that’s helpful. Now I run this command:

g++ -o Window -I/Library/Frameworks/SDL2.framework/Versions/A/Headers/ -l/Library/Frameworks/SDL2.framework/Versions/A/SDL2.a window.cpp

And I get this error:

ld: library not found for -l/Library/Frameworks/SDL2.framework/Versions/A/SDL2.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems I’m closing in on the problem. Any other ideas?

g++ -o window -I/Library/Frameworks/SDL2.framework/Headers -F/Library/Frameworks -framework SDL2 window.cpp

Solves the issue. Does anyone know why this works and the -l flag doesn’t? (-L except lowercase L, it looks like a capitalized I)

If I’m not mistaken, -L is used to tell the compiler to use the specified path when it needs to look for libraries. The -l is used to specify that you want to link to a library. This is so that you can put all of your libaries in a single folder, and link to them individually if you want too. I’m guessing that -F is used to specify a path to search for frameworks, and the -framework is to specify which one to use; however I think that’s something that only Apple does as I’ve never seen them before, (I’ve been programming for Linux and Windows for awhile now). Your -l command was probably getting the default paths prepended to it, which is probably why it wasn’t finding the library. I hate linking with a passion; DESPISE linking… Sorry.

Personally, I use a cross-platform IDE to avoid having to constantly learn/update the command line(s) necessary to compile a program. This also helps me avoid learning more than one set of command lines when switching the OS that I’m programming for. I personally use Code::Blocks as it’s free, and cross-platform. I believe that Codelite, Eclipse, and KDevelop are also all free cross-platform IDEs as well. CLion is a paid IDE that I wanted to try out; however you have to know how to use CMake files at the time looked at it in order to be able to create anything with it, (I’ll have to check it out here again soon). Oooooo, looks like they’re slowing moving away from CMake only projects with CLion right now. YEAH BUDDY!

The ungodly interface that is Visual Studio is something I’ll never use again, it is kinda getting cross-platform compiling support, somewhat.

@Blerg, thanks for the info, that’s really helpful.

I’m curious why the -l is getting the default paths prepended… something I’ll need to Google.

Thanks for the help, I really really appreciate it!