Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将SDL2库与pkg config链接_Linux_Linker_G++_Sdl_Pkg Config - Fatal编程技术网

Linux 将SDL2库与pkg config链接

Linux 将SDL2库与pkg config链接,linux,linker,g++,sdl,pkg-config,Linux,Linker,G++,Sdl,Pkg Config,我正在使用Ubuntu14.04LTS。我通过从源代码(method1)编译和使用sudo apt get install libsdl2-dev安装了SDL2库。 据我所知,前者在/usr/local/(lib和include)中安装了库和头,而后者在/usr/(lib和include)中安装了系统范围的库和头 当我试图编译一个简单的代码来测试功能时: #include <SDL.h> #include <stdio.h> int main(int argc, cha

我正在使用Ubuntu14.04LTS。我通过从源代码(method1)编译和使用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;
如果我更改为
#include
,则会出现以下错误:

/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.
sdltest.cpp:2:17:致命错误:SDL.h:没有这样的文件或目录
#包括
^
编译终止。
使用pkg config
g++sdltest.cpp-o outsdl$(pkg config--cflags--libs sdl2)时,唯一有效并成功编译的命令是

因此,我有以下问题:

1) 为什么需要pkg配置,编译和链接标志如何工作

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
添加到include搜索路径中,这样您就可以在代码中包含
SDL.h
(无SDL2子目录)

出现
未定义引用
错误的原因是您没有
-lSDL2
(它告诉编译器链接libSDL2)

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