Search code examples
cgccmakefile

Make recompiles non-updated file using Windows


I am trying to use make command on Windows (installed via choco). Trying to run simple example with hello.c that contains just print of "Hello, world". If I run just make hello I will face the error

cc     hello.c   -o hello
process_begin: CreateProcess(NULL, cc hello.c -o hello, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [<builtin>: hello] Error 2

So I run make hello CC=gcc and I get gcc hello.c -o hello. And the only one problem here is that I get it every time I use it. It seams that make recompiles it every time and I don't know why. I tried to create Makefile with .PHONY: all and there was no result.

UPD: Makefile

.PHONY: all
CC=gcc

Solution

  • You are on Windows. Windows is a very different environment than the POSIX-based systems make was designed for.

    In particular, on Windows binary programs have a .exe extension, so when the compiler/linker creates a binary it's not named foo, it's named foo.exe. So when you ask make to build a program hello and make invokes a command cc -o hello hello.o, the compiler toolchain probably doesn't create a file named hello it probably creates a file named hello.exe. So when you ask make again to build hello make checks and sees that hello does not exist, and so it tries again, and again the linker creates hello.exe instead. And so on.

    It sounds like the "simple example" you are following is designed to work on a POSIX system, not Windows. So as you follow that example you'll have to remember to translate everything in it to Windows.

    Or, use WSL or similar to work your examples.