简体   繁体   English

在 linux 上使用 sdl 交叉编译 c++

[英]Cross-compiling c++ with sdl on linux

I'm on Arch Linux, and I have a C++ SDL2 program, contained in single main.cpp file, and I compile it for Linux with such command:我在 Arch Linux 上,我有一个 C++ SDL2 程序,包含在单个main.cpp文件中,我用这样的命令为 Linux 编译它:

g++ main.cpp -lSDL2 -lSDL2_image

Now I wanna compile it for windows.现在我想为 windows 编译它。 Any advice on what should I do?关于我应该怎么做的任何建议?

Consider using Cygwin .考虑使用Cygwin When I need to compile C++ code on windows I follow this instructions and they work fine for me.当我需要在 windows 上编译 C++ 代码时,我按照此说明进行操作,它们对我来说很好。 You may select other specific packages you need from the installation list.您可以从安装列表中 select 其他您需要的特定软件包。

I suggest my own tool, quasi-msys2 , which lets you reuse the precompiled SDL2 for MinGW provided by MSYS2 (and more).我建议使用我自己的工具quasi-msys2 ,它可以让您重用MSYS2 (以及更多)提供的用于 MinGW 的预编译 SDL2。

Install Clang, LLD, make , wget , tar , zstd , gpg .安装 Clang、LLD、 makewgettarzstdgpg

git clone https://github.com/HolyBlackCat/quasi-msys2
cd quasi-msys2/
make install _gcc _SDL2 _SDL2_image
env/shell.sh
win-clang++ main.cpp `pkg-config --cflags --libs sdl2 SDL2_image`

This should produce a.exe , which you can test using wine a.exe (or just ./a.exe , after running env/shell.sh ).这应该会产生a.exe ,您可以使用wine a.exe (或仅./a.exe ,在运行env/shell.sh之后)对其进行测试。


How to do this manually:如何手动执行此操作:

For completeness, SDL2 itself distributes precompiled binaries for MinGW, meaning the manual setup is not hard.为了完整起见,SDL2 本身为 MinGW 分发了预编译的二进制文件,这意味着手动设置并不难。 Any tutorial for MinGW should work. MinGW 的任何教程都应该有效。

Install MinGW from your package manager.从 package 管理器安装 MinGW。

Download and unpack SDL2-devel-??-mingw.zip and SDL2_image-devel-??-mingw.zip .下载并解压SDL2-devel-??-mingw.zipSDL2_image-devel-??-mingw.zip

Specify the paths to the directories with .a files using -L... and to .h files using -I... .使用-L...指定带有.a文件的目录的路径,使用-I...指定.h文件的路径。 Add -lmingw32 -lmingw32 -mwindows -lSDL2main -lSDL2 -lSDL2_image to the linker flags.-lmingw32 -lmingw32 -mwindows -lSDL2main -lSDL2 -lSDL2_image添加到 linker 标志。

Follow this troubleshooting guide if you get stuck.如果您遇到困难,请遵循此故障排除指南

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

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