如何在MAC中从/Library/Framework文件夹中包含C++文件

17

我想使用SDL,我在/Library/Frameworks中有一个名为SDL2.framework的文件夹。我想在我的项目中包含文件SDL.h。我该怎么做?我的代码如下:

// Example program:
// Using SDL2 to create an application window

#include <SDL.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    SDL_Window *window;                    // Declare a pointer
    SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2
    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );
    // Check that the window was successfully made
    if (window == NULL) {
        // In the event that the window could not be made...
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }
    // The window is open: enter program loop (see SDL_PollEvent)
    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example
    // Close and destroy the window
    SDL_DestroyWindow(window);
    // Clean up
    SDL_Quit();
    return 0;
}

我收到的错误信息是:

Aarons-MacBook-Air:SDL aaron$ g++ main.cpp
main.cpp:4:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
          ^ 1 error generated.

我该如何正确地引用SDL文件?它在SDL2.frameworkheadersSDL.h内部...


1
你是否已经把框架添加到项目中了? - Grady Player
我正在使用VIM编辑器。没有其他需要补充的内容。整个代码库如上所示。我尝试不使用任何XCODE或其他构建工具,只是尝试在命令行中使用g++进行编译。@Grady Player - ILikeTurtles
@GradyPlayer 正在尝试将您添加到此中。 - ILikeTurtles
1
@mbaros -framework 框架名称 - Grady Player
1
@GradyPlayer 谢谢。它起作用了...!!! - mbaros
显示剩余5条评论
2个回答

19
您需要为此编写一个构建脚本,但重要的部分如下: -I/usr/local/include或其他安装头文件的位置。
我使用了Homebrew: brew install sdl2 这将把库放在/usr/local/Cellar/中。
因此,如果您需要指定lib路径,还需要添加: -L/usr/local/lib -lSDL2 我还将您的include行更改为#include <SDL2/SDL.h>

我的Make文件现在看起来像这样 - all: g ++ main.cpp -I / usr / local / include -L / usr / local / lib -lSDL2 - ILikeTurtles

3

你的头文件位于Headers文件夹下,因此为了正确地包含它:

clang++ -std=c++11 -stdlib=libc++ -I/Library/Frameworks/SDL2.framework/Headers/

但我建议使用Homebrew进行安装:
brew install sdl2

Homebrew将会在/usr/local/lib和/usr/local/include目录下安装SDL2的libSDL2.a文件,因此您只需要使用-L标志引用这个库路径,并使用-I标志添加在/usr/local/include目录中进行搜索:
clang++ -std=c++11 -stdlib=libc++ main.cpp -I/usr/local/include -L/usr/local/lib -lSDL2 -o programfile

并包括:

#include <SDL2/SDL.h>

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