2015-08-30 50 views
2

这是个不错的问题。基本上我似乎无法使用SDL2外部库在OSX(Yosemite)上编译基本的Hello World程序。如何在OS X上使用SDL2编译C++程序?

我想在控制台上做这件事,没有任何IDE的帮助。我已经安装了SDL 2.0.3,它位于/Library/Frameworks/SDL2.framework路径上。

我的主要文件是这样的:

#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; 
} 

而且我也有一个生成文件(从一个例子,我发现某处拍摄),看起来像这样:

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 [email protected] $< 

而且仅此而已。当我运行make时,我收到此回复:

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 

那么,我可以得到一些指导吗?我从哪里开始有点失落。我以前从未在OSX或任何基于Unix的操作系统上编译过程序。

我搜索了我失踪的那个root-config的东西,而且似乎我必须安装一个名为Root的库。我做的。解压缩到一个目录,不知道从哪里去。

在此先感谢。

+0

这里是我的SDL2例如,适用于OSX,但我使用CMake的构建:https://github.com/nickdesaulniers/sdl2web –

回答

3

您发现的makefile具有ROOT数据分析框架的变量,而不是SDL2。

尝试运行

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

上手。

0

正如amitp所说,您正试图为ROOT框架使用编译器和链接器标志。试试这个:

CFLAGS += -F/Library/Frameworks 
LDFLAGS += -F/Library/Frameworks 
LIBS += -framework SDL2 
相关问题