简体   繁体   English

如何在我的程序中正确使用 SDL2?

[英]How do I use SDL2 in my programs correctly?

I want to make a game using SDL2, but I'm unable to compile and/or run my code, please help!我想使用 SDL2 制作游戏,但我无法编译和/或运行我的代码,请帮忙!

SDL2 is notoriously hard to set up, and it's often the first library aspiring game developers try to use. SDL2 是出了名的难以设置,它通常是有抱负的游戏开发人员尝试使用的第一个库。

This post is intended as a canonical duplicate for common problems with setting up SDL2.这篇文章旨在作为设置 SDL2 常见问题的规范副本。

This answer is about MinGW / GCC, and not Visual Studio.这个答案是关于 MinGW / GCC,而不是 Visual Studio。

This answer only applies to Windows.此答案仅适用于 Windows。


Common errors常见错误

The common errors are:常见的错误是:

  • SDL.h: No such file or directory (when compiling) SDL.h: No such file or directory (编译时)
  • Various SDL_main problems: "undefined reference to SDL_main", "conflicting types for SDL_main" or "number of arguments doesn't match prototype", etc. (when compiling or linking)各种SDL_main问题:“未定义对 SDL_main 的引用”、“SDL_main 的类型冲突”或“参数数量与原型不匹配”等(编译或链接时)
  • undefined reference to other functions (when linking)对其他函数的undefined reference (链接时)
  • DLL problems: (when running your program) DLL 问题:(运行程序时)
    • '??.dll' was not found
    • procedure entry point ... could not be located in ... , and other mysterious DLL-related errors procedure entry point ... could not be located in ... ,以及其他与 DLL 相关的神秘错误
    • The program seemingly doing nothing when launched该程序在启动时似乎什么也没做

This list is sorted from bad to good.此列表按从差到好排序。 If you change something and get a different error, use this list to tell if you made things better or worse.如果您更改某些内容并遇到不同的错误,请使用此列表来判断您是否使事情变得更好或更糟。


The preamble序言

0. Don't follow bad advice. 0. 不要听从坏建议。

Some resources will suggest you to do #define SDL_MAIN_HANDLED or #undef main .一些资源会建议您执行#define SDL_MAIN_HANDLED#undef main Don't blindly follow that advice, it's not how SDL2 is intended to be used.不要盲目地遵循这个建议,这不是 SDL2 的使用方式。

If you do everything correcty, it will never be necessary.如果你做的一切都是正确的,那将永远没有必要。 Learn the intended approach first.首先了解预期的方法。 Then you can research what exactly that does, and make an educated decision.然后你可以研究它到底做了什么,并做出有根据的决定。

1. Figure out how to compile directly from the console, you can start using an IDE and/or build system later. 1.弄清楚如何直接从控制台编译,您可以稍后开始使用IDE和/或构建系统。 If you're using an IDE, I suggest to first make sure you're able to compile your program directly from the console, to rule out any IDE configuration problems.如果您使用的是 IDE,我建议首先确保您能够直接从控制台编译程序,以排除任何 IDE 配置问题。 After you figure that out, you can use the same compiler options in your IDE.弄清楚之后,您可以在 IDE 中使用相同的编译器选项。

The same applies to build systems, such as CMake.这同样适用于构建系统,例如 CMake。

2. Download the right SDL2 files . 2. 下载正确的 SDL2 文件 Make sure you have the right files.确保您拥有正确的文件。 You need the archive called SDL2-devel-2.0.x-mingw.tar.gz from here .您需要来自此处的名为SDL2-devel-2.0.x-mingw.tar.gz的存档。

Extract it to any directory, preferably somewhere near your source code.将其解压缩到任何目录,最好是靠近源代码的某个位置。 Extracting into the compiler installation directory is often considered a bad practice (and so is copying them to C:\Windows , which is a horrible idea).提取到编译器安装目录通常被认为是一种不好的做法(将它们复制到C:\Windows也是如此,这是一个可怕的想法)。

