linux - 将 SDL2 库与 pkg-config 链接

标签 linux linker g++ sdl pkg-config

我使用的是 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 <SDL2/SDL.h>我收到以下错误:

/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) $(...) 在命令行中实际执行什么操作,它与 `...` 完全相同吗?

谢谢。

最佳答案

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

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

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

您收到SDL.h:没有这样的文件或目录的原因是因为pkg-config添加了-I/usr/include/SDL2 到包含搜索路径,以便您可以在代码中包含 SDL.h(不包含 SDL2 子目录)。

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

关于linux - 将 SDL2 库与 pkg-config 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900855/

相关文章:

node.js - 我无法在 Linux VPS 上创建 HTTP/HTTPS 服务器

python - cx_freeze 不会使 Python 应用程序独立

c++ - 为什么我在链接期间得到 "undefined reference to ` glibtop_init'"?

iphone - 导入第三方项目时出现链接器错误

c++ - 微小的崩溃程序

c++ - 禁用特定包含文件的警告

linux - 从 Windows 到 Linux 服务器的 FTP 上传时附加的 CRLF 标签

linux - 如何解决 ip_options_compile, EXPORT_SYMBOL

c++ - Incredibuild 链接

audio - 用mingw编译和链接openAL?