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

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

这是一个非常菜鸟的问题。基本上我似乎无法使用 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 的 OS 上编译过程序。

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

提前致谢。

您找到的 makefile 包含用于 ROOT 数据分析框架的变量,而不是用于 SDL2 的变量。

尝试运行

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

开始。

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

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