3. Know the difference between compiler flags and linker flags . 3.了解编译器标志链接器标志之间的区别。 A "flag" is an option you specify in the command line when building your program. “标志”是您在构建程序时在命令行中指定的选项。 When you use a single command, eg g++ foo.cpp -o foo.exe , all your flags are added to the same place (to this single command).当您使用单个命令时,例如g++ foo.cpp -o foo.exe ,您的所有标志都将添加到同一个位置(添加到该单个命令)。

But when you build your program in two steps, eg:但是,当您分两步构建程序时,例如:

  • g++ foo.cpp -c -o foo.o (compiling) g++ foo.cpp -c -o foo.o (编译)
  • g++ foo.o -o foo.exe (linking) g++ foo.o -o foo.exe (链接)

you have to know which of the two commands to add a flag to.您必须知道要添加标志的两个命令中的哪一个。 Those are "compiler flags" and "linker flags" respectively.这些分别是“编译器标志”和“链接器标志”。

Most IDEs will require you to specify compiler and linker flags separately, so even if you use a single command now , it's good to know which flag goes where.大多数 IDE 将要求您分别指定编译器和链接器标志,因此即使您现在使用单个命令,最好知道哪个标志在哪里。

Unless specified otherwise, the order of the flags doesn't matter.除非另有说明,否则标志的顺序无关紧要。


SDL.h: No such file or directory

Or any similar error related to including SDL.h or SDL2/SDL.h .或与包含SDL.hSDL2/SDL.h相关的任何类似错误。

You need to tell your compiler where to look for SDL.h .您需要告诉编译器在哪里寻找SDL.h It's in the SDL files you've downloaded (see preamble).它位于您下载的 SDL 文件中(参见序言)。

Add -Ipath to your compiler flags, where path is the directory where SDL.h is located.-Ipath添加到编译器标志,其中pathSDL.h所在的目录。

Example: -IC:/Users/HolyBlackCat/Downloads/SDL2-2.0.12/x86_64-w64-mingw32/include/SDL2 .示例: -IC:/Users/HolyBlackCat/Downloads/SDL2-2.0.12/x86_64-w64-mingw32/include/SDL2 Relative paths work too, eg -ISDL2-2.0.12/x86_64-w64-mingw32/include/SDL2 .相对路径也可以,例如-ISDL2-2.0.12/x86_64-w64-mingw32/include/SDL2

Note that the path will be different depending on how you write the #include :请注意,路径将根据您编写#include的方式而有所不同:

  • If you do #include <SDL.h> , then the path should end with .../include/SDL2 (like above).如果您执行#include <SDL.h> ,则路径应以.../include/SDL2结尾(如上所示)。 This is the recommended way.这是推荐的方式。
  • If you do #include <SDL2/SDL.h> , then the path should end with .../include .如果您执行#include <SDL2/SDL.h> ,则路径应以.../include结尾。

Various SDL_main problems各种SDL_main问题

You can get several different errors mentioning SDL_main , such as undefined reference to SDL_main , or conflicting types for 'SDL_main' , or number of arguments doesn't match prototype , etc.您可能会遇到几个提到SDL_main的不同错误,例如undefined reference to SDL_main ,或conflicting types for 'SDL_main' ,或number of arguments doesn't match prototype等。

You need to have a main function.你需要有一个main的功能。 Your main function must look like int main(int, char **) .您的main功能必须看起来像int main(int, char **) NOT int main() and NOT void main() . NOT int main()NOT void main() This is a quirk of SDL2, related to it doing #define main SDL_main .这是 SDL2 的一个怪癖,与它执行#define main SDL_main相关。

Adding parameter names is allowed (and is mandatory in C), eg int main(int argc, char **argv) .允许添加参数名称(并且在 C 中是强制性的),例如int main(int argc, char **argv) Also the second parameter can be written as char *[] or with a name: char *argv[] .第二个参数也可以写为char *[]或名称: char *argv[] No other changes are allowed.不允许进行其他更改。

