我希望在 Linux 和 Windows 上构建相同的 Makefile。我在 Linux 上使用默认的 GNU make,在 Windows 上使用 mingw32-make(也是 GNU make)。
我希望 Makefile 检测它是在 Windows 还是 Linux 上运行。
例如,Windows 上的 make clean
命令如下所示:
clean:
del $(DESTDIR_TARGET)
但在 Linux 上:
clean:
rm $(DESTDIR_TARGET)
我还想在 Windows (\
) 和 Linux (/
) 上使用不同的目录分隔符。
可以在 Makefile 中检测 Windows 操作系统吗?
PS:我不想在 Windows 上模拟 Linux(cygwin 等)
有类似的问题:OS detecting makefile ,但我在这里没有找到答案。
最佳答案
我通过寻找一个只能在 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 上。
然后,这些 block 为不同的程序设置变量以及将正斜杠转换为反斜杠的函数。
调用外部命令时必须使用 $(call FixPath,path) (内部命令可以正常工作)。你也可以使用类似的东西:
/ := /
然后
objs$(/)*
如果你更喜欢这种格式。
关于linux - 区分 Windows 和类 Unix 系统的 Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058840/