将SDL2库与pkg-config相链接

时间:2016-08-11 15:43:26

标签: 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)$(...)在命令行中实际做了什么,它与```完全相同?

谢谢。

1 个答案:

答案 0 :(得分: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)。

相关问题