Linux 区分 Windows 和类 Unix 系统的 Makefile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4058840/
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
Makefile that distincts between Windows and Unix-like systems
提问by Tom Pa?ourek
I would like to have the same Makefile for building on Linux and on Windows. I use the default GNU makeon Linux and the mingw32-make(also GNU make) on Windows.
我想要在 Linux 和 Windows 上构建相同的 Makefile。我在 Linux 上使用默认的GNU make,在 Windows 上使用 mingw32-make(也是GNU make)。
I want the Makefile to detect whether it operates on Windows or Linux.
我希望 Makefile 检测它是在 Windows 还是 Linux 上运行。
For example make clean
command on Windows looks like:
例如make clean
Windows 上的命令如下所示:
clean:
del $(DESTDIR_TARGET)
But on Linux:
但在 Linux 上:
clean:
rm $(DESTDIR_TARGET)
Also I would like to use different directory separator on Windows (\
) and Linux (/
).
另外我想在 Windows ( \
) 和 Linux ( /
)上使用不同的目录分隔符。
It is possible to detect Windows operating system in Makefile?
可以在 Makefile 中检测 Windows 操作系统吗?
PS: I do not want to emulate Linux on Windows (cygwin etc.)
PS:我不想在 Windows 上模拟 Linux(cygwin 等)
There is similiar question: OS detecting makefile, but I didn't find the answer here.
有类似的问题:OS检测 makefile,但我没有在这里找到答案。
采纳答案by Paul Hutchinson
I solved this by looking for an env variable that will only be set on windows.
我通过寻找一个只能在 Windows 上设置的 env 变量来解决这个问题。
ifdef OS
RM = del /Q
FixPath = $(subst /,\,)
else
ifeq ($(shell uname), Linux)
RM = rm -f
FixPath =
endif
endif
clean:
$(RM) $(call FixPath,objs/*)
Because %OS% is the type of windows, it should be set on all Windows computers but not on Linux.
因为 %OS% 是 windows 的类型,它应该在所有 Windows 计算机上设置,而不是在 Linux 上。
The blocks then setups up variables for the different programs as well as a function for converting the forward slashes into backslashes.
然后这些块为不同的程序设置变量以及将正斜杠转换为反斜杠的函数。
You to have to use $(call FixPath,path) when you call an outside command (internal commands work fine). You could also use something like:
当您调用外部命令(内部命令工作正常)时,您必须使用 $(call FixPath,path)。你也可以使用类似的东西:
/ := /
and then
进而
objs$(/)*
if you like that format better.
如果你更喜欢那种格式。
回答by Antoine Pelisse
You should probably use the $(RM) variable to remove some files.
您可能应该使用 $(RM) 变量来删除一些文件。
回答by Antoine Pelisse
回答by tomsgd
The SystemRoot trick didn't work for me on Windows XP but this did:
SystemRoot 技巧在 Windows XP 上对我不起作用,但这样做了:
ifeq ($(OS),Windows_NT)
#Windows stuff
...
else
#Linux stuff
....
endif
回答by sezero
Checking WINDIR or COMSPEC is case-sensitive. Instead, I came up with the following solution, hope that helps someone someday:
检查 WINDIR 或 COMSPEC 区分大小写。相反,我想出了以下解决方案,希望有一天能对某人有所帮助:
# detect if running under unix by finding 'rm' in $PATH :
ifeq ($(wildcard $(addsuffix /rm,$(subst :, ,$(PATH)))),)
WINMODE=1
else
WINMODE=0
endif
ifeq ($(WINMODE),1)
# native windows setup :
UNLINK = del $(subst /,\,$(1))
CAT = type $(subst /,\,$(1))
else
# cross-compile setup :
UNLINK = $(RM) $(1)
CAT = cat $(1)
endif