How to compile a C++ program with SDL2 on OS X?

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

This is a pretty noob question. Basically I can't seem to be able to compile a basic Hello World program on OSX (Yosemite) using the SDL2 external library.

I'm trying to do this on console, without the help of any IDEs. I already installed the SDL 2.0.3 and it is located on the /Library/Frameworks/SDL2.framework path.

My main file looks like this:

#include <SDL2/SDL.h> #include <stdio.h>  bool init(); void close();  SDL_Window* gameWindow = NULL; SDL_Surface* gameScreenSurface = NULL;  bool init() {     ... }  void close() {     ... }  int main( int argc, char** argv) {     if( !init() )     {         printf( "Failed to initialize!\n" );     }     else     {       SDL_Delay( 2000 );     }     close();     return 0; } 

And I also have a makefile (taken from an example I found somewhere) that looks like this:

CC = g++ LDFLAGS = -g -Wall  PROGNAME = doom SOURCES = main.cpp INCLUDES =  OBJECTS = $(subst %.cc, %.o, $(SOURCES)) ROOTCFLAGS  := $(shell root-config --cflags) ROOTLIBS    := $(shell root-config --libs) ROOTGLIBS   := $(shell root-config --glibs) ROOTLIBS    := $(shell root-config --nonew --libs) CFLAGS      += $(ROOTCFLAGS) LIBS        += $(ROOTLIBS)  all: doom  $(PROGNAME): $(OBJECTS)     $(CC) $(LDFLAGS) -o doom $(OBJECTS) $(LIBS)  %.o : %.cc $(INCLUDES)     $(CC) ${CFLAGS} -c -g -o $@ $< 

And that's about it. When I run make, I receive this response:

make: root-config: Command not found make: root-config: Command not found make: root-config: Command not found make: root-config: Command not found g++ -g -Wall -o doom  main.cpp Undefined symbols for architecture x86_64:   "_SDL_CreateWindow", referenced from:       init() in main-8b6fae.o   "_SDL_Delay", referenced from:       _main in main-8b6fae.o   "_SDL_DestroyWindow", referenced from:       close() in main-8b6fae.o   "_SDL_GetError", referenced from:       init() in main-8b6fae.o   "_SDL_GetWindowSurface", referenced from:       init() in main-8b6fae.o   "_SDL_Init", referenced from:       init() in main-8b6fae.o   "_SDL_Quit", referenced from:       close() in main-8b6fae.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [doom] Error 1 

So, can I get some guidance please? I'm a little lost on where to start. I have never compiled a program on OSX or any Unix-based OS before.

I searched for that root-config thing that I'm missing, and It seems that I have to install a library called Root. I did it. Uncompressed it on a directory, don't know where to go from there.

Thanks in advance.

回答1:

The makefile you found has variables for the ROOT data analysis framework, and not for SDL2.

Try running

g++ $(sdl2-config --cflags) -g -Wall -o doom  main.cpp $(sdl2-config --libs) 

to get started.



回答2:

As amitp said, you are trying to use compiler and linker flags for the ROOT framework. Try this instead:

CFLAGS  += -F/Library/Frameworks LDFLAGS += -F/Library/Frameworks LIBS    += -framework SDL2 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!