如何在 Windows 上静态编译 SDL 游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/115813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to statically compile an SDL game on Windows
提问by Sec
I have been trying to produce a statically linked "single binary" version of my game for windows. I want to link with sdl, sdl_image and sdl_mixer which in turn pull in a few support libraries. Unfortunately I haven't found a way to get them all to compile and link using cygwin/mingw/gcc. As far as I can tell all existing public versions are only shared libraries / dlls.
我一直在尝试为我的 Windows 游戏制作一个静态链接的“单一二进制”版本。我想与 sdl、sdl_image 和 sdl_mixer 链接,它们又会引入一些支持库。不幸的是,我还没有找到一种方法让它们全部使用 cygwin/mingw/gcc 进行编译和链接。据我所知,所有现有的公共版本都只是共享库/dll。
Please note that I'm not talking about licencing here. The source will be open thus the GPL/LGPLness of sdl is not relevant.
请注意,我不是在这里谈论许可。源代码将是开放的,因此与 sdl 的 GPL/LGPL 无关。
回答by Alex Lyman
When compiling your project, you need to make just a couple changes to your makefile.
编译项目时,只需对 makefile 进行几处更改。
- Instead of
sdl-config --libs
, usesdl-config --static-libs
- Surround the use of the above-mentioned
sdl-config --static-libs
with-Wl,-Bstatic
and-Wl,-Bdynamic
. This tells GCC to force static linking, but only for the libraries specified between them.
- 而不是
sdl-config --libs
,使用sdl-config --static-libs
- 围绕使用上述的
sdl-config --static-libs
带-Wl,-Bstatic
和-Wl,-Bdynamic
。这告诉 GCC 强制静态链接,但仅限于它们之间指定的库。
If your makefile currently looks like:
如果您的 makefile 当前看起来像:
SDLLIBS=`sdl-config --libs`
Change it to:
将其更改为:
SDLLIBS=-Wl,-Bstatic `sdl-config --static-libs` -Wl,-Bdynamic
These are actually the same things you shoulddo on Unix-like systems, but it usually doesn't cause as many errors on Unix-likes if you use the simpler -static
flag to GCC, like it does on Windows.
这些实际上是你应该在类 Unix 系统上做的同样的事情,但是如果你使用简单的-static
GCC 标志,它通常不会像在 Windows 上那样在类 Unix 系统上引起那么多错误。
回答by David Locke
Via this SDL mailing list postit seems that the sdl development tools ship with a sdl-config script that you can use with the --static-libs flag to determine what linker flags you need to use.
通过此SDL 邮件列表帖子,似乎 sdl 开发工具附带了一个 sdl-config 脚本,您可以将其与 --static-libs 标志一起使用,以确定您需要使用哪些链接器标志。
回答by Anónimo
Environment: VMWare Virtual Machine with Windows 7 x64 and Equipment we Dev c + + build 7.4.2.569, complilador g+ + (tdm-1) 4.6.1
环境:VMWare Virtual Machine with Windows 7 x64 and Equipment we Dev c++ build 7.4.2.569, complilador g++ (tdm-1) 4.6.1
Once, SDL2-2.0.3 API installed as configuration Dev c ++ is not very clear what I've done as tradition requires command line.
曾经,作为配置 Dev c ++ 安装的 SDL2-2.0.3 API 不是很清楚我做了什么,因为传统需要命令行。
The first problem is that Windows 7 appears to have changed the methodology and they go to his ball. Inventory. Ref. https://stackoverflow.com/users/464581/cheers-and-hth-alf
第一个问题是 Windows 7 似乎已经改变了方法,他们去他的球。存货。参考 https://stackoverflow.com/users/464581/cheers-and-hth-alf
After the first hurdle, SDL_platform.h is that bad, it's down another, I do not remember where I downloaded, but the next does not work in the indicated version.
在第一关之后,SDL_platform.h 就那么糟糕,它又降了下来,我不记得我在哪里下载的,但下一个在指示的版本中不起作用。
We must put SDL2.h ls in the directory of the executable.
我们必须将 SDL2.h ls 放在可执行文件的目录中。
D:\prg_desa\zsdl2>g++ bar.cpp main.cpp -o pepe1 -ID:\SDL2-2.0.3\i686-w64-mingw32\include\SDL2 -LD:\SDL2-2.0.3\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 -mwindow
D:\prg_desa\zsdl2>g++ bar.cpp main.cpp -o pepe1 -ID:\SDL2-2.0.3\i686-w64-mingw32\include\SDL2 -LD:\SDL2-2.0.3\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 -mwindow
I've finally compiled and works SDL2 testing.
我终于编译并进行了 SDL2 测试。
回答by Nils Pipenbrinck
That's because the SDL libs are under the LGPL-license.
那是因为 SDL 库在 LGPL 许可下。
If you want to static link the libs (you can do that if your recompile them. It needs some hacking into the makefiles though) you have to place your game under some compatible open source license as well.
如果你想静态链接这些库(如果你重新编译它们,你可以这样做。虽然它需要对 makefile 进行一些黑客攻击),你还必须将你的游戏置于一些兼容的开源许可下。
The SDL-libs come as shared libraries because most programs that use them are closed source. The binary distribution comes in a form that most people need.
SDL-libs 作为共享库出现,因为大多数使用它们的程序都是闭源的。二进制分发以大多数人需要的形式出现。
回答by singpolyma
On my system (Ubuntu) I have to use the following flags:
在我的系统 (Ubuntu) 上,我必须使用以下标志:
-Wl,Bstatic -lSDL_image `sdl-config --libs` -lpng12 -lz -ltiff -ljpeg -lasound -laudio -lesd -Wl,-Bdynamic `directfb-config --libs` -lpulse-simple -lcaca -laa -ldl
That links SDL, SDL_
image, and many of their dependencies as static. libdl you never want static, so making a fully-static binary that uses SDL_
image is a poor idea. pulse,caca,aa, and directfb can probably be made static. I haven't got far enough to figure them out yet.
这将 SDL、SDL_
图像和它们的许多依赖项链接为静态。libdl 你永远不需要静态的,所以制作一个使用 SDL_
图像的完全静态的二进制文件是一个糟糕的主意。脉冲、caca、aa 和 directfb 可能可以设为静态。我还没有搞清楚它们。