[英]Cross compile SDL2 in C for Windows on Linux
我有一個我一直在 Ubuntu 上開發的 SDL2 程序,我想為 Windows 編譯它。 現在我用這樣的makefile編譯並運行它:
OBJECTS = main.o text.o object.o vector.o physics.o shapes.o target.o
run: all
./a.out
all: $(OBJECTS)
gcc $(OBJECTS) `sdl2-config --cflags --libs` -lSDL2_ttf -lSDL2_image -lm -ldl
main.o: main.c defs.h
gcc -c main.c `sdl2-config --cflags --libds` -lSDL2_ttf
text.o: text.c text.h defs.h
gcc -c text.c
object.o: object.c object.h defs.h
gcc -c object.c
vector.o: vector.c vector.h defs.h
gcc -c vector.c
physics.o: physics.c physics.h defs.h
gcc -c physics.c
shapes.o: shapes.c shapes.h defs.h
gcc -c shapes.c
target.o: target.c target.h defs.h
gcc -c target.c
clean:
rm *.o
我建議使用 quasi-msys2交叉編譯環境(我是作者)。
乍一看,您的 makefile 應該可以在其中進行最小的更改:
sdl2-config
在那里不起作用(這是一個小缺陷),但pkg-config
可以。
將sdl2-config
替換為pkg-config sdl2 SDL2_image SDL2_ttf
(后跟--libs
和/或--cflags
)。
然后-lSDL2_ttf -lSDL2_image
應該被刪除,因為 pkg-config 無論如何都會輸出這些。
請注意,鏈接時應使用pkg-config ... --libs
,編譯時應使用pkg-config --cflags
(與sdl2-config
相同)。
-ldl
應該被刪除。 -lm
是不必要的,但應該是無害的。
gcc
應替換為$CC
以從環境變量中獲取編譯器。
雖然這不會影響正確性,但在 makefile 中手動指定.h
依賴項是容易出錯且乏味的。 相反,您應該使用-MMD -MP
標志進行編譯,以自動生成依賴項。
由於刪除依賴項會使您的所有.o
配方看起來非常相似,因此您可以將它們重復數據刪除到單個配方中:
%.o : %.c $CC -c $< -MMD -MP
-MMD -MP
將生成.d
文件(微小的 makefile 片段),需要使用-include $(OBJECTS:.o=.d)
包含這些文件。
通過這些修復,makefile 應該可以在 quasi-msys2 中工作,除了生成的可執行文件將被稱為a.exe
而不是a.out
...
# We're going to cross-compile with Clang. Install Clang, LLD, Wine. Then...
# Clone the repo
https://github.com/HolyBlackCat/quasi-msys2
cd quasi-msys2
# Install the necessary packages
make install _gcc _SDL2 _SDL2_image _SDL2_ttf
# Open the cross-compilation shell
# Among other things, this sets `CC` to a wrapper for Clang that invokes it
# with the right flags, and sets env variables for `pkg-config` to find the
# newly installed libs.
env/shell.sh
然后像往常一樣運行make
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.