代码之家  ›  专栏  ›  技术社区  ›  Tom Pažourek

在Windows和类Unix系统之间区分的Makefile

  •  42
  • Tom Pažourek  · 技术社区  · 14 年前

    我想有相同的Makefile用于在Linux和Windows上构建。我使用默认值 GNU品牌 在Linux和 (同时 GNU品牌

    我想让Makefile检测它是在Windows还是Linux上运行。


    例如 make clean

    clean:
        del $(DESTDIR_TARGET)
    

    但在Linux上:

    clean:
        rm $(DESTDIR_TARGET)
    

    另外,我想在Windows上使用不同的目录分隔符( \ )和Linux( / ).


    是否可以在Makefile中检测Windows操作系统?

    附言: 我不想在Windows上模拟Linux(cygwin等)

    有一个类似的问题: OS detecting makefile

    5 回复  |  直到 8 年前
        1
  •  42
  •   Paul Hutchinson    6 年前

    我通过查找只在windows上设置的env变量解决了这个问题。

    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/*)
    

    因为%OS%是windows类型,所以应该在所有windows计算机上设置,但不能在Linux上设置。

    在调用外部命令(内部命令可以正常工作)时,必须使用$(call FixPath,path)。您还可以使用类似于:

    / := /
    

    然后

    objs$(/)*
    

    如果你更喜欢那种格式。

        2
  •  41
  •   tomsgd    14 年前

    SystemRoot技巧在Windows XP上对我不起作用,但它确实起了作用:

    ifeq ($(OS),Windows_NT)
        #Windows stuff
        ...
    else
        #Linux stuff
        ....
    endif
    
        3
  •  8
  •   Antoine Pelisse    14 年前

    您可能应该使用$(RM)变量删除一些文件。

        4
  •  3
  •   Mads Elvheim Mads Elvheim    14 年前

    我想有相同的Makefile用于在Linux和Windows上构建。

    也许你会喜欢 CMake

        5
  •  1
  •   sezero    7 年前

    有了下面的解决方案,希望有一天能帮助别人:

    # 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
    
    推荐文章