在Windows和类Unix系统之间有区别的Makefile [英] Makefile that distincts between Windows and Unix-like systems
问题描述
我希望在Linux和Windows上具有相同的Makefile.我在Linux上使用默认的 GNU make ,在Windows上使用 mingw32-make (也是 GNU make ).
I would like to have the same Makefile for building on Linux and on Windows. I use the default GNU make on Linux and the mingw32-make (also GNU make) on Windows.
我希望Makefile能够检测它是否在Windows或Linux上运行.
I want the Makefile to detect whether it operates on Windows or Linux.
例如Windows上的make clean
命令如下所示:
For example make clean
command on Windows looks like:
clean:
del $(DESTDIR_TARGET)
但是在Linux上:
clean:
rm $(DESTDIR_TARGET)
我还想在Windows(\
)和Linux(/
)上使用不同的目录分隔符.
Also I would like to use different directory separator on Windows (\
) and Linux (/
).
可以在Makefile中检测Windows操作系统吗?
PS:我不想在Windows(cygwin等)上模拟Linux
有一个类似的问题:操作系统正在检测Makefile ,但我在这里找不到答案.
There is similiar question: OS detecting makefile, but I didn't find the answer here.
推荐答案
我通过查找仅在Windows上设置的env变量来解决了这个问题.
I solved this by looking for an env variable that will only be set on windows.
ifdef OS
RM = del /Q
FixPath = $(subst /,\,$1)
else
ifeq ($(shell uname), Linux)
RM = rm -f
FixPath = $1
endif
endif
clean:
$(RM) $(call FixPath,objs/*)
由于%OS%是Windows的类型,因此应在所有Windows计算机上设置,但不能在Linux上设置.
Because %OS% is the type of windows, it should be set on all Windows computers but not on Linux.
然后,这些块为不同的程序设置变量,以及将正斜杠转换为反斜杠的功能.
The blocks then setups up variables for the different programs as well as a function for converting the forward slashes into backslashes.
调用外部命令时必须使用$(调用FixPath,path)(内部命令可以正常使用).您还可以使用类似的内容:
You to have to use $(call FixPath,path) when you call an outside command (internal commands work fine). You could also use something like:
/ := /
然后
objs$(/)*
如果您更喜欢这种格式.
if you like that format better.
这篇关于在Windows和类Unix系统之间有区别的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!