我使用的是 Ubuntu 14.04LTS。我已经通过从源代码编译安装了 SDL2 库(方法 1https://askubuntu.com/questions/344512/what-is-the-general-procedure-to-install-development-libraries-in-ubuntu https://askubuntu.com/questions/344512/what-is-the-general-procedure-to-install-development-libraries-in-ubuntu)并使用 sudo apt-get install libsdl2-dev。
据我了解,前者将库和头文件安装在 /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;
using: 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) $(...) 在命令行中实际上做了什么,它与 `...` 完全相同吗?
谢谢。