If your project has multiple source files, make sure to include SDL.h in the file that defines the main function, even if it doesn't otherwise use SDL directly.如果您的项目有多个源文件,请确保在定义main函数的文件中包含SDL.h ,即使它不直接使用 SDL。

Try to avoid #define SDL_MAIN_HANDLED or #undef main when solving this issue, see preamble for explanation.解决此问题时尽量避免#define SDL_MAIN_HANDLED#undef main ,请参阅序言以获取解释。


undefined reference to various functions undefined reference to

The error message will mention various SDL_... functions, and/or WinMain .错误消息将提及各种SDL_...函数和/或WinMain If it mentions SDL_main , consult the section "Various SDL_main problems" above.如果它提到SDL_main ,请参阅上面的“各种SDL_main问题”部分。

You need to add following linker flags: -lmingw32 -lSDL2main -lSDL2 -Lpath , where path is the directory where libSDL2.dll.a and libSDL2main.a (which you've downloaded) are located.您需要添加以下链接器标志: -lmingw32 -lSDL2main -lSDL2 -Lpath ,其中pathlibSDL2.dll.alibSDL2main.a (您已下载)所在的目录。 The order of the -l... flags matters. -l...标志的顺序很重要。 They must appear AFTER any .c / .cpp / .o files.它们必须出现在任何.c / .cpp / .o文件之后。

Example: -LC:/Users/HolyBlackCat/Desktop/SDL2-2.0.12/x86_64-w64-mingw32/lib .示例: -LC:/Users/HolyBlackCat/Desktop/SDL2-2.0.12/x86_64-w64-mingw32/lib Relative paths work too, eg -LSDL2-2.0.12/x86_64-w64-mingw32/lib .相对路径也可以,例如-LSDL2-2.0.12/x86_64-w64-mingw32/lib

When you use -l???当你使用-l??? , the linker will look for a file called lib???.dll.a or lib???.a (and some other variants), which is why we need to pass the location of those files. ,链接器将查找名为lib???.dll.alib???.a文件(以及其他一些变体),这就是为什么我们需要传递这些文件的位置。 libmingw32.a (corresponding to -lmingw32 ) is shipped with your compiler, so it already knows where to find it. libmingw32.a (对应于-lmingw32 )随您的编译器一起提供,因此它已经知道在哪里可以找到它。

I added all those flags and nothing changed, or I'm getting skipping incompatible X when searching for Y :我添加了所有这些标志并且没有任何改变,或者skipping incompatible X when searching for Y

You probably use the wrong SDL .a files.您可能使用了错误的 SDL .a文件。 The archive you downloaded contains two sets of files: i686-w64-mingw32 (32-bit) and x86_64-w64-mingw32 (64-bit).您下载的存档包含两组文件: i686-w64-mingw32 (32 位)和x86_64-w64-mingw32 (64 位)。 You must use the files matching your compiler, which can also be either 32-bit or 64-bit.您必须使用与您的编译器匹配的文件,它也可以是 32 位或 64 位。

Print (8*sizeof(void*)) to see if your compiler is 32-bit or 64-bit.打印(8*sizeof(void*))以查看您的编译器是 32 位还是 64 位。

Even if you think you use the right files, try the other ones to be sure.即使您认为您使用了正确的文件,也请尝试使用其他文件来确定。

Some MinGW versions can be switched between 32-bit and 64-bit modes using -m32 and -m64 flags (add them to both compiler and linker flags).一些 MinGW 版本可以使用-m32-m64标志在 32 位和 64 位模式之间切换(将它们添加到编译器和链接器标志)。

I get undefined reference to a specific function:我得到对特定函数的undefined reference

undefined reference to WinMain only • 仅对undefined reference to WinMain

There are several possibilities, all of which were covered in the previous section:有几种可能性,所有这些都在上一节中介绍过:

  • You forgot -lmingw32 and/or -lSDL2main linker flags.您忘记了-lmingw32和/或-lSDL2main链接器标志。
    You must use following linker flags, in this exact order, after any .c / .cpp / .o files: -lmingw32 -lSDL2main -lSDL2您必须在任何.c / .cpp / .o文件之后按此确切顺序使用以下链接器标志: -lmingw32 -lSDL2main -lSDL2
  • The libSDL2main.a file you use doesn't match your compiler (32-bit file with a 64-bit compiler, or vice versa).您使用的libSDL2main.a文件与您的编译器不匹配(32 位文件与 64 位编译器,反之亦然)。

Try to avoid #define SDL_MAIN_HANDLED or #undef main when solving this issue, see preamble for explanation.解决此问题时尽量避免#define SDL_MAIN_HANDLED#undef main ,请参阅序言以获取解释。

undefined reference to SDL_main only undefined reference to SDL_main

See the section "Various SDL_main problems" above.请参阅上面的“各种SDL_main问题”部分。


Nothing happens when I try run my app当我尝试运行我的应用程序时没有任何反应

Let's say you try to run your app, and nothing happens.假设您尝试运行您的应用程序,但没有任何反应。 Even if you try to print something at the beginning of main() , it's not printed.即使您尝试在main()的开头打印一些东西,它也不会被打印出来。

Windows has a nasty habit of not showing some DLL-related errors when the program is started from the console.当程序从控制台启动时,Windows 有一个讨厌的习惯,即不显示一些与 DLL 相关的错误。

If you were running your app from the console (or from an IDE), instead try double-clicking the EXE in the explorer.如果您从控制台(或 IDE)运行您的应用程序,请尝试在资源管理器中双击 EXE。 Most probably you'll now see some DLL-related error;您现在很可能会看到一些与 DLL 相关的错误; then consult one of the next sections.然后查阅下一节。


??.dll was not found ??.dll没有找到

Copy the .dll mentioned in the error message, and place it next to your .exe .复制错误消息中提到的.dll并将其放在您的.exe旁边。

If the DLL is called SDL2.dll , then it's in the SDL files you've downloaded (see preamble).如果 DLL 名为SDL2.dll ,那么它就在您下载的 SDL 文件中(参见序言)。 Be aware that there are two different SDL2.dll s: a 32-bit one (in the i686-w64-mingw32 directory), and a 64-bit one (in x86_64-w64-mingw32 ).请注意,有两种不同的SDL2.dll :一种是 32 位的(在i686-w64-mingw32目录中),另一种是 64 位的(在x86_64-w64-mingw32中)。 Get the right one, if necessary try both.找一个合适的,如果有必要,两个都试试。

Any other DLLs will be in your compiler's bin directory (the directory where gcc.exe is located).任何其他 DLL 都将位于编译器的bin目录( gcc.exe所在的目录)中。

You might need to repeat this process 3-4 times, this is normal.您可能需要重复此过程 3-4 次,这是正常的。

For an automatic way of determining the needed DLLs, see the next section.有关确定所需 DLL 的自动方法,请参阅下一节。


procedure entry point ... could not be located in ... and other cryptic DLL errors procedure entry point ... could not be located in ...和其他神秘的 DLL 错误

Your program needs several .dll s to run, and it found a wrong version of one, left over from some other program you have installed.你的程序需要几个.dll来运行,它发现了一个错误的版本,是你安装的其他程序遗留下来的。

It looks for DLLs in several different places, but the directory with the .exe has the most priority.它在几个不同的地方查找 DLL,但.exe的目录具有最高优先级。

You should copy all DLLs your program uses (except the system ones) into the directory where your .exe is located.您应该将程序使用的所有 DLL(除了系统的)复制到.exe所在的目录中。

A reliable way to get a list of needed DLLs is to blindly copy a bunch of DLLs, and then remove the ones that turn out to be unnecessary:获取所需 DLL 列表的可靠方法是盲目地复制一堆 DLL,然后删除那些被证明是不必要的:

  • Copy SDL2.dll .复制SDL2.dll It's in the SDL files you've downloaded (see preamble).它位于您下载的 SDL 文件中(参见序言)。 Be aware that there are two different SDL2.dll s: a 32-bit one (in the i686-w64-mingw32 directory), and a 64-bit one (in x86_64-w64-mingw32 ).请注意,有两种不同的SDL2.dll :一种是 32 位的(在i686-w64-mingw32目录中),另一种是 64 位的(在x86_64-w64-mingw32中)。 Get the right one, if necessary try both.找一个合适的,如果有必要,两个都试试。

  • Copy all DLLs from your compiler's bin directory (the directory where gcc.exe is located).从编译器的bin目录( gcc.exe所在的目录)复制所有 DLL。

  • Now your program should run, but we're not done yet.现在你的程序应该可以运行了,但我们还没有完成。

  • Download NTLDD (or some other program that displays a list of used DLLs).下载NTLDD (或其他显示已用 DLL 列表的程序)。 Run ntldd -R your_program.exe .运行ntldd -R your_program.exe

  • Any DLL not mentioned in its output should be removed from the current directory.任何在其输出中提及的 DLL 都应从当前目录中删除。 Your program uses everything that remains.你的程序使用了剩下的一切。

I ended up with following DLLs, expect something similar: SDL2.dll , libgcc_s_seh-1.dll , libstdc++-6.dll (C++ only), libwinpthread-1.dll .我最终得到了以下 DLL,期待类似的东西: SDL2.dlllibgcc_s_seh-1.dlllibstdc++-6.dll (仅限 C++)、 libwinpthread-1.dll

Can I determine the needed DLLs without copying excessive ones?我可以在不复制过多 DLL 的情况下确定所需的 DLL 吗?

Yes, but it's less reliable.是的,但它不太可靠。

Your program searches for DLLs in following locations, in this order:您的程序按以下顺序在以下位置搜索 DLL:

  • The directory where your .exe is located. .exe所在的目录。
  • C:\Windows , including some of its subdirectories. C:\Windows ,包括它的一些子目录。
  • The directories listed in PATH . PATH中列出的目录。

Assuming you (or some jank installer) didn't put any custom DLLs into C:\Windows , adding your compiler's bin directory to the PATH (preferably as the first entry) and either putting SDL2.dll in the same directory as the .exe or into some directory in the PATH should be enough for your program to work.假设您(或某些 jank 安装程序)没有将任何自定义 DLL 放入C:\Windows中,将编译器的bin目录添加到 PATH(最好作为第一个条目)并将SDL2.dll.exe放在同一目录中或进入 PATH 中的某个目录应该足以让您的程序工作。

If this works, you can then run ntldd without copying any DLLs beforehand, and copy only the necessary ones.如果这可行,您可以在不事先复制任何 DLL 的情况下运行ntldd ,并且只复制必要的那些。 The reason why you'd want to copy them at all at this point (since your app already works) is to be able to distribute it to others, without them having to install the compiler for its DLLs.您此时想要复制它们的原因(因为您的应用程序已经运行)是为了能够将其分发给其他人,而无需他们为其 DLL 安装编译器。 Skip any DLLs located outside of your compiler's bin directory (except for SDL2.dll ).跳过位于编译器bin目录之外的任何 DLL( SDL2.dll除外)。

Note that the possibility of having weird DLLs in C:\Windows is real.请注意,在C:\Windows中存在奇怪 DLL 的可能性是真实存在的。 Eg Wine tends to put OpenAL32.dll into C:\Windows , so if you try this process with OpenAL on Wine, it will fail.例如,Wine 倾向于将OpenAL32.dll放入C:\Windows中,因此如果您在 Wine 上使用 OpenAL 尝试此过程,它将失败。 If you're making a sciprt that runs ntldd automatically, prefer copying the DLLs (or at least symlinking them - I heard MSYS2 can emulate symlinks on Windows?).如果您正在制作自动运行ntldd的 sciprt,则更喜欢复制 DLL(或至少对它们进行符号链接——我听说 MSYS2 可以在 Windows 上模拟符号链接?)。

Can I make an EXE that doesn't depend on any DLLs?我可以制作一个不依赖任何 DLL 的 EXE 吗?

It's possible to make an .exe that doesn't depend on any (non-system) .dll s by using the -static linker flag, this is called "static linking".可以使用-static链接器标志创建一个不依赖于任何(非系统) .dll.exe ,这称为“静态链接”。 This is rarely done, and you shouldn't need to do this if you did the above steps correctly.很少这样做,如果您正确执行了上述步骤,则不需要这样做。 This requires some additional linker flags;这需要一些额外的链接器标志; they are listed in file ??-w64-mingw32/lib/pkgconfig/sdl2.pc shipped with SDL, in the Libs.private section.它们列在Libs.private部分的 SDL 随附的文件??-w64-mingw32/lib/pkgconfig/sdl2.pc中。 Notice that there are two files, for x32 and x64 respectively.请注意,有两个文件,分别用于 x32 和 x64。


How do I distribute my app to others?如何将我的应用分发给其他人?

Follow the steps in the previous section, titled procedure entry point ... could not be located in ... .按照上一节中的步骤操作,标题为procedure entry point ... could not be located in ...


A saner alternative?一个更理智的选择?

There is MSYS2 .MSYS2

It has a package manager that lets you download prebuilt libraries, and, as a bonus, a fresh version of the compiler.它有一个包管理器,可让您下载预构建的库,并且作为奖励,还有一个新版本的编译器。

Install SDL2 from its package manager.从其包管理器安装 SDL2。 Use a tool called pkg-config (also from the package manager) to automatically determine all necessary flags ( pkg-config --cflags SDL2 for compiler flags, pkg-config --libs SDL2 for linker flags).使用名为pkg-config的工具(也来自包管理器)自动确定所有必要的标志( pkg-config --cflags SDL2用于编译器标志, pkg-config --libs SDL2用于链接器标志)。

This is the same experience as you would have on Linux (maybe except for some DLL management hassle).这与您在 Linux 上的体验相同(可能除了一些 DLL 管理麻烦之外)。


Bonus - Other problems奖金 - 其他问题

  • Q: My program always opens a console window when I run it, how do I hide it?问:我的程序在运行时总是打开一个控制台窗口,如何隐藏它?

    • A: Add -mwindows to the linker flags.答:将-mwindows添加到链接器标志。
  • Q: I get error 'SDL_VideoMode' wasn't declared in this scope .问:我收到错误'SDL_VideoMode' wasn't declared in this scope

    • A: SDL_VideoMode is from SDL1.2, it's not a part of the newer SDL2. A: SDL_VideoMode来自 SDL1.2,它不是新 SDL2 的一部分。 Your code was written for the outdated version of SDL.您的代码是为过时的 SDL 版本编写的。 Find a better tutorial that deals specifically with SDL2.查找专门处理 SDL2 的更好的教程。
  • Q: My program has the default file icon , but I want a custom one.问:我的程序有默认的文件图标,但我想要一个自定义的。

    • A: Your icon must be in the .ico format.答:您的图标必须是.ico格式。 If your graphics editor doesn't support it, make a series of .png s of common sizes (eg 16x16, 32x32, 48x48, 64x64), then convert them to a single .ico using ImageMagick : magick *.png result.ico (or with convert instead of magick ).如果您的图形编辑器不支持它,请制作一系列常见尺寸的.png (例如 16x16、32x32、48x48、64x64),然后使用ImageMagick将它们转换为单个.icomagick *.png result.ico (或使用convert而不是magick )。

      Create a file with the .rc extension (say, icon.rc ), with following contents MyIconName ICON "icon.ico" (where MyIconName is an arbitrary name, and "icon.ico" is the path to the icon).创建一个扩展名为.rc的文件(例如icon.rc ),其内容如下MyIconName ICON "icon.ico" (其中MyIconName是任意名称,而"icon.ico"是图标的路径)。 Convert the file to an .o using windres -O res -i icon.rc -o icon.o (the windres program is shipped with your compiler).使用windres -O res -i icon.rc -o icon.o将文件转换为.owindres程序随编译器一起提供)。 Specify the resulting .o file when linking, eg g++ foo.cpp icon.o -o foo.exe .链接时指定生成的.o文件,例如g++ foo.cpp icon.o -o foo.exe

      Recent versions of SDL2 have a nice property of using the same icon as the window icon, so you don't have to use SDL_SetWindowIcon .最近版本的 SDL2 有一个很好的属性,即使用与窗口图标相同的图标,因此您不必使用SDL_SetWindowIcon

