2013-11-29 13 views
8

我正在嘗試使用SDL。我在/Library/Frameworks中有一個名爲SDL2.framework的文件夾。我想在我的項目中包含文件SDL.h。我該怎麼做呢?我的代碼如下所示:如何在MAC中的/ Library/Framework文件夾中包含C++文件

// 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

你的架構添加到項目中? –

+0

我正在使用VIM。沒有什麼可以添加到它。整個代碼庫如上所示。我試圖不使用任何種類的XCODE或其他建築工具。試圖在命令行中使用g ++構建。 @Grady播放器 – ILikeTurtles

+0

@GradyPlayer嘗試添加到此。 – ILikeTurtles

回答

10

你會希望構建腳本這個很明顯,但重要的部分是:

-I/usr/local/include或其它地方得到安裝在您的頭。

我用家釀:

brew install sdl2

這使圖書館在/usr/local/Cellar/

所以如果你需要指定庫路徑也將增加:

-L/usr/local/lib -lSDL2

我也將您的包含行更改爲#include <SDL2/SDL.h>

+0

我的Make文件現在看起來像這樣 - all: \t g ++ main。cpp -I/usr/local/include -L/usr/local/lib -lSDL2 – ILikeTurtles

1

你的頭文件是頭文件夾下,所以爲了這包括正確:

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

但我建議用自制軟件安裝:

brew install sdl2 

自制軟件將安裝SDL2 libSDL2.a文件在/ usr/local/lib和/ usr/local/include下,所以你只需要使用-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> 
相關問題