区分Windows和类Unix系统的Makefile
我想要在 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等)
有类似的问题:操作系统检测makefile,但我在这里没有找到答案。
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.
I want the Makefile to detect whether it operates on Windows or Linux.
For example make clean
command on Windows looks like:
clean:
del $(DESTDIR_TARGET)
But on Linux:
clean:
rm $(DESTDIR_TARGET)
Also I would like to use different directory separator on Windows (\
) and Linux (/
).
It is possible to detect Windows operating system in Makefile?
PS: I do not want to emulate Linux on Windows (cygwin etc.)
There is similiar question: OS detecting makefile, but I didn't find the answer here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过查找仅在 Windows 上设置的环境变量解决了这个问题。
由于 %OS% 是 Windows 类型,因此应在所有 Windows 计算机上设置它,但不能在 Linux 上设置。
然后,这些块为不同的程序设置变量以及将正斜杠转换为反斜杠的函数。
当您调用外部命令(内部命令工作正常)时,您必须使用 $(call FixPath,path) 。您还可以使用类似:
然后
如果您更喜欢这种格式。
I solved this by looking for an env variable that will only be set on windows.
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.
You to have to use $(call FixPath,path) when you call an outside command (internal commands work fine). You could also use something like:
and then
if you like that format better.
SystemRoot 技巧在 Windows XP 上对我不起作用,但它起作用了:
The SystemRoot trick didn't work for me on Windows XP but this did:
您可能应该使用 $(RM) 变量来删除一些文件。
You should probably use the $(RM) variable to remove some files.
检查 WINDIR 或 COMSPEC 区分大小写。相反,我出现了
通过以下解决方案,希望有一天能帮助别人:
Checking WINDIR or COMSPEC is case-sensitive. Instead, I came up
with the following solution, hope that helps someone someday:
也许你会喜欢 CMake
Maybe you will like CMake