A solution for Visual Studio: Visual Studio 的解决方案:

Why not use a package manager?为什么不使用包管理器? I use vcpkg , and it makes super easy to consume 3rd party libraries.我使用vcpkg ,它使使用 3rd 方库变得超级容易。 Grab the vcpkg source, and extract it to a safe place, like C:/ , then run its bootstrap script bootstrap-vcpkg.bat , this will generate vcpkg executable.获取 vcpkg 源,并将其解压缩到安全的地方,例如C:/ ,然后运行其引导脚本bootstrap-vcpkg.bat ,这将生成vcpkg可执行文件。 Then run vcpkg integrate install to make libraries installed with vcpkg available in Visual Studio.然后运行vcpkg integrate install以使与 vcpkg 一起安装的库在 Visual Studio 中可用。

Search for the library you need:搜索您需要的库:

vcpkg search sdl

imgui[sdl2-binding]                   Make available SDL2 binding
libwebp[vwebp-sdl]                    Build the vwebp viewer tool.
magnum[sdl2application]               Sdl2Application library
sdl1                 1.2.15#12        Simple DirectMedia Layer is a cross-platform development library designed to p...
sdl1-net             1.2.8-3          Networking library for SDL
sdl2                 2.0.12-1         Simple DirectMedia Layer is a cross-platform 
...

