英文:
I'm trying to port a make file that was used in linux to be able to work in a windows, confused about some errors
问题
在进行编译时,我执行了以下操作:
@copy $(OUTPUT_HEX) $(BUILD_DIR)$(OUTPUT_NAME)_$(VARIANT_MODE)_new.hex
由于这不起作用,我尝试了来自相同目录的以下命令:
copy /Y build\debug21_0822_CON_debug.hex build\debug\hello_new.hex
这个命令在命令提示符中可以正常工作,没有问题。但是当我在makefile中尝试完全相同的命令时,出现了一个错误,说系统找不到特定的路径。
所以我的猜想是,由于某种未知原因,编译正在从另一个路径创建,因此在运行命令之前,我执行了以下操作:
@cd C:/Users/john/git/0821_0822/01_Control/app
@copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex
这个命令和我在命令提示符中使用的复制命令是在相同的目录中执行的,而在命令提示符中可以正常工作,我不明白为什么它在makefile中不起作用。
我尝试了一些以下命令:
@echo $(CURDIR)
在执行cd之前和之后,目录似乎是正确的,但实际上cd似乎不起作用,因为如果我将它更改为另一个目录,目录仍然相同。
我对此感到非常困惑。顺便说一下,我是在ST32CubeIDE中进行此操作的,没有其他可选方法。谢谢!
英文:
At some point in the make I'm doing this:
@copy $(OUTPUT_HEX) $(BUILD_DIR)$(OUTPUT_NAME)_$(VARIANT_MODE)_new.hex
As this wasn't working I tried the following command from the same directory:
copy /Y build\debug21_0822_CON_debug.hex build\debug\hello_new.hex
This command is working on cmd, no problem. When I try the same exactly command in the makefile I've got an error saying that the system cannot find the specific route.
So my guess was that for an unknown reason the build is being created from another route, so before running the command I do this:
@cd C:/Users/john/git/0821_0822/01_Control/app
@copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex
Same directory I'm using the copy command in cmd, and it's working there, I don't understand why it's not working.
I've tried some
@echo $(CURDIR)
Before & after doing the cd, and the directory seems correct, but cd actually doesn't seem to be working, as the diretory is still the same if I change it to another.
I'm really confused about this.
By the way, I'm doing this in ST32CubeIDE, no alternative from what I have to do.
Thanks!
答案1
得分: 1
为了开始,pmacfarlane是100%正确的。在你的 makefile 百分之百正确之前,使用 @
是个坏主意。你不会尝试通过首先将程序的所有输出重定向到 /dev/null
来调试你的程序:这是等同的。
第一个问题是这不会做你认为它会做的事情:
@cd C:/Users/john/git/0821_0822/01_Control/app
@copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex
在 makefile 配方中的每个逻辑行都在一个 不同的 shell 中运行,这意味着对本地 shell 环境的任何更改(比如设置环境变量或更改工作目录)都会在命令退出时丢失。因此,这里的第一个命令是一个无操作:它启动一个 shell,更改目录,退出 shell,随后目录更改被撤销。
其次,在 Windows 中编写 makefile 非常棘手,因为有很多不同的可能做事的方式。Make 调用一个 shell,但它调用哪个 shell?它是调用 cmd.exe
吗?它是调用 Powershell 吗?它是调用你安装的 POSIX shell 吗(比如作为 Git for Windows 的一部分)?
这很重要,因为与 UNIX 系统不同,在 Windows shell 中运行的许多程序都是内置到 shell 中的;这包括 copy
例如。这意味着如果你启动一个不同的 shell,你不能将它们作为独立程序运行。
所以看起来你的版本的 make
在你的 PATH 上找到了 UNIX shell sh.exe
,所以它正在使用它作为调用命令的 shell,当然这不会允许 copy
工作。
最简单的解决方法是明确设置你想要运行的 shell。例如,你可以在你的 makefile 中添加这个:
SHELL := cmd.exe
英文:
To start, pmacfarlane is 100% right. Using @
, certainly before your makefile is working 100% correctly, is a bad idea. You wouldn't try to debug your program by first redirecting all its output to /dev/null
: this is the equivalent.
The first problem is that this doesn't do what you think it does:
@cd C:/Users/john/git/0821_0822/01_Control/app
@copy /Y build/debug/0821_0822_CON_debug.hex build/debug/hola_app_only.hex
Every logical line in a makefile recipe is run in a different shell which means any changes to the local shell environment (such as setting environment variables, or changing the working directory) are lost whenever the command exits. So the first command here is a no-op: it starts a shell, changes the directory, exits the shell whereupon the directory change is gone.
Second, writing makefiles in Windows is very tricky because there are so many different possible ways to do things. Make invokes a shell, but what shell does it invoke? Is it invoking cmd.exe
? Is it invoking Powershell? Is it invoking a POSIX shell you installed (say as part of Git for Windows)?
That matters because unlike on UNIX systems, many of the programs you run in a Windows shell are built-in to the shell; that includes copy
for example. That means that you can't run them as stand-alone programs if you start a different shell.
So it looks to me like your version of make
is finding UNIX shell sh.exe
on your PATH, and so it's using that as the shell to invoke commands, and of course that won't allow copy
to work.
The simplest way around this is to explicitly set the shell you want to run. For example you can add this to your makefile:
SHELL := cmd.exe
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论