【问题标题】:How do you include files in C++ from the /Library/Framework folder in MAC如何在 MAC 的 /Library/Framework 文件夹中包含 C++ 中的文件
【发布时间】:2013-12-15 03:52:21
【问题描述】:

我正在尝试使用 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...

【问题讨论】:

  • 你在项目中添加了框架吗?
  • 我正在使用 VIM。没有什么可添加的。整个代码库如上所示。我试图不使用任何类型的 XCODE 或其他构建工具。只是尝试在命令行中使用 g++ 构建。 @格雷迪播放器
  • @GradyPlayer 正在尝试将您添加到此。
  • @mbaros -framework FrameworkName
  • @GradyPlayer 谢谢。成功了……!!!

标签: c++ macos compilation include sdl


【解决方案1】:

你显然会想要为此制作一个构建脚本,但重要的部分是:

-I/usr/local/include 或安装您的标头的任何位置。

我用的是自制的:

brew install sdl2

将库放入/usr/local/Cellar/

因此,如果您需要指定 lib 路径,您还将添加:

-L/usr/local/lib -lSDL2

我还将您的包含行更改为#include &lt;SDL2/SDL.h&gt;

【讨论】:

  • 我的 Make 文件现在看起来像这样 -- all: g++ main.cpp -I/usr/local/include -L/usr/local/lib -lSDL2
【解决方案2】:

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

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

但我建议使用自制软件安装:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多