Install it with: vcpkg install sdl2 .安装它: vcpkg install sdl2

Now you just need include SDL2 headers, and everything will work out of the box.现在您只需要包含 SDL2 标头,一切都可以开箱即用。 The library will be linked automatically.该库将自动链接。

You can learn more about vcpkg here .您可以在此处了解有关 vcpkg 的更多信息。

On Mac this is what I follow for XCode (must install g++):在 Mac 上,这是我为 XCode 所遵循的(必须安装 g++):

sdl linking: sdl链接:

g++ main.cpp -o main $(sdl2-config --cflags --libs)

XCODE project steps: XCODE项目步骤:

  1. open terminal app (macOS)打开终端应用程序 (macOS)

  2. BUILD SETTINGS (select 'all' and 'combined' search bar enter: "search") BUILD SETTINGS(选择“全部”和“组合”搜索栏输入:“搜索”)

  3. click on "header search paths(way right side click)单击“标题搜索路径(右侧单击方式)

  4. add: /usr/local/include添加: /usr/local/include

  5. BUILD PHASES --> LINK BINARY LIBRARIES (click plus)构建阶段 --> 链接二进制库(单击加号)

  6. type in SDL --> click "add other"输入SDL --> 点击“添加其他”

  7. press: command + SHIFT + g (to bring search bar)按: command + SHIFT + g (带上搜索栏)

  8. type in: usr/local/Cellar输入: usr/local/Cellar

  9. navigate to: SDL2 -->2.0.8 -->lib --> libSDL2-2.2.0.dylib (make sure not shortcut)导航到:SDL2 -->2.0.8 -->lib --> libSDL2-2.2.0.dylib(确保不是快捷方式)

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

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