You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写跨平台Makefile:在Windows/Linux中读取变量

问题

现有一段仅能在Linux环境正常运行的Makefile代码,需编写同时兼容Linux与Windows的通用版本。原代码如下:

migrations:
    @read -p "Enter migration message: " message; \
    alembic revision --autogenerate --message "$$message"

解决方案

核心思路是通过系统环境变量区分Windows和Linux,分别使用对应系统的输入读取命令实现兼容:

migrations:
ifeq ($(OS),Windows_NT)
	@set /p "message=Enter migration message: " && alembic revision --autogenerate --message "%message%"
else
	@read -p "Enter migration message: " message; alembic revision --autogenerate --message "$$message"
endif

关键说明

  • 系统判断:利用Makefile内置的$(OS)变量,Windows环境下该变量值为Windows_NT,Linux环境下则为对应发行版名称(如Linux)
  • Windows输入处理:用set /p命令读取用户输入,通过&&连接后续命令,变量引用格式为%message%
  • Linux输入处理:保留原有的read命令,变量引用需用$$message转义(避免被Makefile直接解析)
  • 缩进要求:Makefile中命令行必须使用Tab键缩进,不能用空格替代

内容的提问来源于stack exchange,提问作者Seliverstov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:12:34