I had learned Makefile before, but there is a problem in Makefile when I typed the command "make" today.
Here is the piece of code:
[ -d _build ] || mkdir _build
And the error:
'[' is not recognized as an internal or external command, operable program or batch file.
and there is my Make version:
GNU Make 3.82.90
Built for i686-pc-mingw32
Can anybody tell me how to fix it?
Your makefile was written for Linux/Unix and you are running it under Windows.
In a makefile, the commands that compose the recipes of the targets are
written in the language of the OS shell. Under Windows the shell is cmd.
Under Linux by default it is bash. Under other Unix variants it is sh or
some other shell.
[ -d _build ] || mkdir _build
is a valid command for bash and some other Linux/Unix shells. Not for
cmd. The same will be true of almost all the other recipe commands in the
makefile. You have just hit the first such problem.
Your makefile is of no use under native Windows. On Windows you have a chance
of running it in a Linux virtual machine, or under the Windows 10 Subsystem for Linux,
or under a Unix-like environment for Windows such as Cygwin or MSYS2. You may
well need to install tools that the makefile recipes require in that Linux/Unix-like
system.
If the makefile is simple you can consider translating the unrecognized shell
commands into cmd commands, if you are able.
Related
I've created a makefile for GNU make 3.80 which works fine on my main development machine running Windows. I've some experience when to use '\' instead of '/' or when a '\\' is applicable.
This time there is '\' in paths as the makefile gets generated from a VS .vxproj via a Perl script.
Strange thing is now that a 100% working makefile behaves different on another machine running exactly the same make.exe binary which is part of my repository.
A rule like this
$(OBJ_DIR)\Atomics.obj : ..\BSW\Atomics\src\Atomics.c
$(CC) $(CFLAGS) $<
on the other machine produces the error message
Cannot open source file: '..BSWAtomicssrcAtomics.c'
OK, solution is make Perl toggle the '\' into '/' when creating the makefile.
But still I wonder if there is some Windows setting which causes this problem?
In a cmd.exe in same directory the same make.exe is called just like:
D:\project\XYZ\make>..\tool\make\make.exe
Yes indeed the presence of some shell binary (msys, mingw, etc.) broke my makefile for the other host. So the only way for my makefile to stick to its MS-DOS commands is to hide the UN*X tools by PATH-settings (e.g. start sub-shell with mini environment).
Within a Makefile on linux, we can excute shell/bash commands with it to move directories or excute another files. However when porting the same Makefile over to macOS, all the commands are not readible (therefore path and execution are broken). Is there a universal command or workflow that can work on both?
Example of Makefile
.ONESHELL:
COMMAND ?= none
GIT_HASH ?= githash
alpine:
#cd images/alpine
#make ${COMMAND} GIT_HASH=${GIT_HASH} ALPINE_VERSION=3.6.5 TAG=3.6
so in a linux box both #CD and #MAKE are executed but not for macOS Catalina. I would like to make it universal so that both system will respect the appropriate command that follows.
Chances are that your MacOS box uses its default GNU make version (3.81). .ONESHELL was introduced with 3.82. Upgrade with Homebrew or MacPort. Anyway, better avoid make in recipes, prefer $(MAKE), and instead of cd; make you can use GNU make's -C option: $(MAKE) -C images/alpine ...
I'm seeing a strange issue when trying to change directory from inside a Makefile in Windows. My pseudo-code is as follows:-
all:
cd ../ProjectDir && ../AutoExc InputFile.h
Where AutoExc is an executable that I have in the parent directory. When I run 'make' from the command line, I see the following output:-
cd ../ProjectDir && ../AutoExc InputFile.h
'..' is not recognized as an internal or external command.
The strange thing is that running this exact Makefile in Linux works. I have also tried running the above command from the command line in Windows and it worked, so there isn't an issue with 'cd' for example.
Any idea why this is happening and what can I do to get it to work? If there's an issue with the version of Make, is there another more reliable mechanism to change directory and run an executable on Windows?
I am using Make version 3.81 on Windows 7 (latest) and Make version 4.1 on Linux 4.15.0-20-generic #21-Ubuntu. I have 'cd' installed as part of 'Git for Windows', the git version is 2.16.1.windows.4.
Your command contains shell characters and is passed down to cmd.exe:
>cd ../Public && ../test
'..' is not recognized as an internal or external command,
operable program or batch file.
>cd ../Public && ..\test
'..\test' is not recognized as an internal or external command,
operable program or batch file.
i.e. cmd.exe interprets ../test as command .. with option /test.
I guess one solution would to apply a macro to command names with paths, e.g. (note: untested, just typed from my head):
if (...I'm running under Windows...)
convert_path = $(subst /,\\,$(1))
else
convert_path = $(1)
endif
all:
cd ../ProjectDir && $(call convert_path,../AutoExc) InputFile.h
or, if possible, use the suggestion from the comments SHELL := /bin/sh, or whatever the correct path is to a UNIX compatible shell in your Windows environment. "Git for Windows" is AFAIR based on MinGW, so you should have bash available.
How do I pass an arbitrary command line argument to a native Windows program from a MinGW shell?
I would like a general solution, but a solution that works for any valid Windows filename would be acceptable.
That shell is Bash. Cygwin/MSYS2 Bash can accept Windows paths, but you need to
deal with spaces and backslashes. Regarding backslashes:
program 'C:\alfa.txt'
program C:\\alfa.txt
program C:/alfa.txt
Regarding spaces:
program 'C:\alfa bravo.txt'
program C:\\alfa\ bravo.txt
program C:/alfa\ bravo.txt
As you can see, if you are supplying Windows paths, this is pretty straight
forward. The only issue you might get is if you are trying to supply Bash paths
to a Windows native program:
program /tmp/alfa.txt
Windows native programs have no concept of /tmp or even /. Cygwin/MSYS2 have
cygpath to assist in converting these paths:
program $(cygpath -m /tmp/alfa.txt)
program "$(cygpath -w /tmp/alfa.txt)"
program "$(cygpath -m '/tmp/alfa bravo.txt')"
program "$(cygpath -m /tmp/alfa\ bravo.txt)"
program "$(cygpath -w '/tmp/alfa bravo.txt')"
program "$(cygpath -w /tmp/alfa\ bravo.txt)"
Side note: MinGW is an old project. You should be using Cygwin or MSYS2.
I have never worked with Linux and hence I am ignorant in the commands.However, I need to use the GNU for win64 environment for one of the programming tools to function. I have downloaded the GNU from cygwin. This package includes gcc and various commands and
shells (sh, bash, etc.) that make the PC have a unix
like environment.
I have to change to the directory of the program code MyPrograms and type "make all".
On opening the terminal of cygwin i get this line >> -bash-4.1$
Question is what is the command for changing to the directory MyPrograms;what do I type in after -bash-4.1$
If your MyPrograms folder is located at C:\MyPrograms, then in Cygwin, type:
cd /cygdrive/c/MyPrograms
Refer to this FAQ