如何在make文件中检测操作系统?

发布于 2025-01-05 21:23:22 字数 105 浏览 1 评论 0原文

我有一个命令在 OSX/Unix 中以一种方式工作,在 Debian/Linux 中以另一种方式工作。我想为我的应用程序创建一个 make 文件,但需要检测操作系统并相应地发出命令。我该怎么做呢?

I have a command that works one way in OSX/Unix and another in Debian/Linux. I want to create a make file for my application but need to detect the OS and issue the command accordingly. How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心安伴我暖 2025-01-12 21:23:22

您可以使用 uname 来执行此操作。在你的 Makefile 中,你可以这样写:

OS := $(shell uname)
ifeq $(OS) Darwin
# Run MacOS commands 
else
# check for Linux and run other commands
endif

You could use uname to do this. In your Makefile, you could write something like:

OS := $(shell uname)
ifeq $(OS) Darwin
# Run MacOS commands 
else
# check for Linux and run other commands
endif
摘星┃星的人 2025-01-12 21:23:22

什么对我有用

OS := $(shell uname)
ifeq ($(OS),Darwin)
  # Run MacOS commands
else
  # check for Linux and run other commands
endif

What worked for me

OS := $(shell uname)
ifeq ($(OS),Darwin)
  # Run MacOS commands
else
  # check for Linux and run other commands
endif
酒废 2025-01-12 21:23:22

像在 shell 命令中一样使用 uname 和 if 语句,如此处的建议。

.PHONY: foo

OS := $(shell uname)

foo:
    @if [ OS = "Darwin" ]; then\
      echo "Hello world";\
    fi
    @if [ OS = "Linux" ]; then\
      echo "Hello world";\
    fi

注意关闭;和 \ 在每一行都是必要的

(这是因为 make 将每一行解释为一个单独的命令,除非它以 结尾)

Use uname and an if statement as you do in shell commands, as suggested here.

.PHONY: foo

OS := $(shell uname)

foo:
    @if [ OS = "Darwin" ]; then\
      echo "Hello world";\
    fi
    @if [ OS = "Linux" ]; then\
      echo "Hello world";\
    fi

Note that the closing; and \ at each line are necessary

(This is because make interpret each line as a separate command unless it ends with )

金橙橙 2025-01-12 21:23:22

使用自动工具。这是构建可移植源代码包的标准方法。

Use autotools. It's a standard way of building portable source code packages.

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