【问题标题】:linking SDL2 libraries with pkg-config使用 pkg-config 链接 SDL2 库
【发布时间】:2016-08-11 15:43:25
【问题描述】:

我使用的是 Ubuntu 14.04LTS。我已经通过从源代码(method1 https://askubuntu.com/questions/344512/what-is-the-general-procedure-to-install-development-libraries-in-ubuntu)编译和使用 sudo apt-get install libsdl2-dev 安装了 SDL2 库。 据我了解,前者将库和头文件安装在 /usr/local/(lib 和 include) 中,而后者将它们安装在系统范围内 /usr/(lib 和 include)。

当我尝试编译一个简单的代码来测试功能时:

#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 created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

// The window is open: could enter program loop here (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;

使用:g++ sdl_test.cpp -o sdlout

编译器输出:

sdltest.cpp:2:17: fatal error: SDL.h: No such file or directory
#include <SDL.h>
                ^
compilation terminated.

如果我更改为#include &lt;SDL2/SDL.h&gt;,我会收到以下错误:

/tmp/cc05JSKn.o: In function `main':
sdltest.cpp:(.text+0x15): undefined reference to `SDL_Init'
sdltest.cpp:(.text+0x3a): undefined reference to `SDL_CreateWindow'
sdltest.cpp:(.text+0x4a): undefined reference to `SDL_GetError'
sdltest.cpp:(.text+0x6d): undefined reference to `SDL_Delay'
sdltest.cpp:(.text+0x79): undefined reference to `SDL_DestroyWindow'
sdltest.cpp:(.text+0x7e): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

哪些是基本功能,所以我假设共享对象库没有正确链接。

我也试过:g++ -Wall sdltest.cpp -o outsdl -I /usr/local/include -L /usr/local/lib 来指定路径,但我又得到了:

sdltest.cpp:2:17: fatal error: SDL.h: No such file or directory
#include <SDL.h>
                ^
compilation terminated.

唯一有效并成功编译的命令是使用 pkg-config g++ sdltest.cpp -o outsdl $(pkg-config --cflags --libs sdl2)

因此,我有以下问题:

1) 为什么需要 pkg-config 以及编译和链接标志如何工作?

2) 是否可以做一些其他的事情来使编译命令更简单?

3) (如果之前没有解释)pkg-config 和使用 -I 和 -L 不起作用有什么区别?

4) $(...) 在命令行中实际做了什么,和`...`完全一样吗?

谢谢。

【问题讨论】:

    标签: linux linker g++ sdl pkg-config


    【解决方案1】:

    pkg-config 命令或多或少是一种跨平台或跨发行版的方式,可以为编译器提供正确的标志,使其能够找到头文件和库文件。这样,您的系统可以将文件存储在不同的位置,并且每个人都可以使用相同的命令来编译您的代码。它还有助于解决您尝试使用的库的任何特殊要求。

    使用$() 与使用反引号相同,因此您可以执行括号内的内容,以查看传递给编译器的额外参数以使其工作。这是我在运行pkg-config --cflags --libs sdl2 时在我的机器上得到的结果:

    -D_REENTRANT -I/usr/include/SDL2 -lSDL2
    

    您获得SDL.h: No such file or directory 的原因是pkg-config-I/usr/include/SDL2 添加到包含搜索路径中,因此您可以在代码中包含SDL.h(不包含SDL2 子目录)。

    您收到undefined reference 错误的原因是因为您没有-lSDL2(它告诉编译器链接libSDL2)。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2012-06-04
      • 2011-04-27
      • 2013-07-11
      相关资源
      最近更新 更多