【问题标题】:How to compile a C++ program with SDL2 on OS X?如何在 OS X 上使用 SDL2 编译 C++ 程序?
【发布时间】:2015-11-24 10:04:02
【问题描述】:

这是一个非常菜鸟的问题。基本上我似乎无法使用 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 的库。我做到了。解压到一个目录,不知道从哪里去。

提前致谢。

【问题讨论】:

标签: c++ macos compilation sdl-2


【解决方案1】:

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

尝试运行

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

开始吧。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 1970-01-01
      • 2014-12-12
      • 2017-07-07
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2015-05-17
      • 2016-05-22
      相关资源
      最近更新 更多