简体   繁体   English

如何编译用 C 编写的示例 SDL 程序?

[英]How to compile an example SDL program written in C?

I'm getting started with SDL and C programming.我正在开始使用 SDL 和 C 编程。 I have experience with other programming languages, but linking/compiling libraries in C is new to me.我有使用其他编程语言的经验,但在 C 中链接/编译库对我来说是新的。 I am using Mac 10.8 and have installed latest stable 2.0 using the instructions in the read me ( ./configure; make; make install ).我正在使用 Mac 10.8 并使用自述文件中的说明安装了最新的稳定版 2.0( ./configure; make; make install )。 Here is the sample code that I am trying to compile:这是我尝试编译的示例代码:

#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"

int main(void)
{
  if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
    fprintf(stderr, "\nUnable to initialize SDL:  %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);

  return 0;
}

When I try to compile my script using gcc example.c , I get an error:当我尝试使用gcc example.c编译我的脚本时,出现错误:

example.c:3:17: error: SDL.h: No such file or directory
example.c: In function ‘main’:
example.c:7: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: ‘SDL_INIT_TIMER’ undeclared (first use in this function)
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:11: error: ‘SDL_Quit’ undeclared (first use in this function)

I tried searching the wiki, and tutorials, and any kind of documentation that I could find, but I could not find any example anywhere that showed how to properly compile a C program that uses SDL.我尝试搜索 wiki、教程以及我能找到的任何类型的文档,但我在任何地方都找不到任何显示如何正确编译使用 SDL 的 C 程序的示例。

What do I need to do to compile this program?我需要做什么来编译这个程序?

A general hint for C beginners: read error logs top-down: often fixing first error will resolve all other. C 初学者的一般提示:自上而下读取错误日志:通常修复第一个错误将解决所有其他错误。 In your case first error is:在您的情况下,第一个错误是:

example.c:3:17: error: SDL.h: No such file or directory

As others have said, you need to instruct gcc where to find SDL.h .正如其他人所说,您需要指示gcc在哪里找到SDL.h You can do this by providing -I option.您可以通过提供-I选项来做到这一点。

To check where SDL.h is installed by default I would issue要检查默认情况下SDL.h的安装位置,我会发出

./configure --help

in the directory where you did build libsdl .在您构建libsdl的目录中。 Then look for --prefix , under Linux default prefix is often /usr/local .然后查找--prefix ,在 Linux 下默认前缀通常是/usr/local To compile your example I would issue (on Linux):要编译您的示例,我将发出(在 Linux 上):

gcc example.c -I/usr/local/include

But the above command compiles and links the code.但是上面的命令编译链接了代码。 After successful compilation, gcc would throw another bunch of errors, one of them being undefined reference .成功编译后, gcc会抛出另一堆错误,其中一个是undefined reference

To prevent that, full command line to build your example (on Linux at least) would be:为了防止这种情况,构建示例的完整命令行(至少在 Linux 上)将是:

gcc example.c -I/usr/local/include -L/usr/local/lib -lSDL

Where:哪里:

  • -I points compiler to directory with SDL.h , -I编译器指向带有SDL.h目录,
  • -L points linker to directory with libSDL.a (or libSDL.so ), -L链接器指向带有libSDL.a (或libSDL.so )的目录,
  • -l instructs linker to link with library, in our case libSDL.a or libSDL.so . -l指示链接器链接库,在我们的例子中是libSDL.alibSDL.so Note that the lib prefix and .a / .so suffix is missing.请注意,缺少lib前缀和.a / .so后缀。

Please note that I didn't check this instruction, even on Linux machine (on the other hand I have no access to Mac OS machine).请注意,即使在 Linux 机器上,我也没有检查此指令(另一方面,我无法访问 Mac OS 机器)。

One more thing: by default binary with the compiled and linked example will be called a.out .还有一件事:默认情况下,带有编译和链接示例的二进制文件将被称为a.out To change that you can provide -o option to gcc .要更改它,您可以为gcc提供-o选项。

I found out you can use a tool called pkg-config to find out the compiler flags expected for a specific library.我发现您可以使用名为pkg-config的工具来找出特定库所需的编译器标志。

$ pkg-config --cflags --libs sdl2
-D_THREAD_SAFE -I/usr/local/include/SDL2 -I/usr/X11R6/include -L/usr/local/lib -lSDL2

$ gcc example.c $(pkg-config --cflags --libs sdl2)

If you are using a Makefile , you need to prefix the command with shell :如果您使用的是Makefile ,则需要在命令前加上shell前缀:

all:
    gcc example.c $(shell pkg-config --cflags --libs sdl2)

tl;dr tl;博士

sudo apt install libsdl1.2-dev

You are missing the SDL library files.您缺少 SDL 库文件。 Just instal them and everything should work out of the box.只需安装它们,一切都应该开箱即用。

Different versions ans extensions不同的版本和扩展

There are multiple versions of SDL, make sure you install the one that is required by your application. SDL 有多个版本,请确保安装应用程序所需的版本。 There are also additional libraries (called projects) for SDL that you also need to install if you use their features.如果您使用它们的功能,还需要安装 SDL 的其他库(称为项目) For example if you use TTF fonts or image related functionalities.例如,如果您使用 TTF 字体或图像相关功能。 Just press TAB twice after you typed in sudo apt install libsdl to see all the available packages.输入sudo apt install libsdl后,只需按两次 TAB 即可查看所有可用的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM