Makefile that distincts between Windows and Unix-l

2019-01-12 22:59发布

I would like to have the same Makefile for building on Linux and on Windows. I use the default GNU make on Linux and the mingw32-make (also GNU make) on Windows.

I want the Makefile to detect whether it operates on Windows or Linux.


For example make clean command on Windows looks like:

clean:
    del $(DESTDIR_TARGET)

But on Linux:

clean:
    rm $(DESTDIR_TARGET)

Also I would like to use different directory separator on Windows (\) and Linux (/).


It is possible to detect Windows operating system in Makefile?

PS: I do not want to emulate Linux on Windows (cygwin etc.)

There is similiar question: OS detecting makefile, but I didn't find the answer here.

5条回答
The star\"
2楼-- · 2019-01-12 23:10

You should probably use the $(RM) variable to remove some files.

查看更多
Juvenile、少年°
3楼-- · 2019-01-12 23:20

Checking WINDIR or COMSPEC is case-sensitive. Instead, I came up with the following solution, hope that helps someone someday:

# detect if running under unix by finding 'rm' in $PATH :
ifeq ($(wildcard $(addsuffix /rm,$(subst :, ,$(PATH)))),)
WINMODE=1
else
WINMODE=0
endif

ifeq ($(WINMODE),1)
# native windows setup :
UNLINK = del $(subst /,\,$(1))
CAT = type $(subst /,\,$(1))
else
# cross-compile setup :
UNLINK = $(RM) $(1)
CAT = cat $(1)
endif
查看更多
地球回转人心会变
4楼-- · 2019-01-12 23:24

The SystemRoot trick didn't work for me on Windows XP but this did:

ifeq ($(OS),Windows_NT)
    #Windows stuff
    ...
else
    #Linux stuff
    ....
endif
查看更多
聊天终结者
5楼-- · 2019-01-12 23:25

I solved this by looking for an env variable that will only be set on windows.

ifdef OS
   RM = del /Q
   FixPath = $(subst /,\,$1)
else
   ifeq ($(shell uname), Linux)
      RM = rm -f
      FixPath = $1
   endif
endif

clean:
    $(RM) $(call FixPath,objs/*)

Because %OS% is the type of windows, it should be set on all Windows computers but not on Linux.

The blocks then setups up variables for the different programs as well as a function for converting the forward slashes into backslashes.

You to have to use $(call FixPath,path) when you call an outside command (internal commands work fine). You could also use something like:

/ := /

and then

objs$(/)*

if you like that format better.

查看更多
来,给爷笑一个
6楼-- · 2019-01-12 23:25

I would like to have the same Makefile for building on Linux and on Windows.

Maybe you will like CMake

查看更多
登录 后发表回答