Debian

使用 gcc 編譯標誌

  • June 5, 2016

我試圖了解如何正確設置 gcc 以在我的環境變數中查找內容。

目前,我編譯了一些程式碼 SDL,並將其添加到我的 .bashrc 中,並獲取了該 .bashrc 的來源。

這是一個簡單的 hello 程序。

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <stdlib.h>
#include <stdio.h>

SDL_Window* window;
SDL_GLContext* main_context;

int main(int argc, char** argv) {
   printf("hello world %d  %c \n", argc, argv[0][argc]);


   if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
       SDL_Log("sdl failed to core_engine_init, %s", SDL_GetError());
       SDL_Quit();
       return -1;
   }

   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
                       SDL_GL_CONTEXT_PROFILE_CORE);
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

   window = SDL_CreateWindow(
       "title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300,
       SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);


   if (NULL == window) {
       SDL_Log("SDL Failed to create window, %s", SDL_GetError());
       SDL_Quit();
       return -1;
   }

   main_context = SDL_GL_CreateContext(window);
   if (NULL == main_context) {
       SDL_Log("SDL failed to create main context, %s", SDL_GetError());
       SDL_Quit();
       return -1;
   }

   return 0;
}

試圖用我編譯這個gcc -o main main.c我得到這些錯誤:

blubee$ gcc -o main main.c
/tmp/cc5hRcaO.o: In function `main':
main.c:(.text+0x3e): undefined reference to `SDL_Init'
main.c:(.text+0x47): undefined reference to `SDL_GetError'
main.c:(.text+0x59): undefined reference to `SDL_Log'
main.c:(.text+0x5e): undefined reference to `SDL_Quit'
main.c:(.text+0x77): undefined reference to `SDL_GL_SetAttribute'
main.c:(.text+0x86): undefined reference to `SDL_GL_SetAttribute'
main.c:(.text+0x95): undefined reference to `SDL_GL_SetAttribute'
main.c:(.text+0xa4): undefined reference to `SDL_GL_SetAttribute'
main.c:(.text+0xb3): undefined reference to `SDL_GL_SetAttribute'
main.c:(.text+0xd8): undefined reference to `SDL_CreateWindow'
main.c:(.text+0xf0): undefined reference to `SDL_GetError'
main.c:(.text+0x102): undefined reference to `SDL_Log'
main.c:(.text+0x107): undefined reference to `SDL_Quit'
main.c:(.text+0x11d): undefined reference to `SDL_GL_CreateContext'
main.c:(.text+0x135): undefined reference to `SDL_GetError'
main.c:(.text+0x147): undefined reference to `SDL_Log'
main.c:(.text+0x14c): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

添加 SDL2 連結器標誌仍會返回錯誤:

blubee$ gcc -lSDL2 -o main main.c
/usr/bin/ld: cannot find -lSDL2
collect2: error: ld returned 1 exit status

這個命令編譯一切都很好

blubee$ gcc -I/opt/SDL2/include/SDL2 main.c -o main -L/opt/SDL2/lib -l
SDL2

問題是我已經將這些路徑添加到我的 .bashrc 中,儘管我可能做錯了。這是我的 .bashrc

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/SDL2/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/SDL_IMAGE/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/SDL_TTF/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/SDL_MIXER/lib

export LD_RUN_PATH=$LD_RUN_PATH:/opt/SDL2/lib
export LD_RUN_PATH=$LD_RUN_PATH:/opt/SDL_IMAGE/lib
export LD_RUN_PATH=$LD_RUN_PATH:/opt/SDL_TTF/lib
export LD_RUN_PATH=$LD_RUN_PATH:/opt/SDL_MIXER/lib

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/SDL2/include/SDL2
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/SDL_IMAGE/include/SDL2
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/SDL_TTF/include/SDL2
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/SDL_MIXER/include/SDL2

呼應這些環境變數表明它們存在並且應該可以工作,但事實並非如此。

我在這個設置中做錯了什麼?

您不需要任何環境變數,只需傳入 SDL2 希望您使用的正確 cflags 和 ldflags:

gcc main.c `pkg-config --cflags sdl2` -o main `pkg-config --libs sdl2`

gcc main.c `sdl2-config --cflags` -o main `sdl2-config --libs`

請記住:CFLAGS 在 LDFLAGS 之前,而 LDFLAGS(以及帶有 -l 的庫規範)在最後。

SDL2 預裝了 sdl2-config 腳本。您需要將 PATH 設置為它所在的目錄才能成功呼叫它:

export PATH=/opt/SDL2/bin:$PATH

如果您將直接執行每個 *-config 命令,您會看到它們只是為您輸出正確的 cflags 和 ldflags。這是因為使用這些腳本的庫通常比指定單個 -I/-L 參數更大,並且為它們指定單個 -I/-L 參數是不可移植的,因為將來可以增加此類參數的數量。

而且您不應該將每個軟體包都安裝在它自己的目錄中。例如,將所有內容安裝到/usr/local其中,然後您甚至不需要指定任何內容(大多數發行版會/usr/local自動指向您)。

引用自:https://unix.stackexchange.com/questions/242061