区分Windows和类Unix系统的Makefile

发布于 2024-09-29 16:47:19 字数 701 浏览 1 评论 0原文

我想要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

单身狗的梦 2024-10-06 16:47:19

我通过查找仅在 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 上设置。

然后,这些块为不同的程序设置变量以及将正斜杠转换为反斜杠的函数。

当您调用外部命令(内部命令工作正常)时,您必须使用 $(call FixPath,path) 。您还可以使用类似:

/ := /

然后

objs$(/)*

如果您更喜欢这种格式。

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/*)

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

objs$(/)*

if you like that format better.

殤城〤 2024-10-06 16:47:19

SystemRoot 技巧在 Windows XP 上对我不起作用,但它起作用了:

ifeq ($(OS),Windows_NT)
    #Windows stuff
    ...
else
    #Linux stuff
    ....
endif

The SystemRoot trick didn't work for me on Windows XP but this did:

ifeq ($(OS),Windows_NT)
    #Windows stuff
    ...
else
    #Linux stuff
    ....
endif
几度春秋 2024-10-06 16:47:19

您可能应该使用 $(RM) 变量来删除一些文件。

You should probably use the $(RM) variable to remove some files.

前事休说 2024-10-06 16:47:19

检查 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

Checking WINDIR or COMSPEC is case-sensitive. Instead, I came up
with the following solution, hope that helps someone someday:

# 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
葬花如无物 2024-10-06 16:47:19

我想要在 Linux 和 Windows 上构建相同的 Makefile。

也许你会喜欢 CMake

I would like to have the same Makefile for building on Linux and on Windows.

Maybe you will like CMake

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文