如何在OS X上使用SDL2编译C++程序?

3

这是一个相当初级的问题。基本上,我似乎无法使用SDL2外部库在OSX(Yosemite)上编译基本的Hello World程序。

我正在尝试在控制台上完成此操作,而不需要任何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;
}

我还有一个类似于下面这样的makefile文件(是从某个地方找来的例子):

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 $@ $<

就是这样了。当我运行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的库。我已经安装了它,并将其解压到一个目录中,但不知道接下来该怎么做。

提前感谢。


这是我的 SDL2 示例,在 OSX 上可行,但我使用 CMake 进行构建: https://github.com/nickdesaulniers/sdl2web - Nick Desaulniers
2个回答

4
你找到的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接