1

I have a problem that I just can't wrap my head around. I have a minimal example makefile that is supposed to compile a very simple .c file into an executable program.

When I run make, the compiler starts compiling and then produces an error message

"T:\printOffsets.c:10:21: error: bootIfc.h: No such file or directory"

Then I copy the exact same command line make is using to build the target and run it directly in the same Windows command shell instance, and suddenly compilation succeeds without errors!! The command line is (path names simplified):

T:\perl\c\bin\gcc.exe T:\printOffsets.c -IT:\include\ -o D:\printOffsets.exe

How do I know? Well, make prints the command line before it executes it, so I simply copy&paste from the shell.

I don't get it! How is this possible?? How can the exact same command work on the shell and fail if launched from within a Makefile??

I'm using GNU Make 3.82 on Windows 7, by the way.

antred
  • 3,697
  • 2
  • 29
  • 37
  • 1
    Could you try with slashes instead of backslashes? I believe I got sometime the same problem and using slashes was better in some cases. – jdarthenay Apr 04 '16 at 13:06
  • 1
    Also maybe Make is not using the shell you believe, may you add a target in your makefile to launch echo $(SHELL)? – jdarthenay Apr 04 '16 at 13:08
  • @jdarthenay You are exactly right!! It is using sh.exe, not the Windows command shell! And replacing all backslashes with forward slashes fixed the problem! Could you post your comment as an answer so I can accept / up-vote it? – antred Apr 04 '16 at 13:15

1 Answers1

3

When command in makefile is giving different result from shell, just make sure it is using the shell you want.

Add a phony target in your make file:

.PHONY:testshell

testshell:
    echo $(SHELL)

And run:

gmake testshell

If the result is not your favorite shell you can force it by adding a line such as this one at the beginning of your makefile:

SHELL=C:\Windows\System32\cmd.exe

If you are not sure of full path of your shell, just open a DOS console and launch:

where cmd

Edit: alternative solution

When using sh shell instead of cmd shell, you can also replaces all backslashes in commands with slashes and keep using sh.

Edit 2: change shell for a single target

Community
  • 1
  • 1
jdarthenay
  • 3,062
  • 1
  • 15
  • 20
  • Is it possible to change the shell to use just for the recipe of one particular make target, but let all the other targets continue to use the default shell? – antred Apr 04 '16 at 13:26
  • @antred No idea how one's could do that. Maybe it's possible to change value of `SHELL` but I never tried it. – jdarthenay Apr 04 '16 at 13:31
  • 1
    @antred Maybe from bash you can launch a single cmd command with `cmd /C echo toto` for instance, and the opposite should also be possible. – jdarthenay Apr 04 '16 at 19:53
  • Yes, that's what I ended up doing. Meanwhile, I found out that there's another way, too. Go here http://stackoverflow.com/questions/589276/how-can-i-use-bash-syntax-in-makefile-targets/589300 and scroll down to the accepted answer. – antred Apr 05 '16 at 08:39
  • @antred thanks for this link, it is indeed very interesting. – jdarthenay Apr 05 '16 at 10:41