首页 文章

操作系统检测makefile

提问于
浏览
217

我经常在几台不同的计算机和几种不同的操作系统上工作,这些操作系统是Mac OS X,Linux或Solaris . 对于我正在进行的项目,我从远程git存储库中提取代码 .

无论我在哪个终端,我都希望能够处理我的项目 . 到目前为止,我已经找到了通过每次切换计算机时更改makefile来绕过操作系统更改的方法 . 然而,这是乏味的,并引起一堆头痛 .

如何修改我的makefile,以便它检测我正在使用哪个操作系统并相应地修改语法?

这是makefile:

cc = gcc -g
CC = g++ -g
yacc=$(YACC)
lex=$(FLEX)

all: assembler

assembler: y.tab.o lex.yy.o
        $(CC) -o assembler y.tab.o lex.yy.o -ll -l y

assembler.o: assembler.c
        $(cc) -o assembler.o assembler.c

y.tab.o: assem.y
        $(yacc) -d assem.y
        $(CC) -c y.tab.c

lex.yy.o: assem.l
        $(lex) assem.l
        $(cc) -c lex.yy.c

clean:
        rm -f lex.yy.c y.tab.c y.tab.h assembler *.o *.tmp *.debug *.acts

12 回答

  • 2

    这里有很多好的答案,但我想分享一个更完整的例子:

    • 不假设Windows上存在 uname

    • 还检测处理器

    这里定义的CCFLAGS不一定是推荐的或理想的;它们正是我添加OS / CPU自动检测的项目恰好正在使用的项目 .

    ifeq ($(OS),Windows_NT)
        CCFLAGS += -D WIN32
        ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
            CCFLAGS += -D AMD64
        else
            ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
                CCFLAGS += -D AMD64
            endif
            ifeq ($(PROCESSOR_ARCHITECTURE),x86)
                CCFLAGS += -D IA32
            endif
        endif
    else
        UNAME_S := $(shell uname -s)
        ifeq ($(UNAME_S),Linux)
            CCFLAGS += -D LINUX
        endif
        ifeq ($(UNAME_S),Darwin)
            CCFLAGS += -D OSX
        endif
        UNAME_P := $(shell uname -p)
        ifeq ($(UNAME_P),x86_64)
            CCFLAGS += -D AMD64
        endif
        ifneq ($(filter %86,$(UNAME_P)),)
            CCFLAGS += -D IA32
        endif
        ifneq ($(filter arm%,$(UNAME_P)),)
            CCFLAGS += -D ARM
        endif
    endif
    
  • 11

    没有参数的uname命令(http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/uname.1.html)应该告诉您操作系统名称 . 我会使用它,然后根据返回值创建条件 .

    UNAME := $(shell uname)
    
    ifeq ($(UNAME), Linux)
    # do something Linux-y
    endif
    ifeq ($(UNAME), Solaris)
    # do something Solaris-y
    endif
    
  • 1

    使用两个简单的技巧检测操作系统:

    • 首先是环境变量 OS

    • 然后是 uname 命令

    ifeq ($(OS),Windows_NT)     # is Windows_NT on XP, 2000, 7, Vista, 10...
        detected_OS := Windows
    else
        detected_OS := $(shell uname)  # same as "uname -s"
    endif
    

    或者更安全的方式,如果不是在Windows和 uname 不可用:

    ifeq ($(OS),Windows_NT) 
        detected_OS := Windows
    else
        detected_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
    endif
    

    如果你想区分Cygwin / MinGW / MSYS / Windows,Ken Jackson提出了一个有趣的选择 . 见his answer看起来像这样:

    ifeq '$(findstring ;,$(PATH))' ';'
        detected_OS := Windows
    else
        detected_OS := $(shell uname 2>/dev/null || echo Unknown)
        detected_OS := $(patsubst CYGWIN%,Cygwin,$(detected_OS))
        detected_OS := $(patsubst MSYS%,MSYS,$(detected_OS))
        detected_OS := $(patsubst MINGW%,MSYS,$(detected_OS))
    endif
    

    然后你可以根据 detected_OS 选择相关的东西:

    ifeq ($(detected_OS),Windows)
        CFLAGS += -D WIN32
    endif
    ifeq ($(detected_OS),Darwin)        # Mac OS X
        CFLAGS += -D OSX
    endif
    ifeq ($(detected_OS),Linux)
        CFLAGS   +=   -D LINUX
    endif
    ifeq ($(detected_OS),GNU)           # Debian GNU Hurd
        CFLAGS   +=   -D GNU_HURD
    endif
    ifeq ($(detected_OS),GNU/kFreeBSD)  # Debian kFreeBSD
        CFLAGS   +=   -D GNU_kFreeBSD
    endif
    ifeq ($(detected_OS),FreeBSD)
        CFLAGS   +=   -D FreeBSD
    endif
    ifeq ($(detected_OS),NetBSD)
        CFLAGS   +=   -D NetBSD
    endif
    ifeq ($(detected_OS),DragonFly)
        CFLAGS   +=   -D DragonFly
    endif
    ifeq ($(detected_OS),Haiku)
        CFLAGS   +=   -D Haiku
    endif
    

    注意:

    • 命令unameuname -s 相同,因为选项 -s--kernel-name )是默认值 . 见why uname -s is better than uname -o .

    • 使用 OS (而不是 uname )简化了识别算法 . 您仍然可以单独使用 uname ,但是您必须处理 if/else 块以检查所有MinGW,Cygwin等变体 .

    • 在不同的Windows版本上,环境变量 OS 始终设置为 "Windows_NT" (请参阅%OS% environment variable on Wikipedia) .

    • OS 的替代方法是环境变量 MSVC (它检查是否存在MS Visual Studio,请参阅example using Visual C++) .


    下面我提供一个完整的示例,使用 makegcc 来构建共享库: *.so*.dll ,具体取决于平台 . 这个例子尽可能简单易懂 .

    要在Windows上安装 makegcc ,请参阅CygwinMinGW .

    我的例子基于五个文件

    ├── lib
     │   └── Makefile
     │   └── hello.h
     │   └── hello.c
     └── app
         └── Makefile
         └── main.c
    

    Reminder: Makefile 使用 tabulation 进行缩进 . 在示例文件下面复制粘贴时的注意事项 .

    两个Makefile文件

    1. lib / Makefile

    ifeq ($(OS),Windows_NT)
        uname_S := Windows
    else
        uname_S := $(shell uname -s)
    endif
    
    ifeq ($(uname_S), Windows)
        target = hello.dll
    endif
    ifeq ($(uname_S), Linux)
        target = libhello.so
    endif
    #ifeq ($(uname_S), .....) #See https://stackoverflow.com/a/27776822/938111
    #    target = .....
    #endif
    
    %.o: %.c
        gcc  -c $<  -fPIC  -o $@
        # -c $<  => $< is first file after ':' => Compile hello.c
        # -fPIC  => Position-Independent Code (required for shared lib)
        # -o $@  => $@ is the target => Output file (-o) is hello.o
    
    $(target): hello.o
        gcc  $^  -shared  -o $@
        # $^      => $^ expand to all prerequisites (after ':') => hello.o
        # -shared => Generate shared library
        # -o $@   => Output file (-o) is $@ (libhello.so or hello.dll)
    

    2. app / Makefile

    ifeq ($(OS),Windows_NT)
        uname_S := Windows
    else
        uname_S := $(shell uname -s)
    endif
    
    ifeq ($(uname_S), Windows)
        target = app.exe
    endif
    ifeq ($(uname_S), Linux)
        target = app
    endif
    #ifeq ($(uname_S), .....) #See https://stackoverflow.com/a/27776822/938111
    #    target = .....
    #endif
    
    %.o: %.c
        gcc  -c $< -I ../lib  -o $@
        # -c $<     => compile (-c) $< (first file after :) = main.c
        # -I ../lib => search headers (*.h) in directory ../lib
        # -o $@     => output file (-o) is $@ (target) = main.o
    
    $(target): main.o
        gcc  $^  -L../lib  -lhello  -o $@
        # $^       => $^ (all files after the :) = main.o (here only one file)
        # -L../lib => look for libraries in directory ../lib
        # -lhello  => use shared library hello (libhello.so or hello.dll)
        # -o $@    => output file (-o) is $@ (target) = "app.exe" or "app"
    

    要了解更多信息,请阅读cfi指出的Automatic Variables documentation .

    源代码

    - lib / hello.h

    #ifndef HELLO_H_
    #define HELLO_H_
    
    const char* hello();
    
    #endif
    

    - lib / hello.c

    #include "hello.h"
    
    const char* hello()
    {
        return "hello";
    }
    

    - app / main.c

    #include "hello.h" //hello()
    #include <stdio.h> //puts()
    
    int main()
    {
        const char* str = hello();
        puts(str);
    }
    

    构建

    修复 Makefile 的复制粘贴(用一个制表替换前导空格) .

    > sed  's/^  */\t/'  -i  */Makefile
    

    make 命令在两个平台上都是相同的 . 给定的输出是在类Unix操作系统上:

    > make -C lib
    make: Entering directory '/tmp/lib'
    gcc  -c hello.c  -fPIC  -o hello.o
    # -c hello.c  => hello.c is first file after ':' => Compile hello.c
    # -fPIC       => Position-Independent Code (required for shared lib)
    # -o hello.o  => hello.o is the target => Output file (-o) is hello.o
    gcc  hello.o  -shared  -o libhello.so
    # hello.o        => hello.o is the first after ':' => Link hello.o
    # -shared        => Generate shared library
    # -o libhello.so => Output file (-o) is libhello.so (libhello.so or hello.dll)
    make: Leaving directory '/tmp/lib'
    
    > make -C app
    make: Entering directory '/tmp/app'
    gcc  -c main.c -I ../lib  -o main.o
    # -c main.c => compile (-c) main.c (first file after :) = main.cpp
    # -I ../lib => search headers (*.h) in directory ../lib
    # -o main.o => output file (-o) is main.o (target) = main.o
    gcc  main.o  -L../lib  -lhello  -o app
    # main.o   => main.o (all files after the :) = main.o (here only one file)
    # -L../lib => look for libraries in directory ../lib
    # -lhello  => use shared library hello (libhello.so or hello.dll)
    # -o app   => output file (-o) is app.exe (target) = "app.exe" or "app"
    make: Leaving directory '/tmp/app'
    

    跑步

    应用程序需要知道共享库的位置 .

    在Windows上,一个简单的解决方案是复制应用程序所在的库:

    > cp -v lib/hello.dll app
    `lib/hello.dll' -> `app/hello.dll'
    

    在类Unix操作系统上,您可以使用 LD_LIBRARY_PATH 环境变量:

    > export LD_LIBRARY_PATH=lib
    

    在Windows上运行该命令:

    > app/app.exe
    hello
    

    在类Unix操作系统上运行命令:

    > app/app
    hello
    
  • 6

    我最近在做实验,以便回答我问自己的问题 . 以下是我的结论:

    因为在Windows中,您无法确定 uname 命令是否可用,您可以使用 gcc -dumpmachine . 这将显示编译器目标 .

    如果要进行交叉编译,使用 uname 时可能还有问题 .

    以下是 gcc -dumpmachine 可能输出的示例列表:

    • mingw32

    • i686-pc-cygwin

    • x86_64-redhat-linux

    您可以在makefile中检查结果,如下所示:

    SYS := $(shell gcc -dumpmachine)
    ifneq (, $(findstring linux, $(SYS)))
     # Do Linux things
    else ifneq(, $(findstring mingw, $(SYS)))
     # Do MinGW things
    else ifneq(, $(findstring cygwin, $(SYS)))
     # Do Cygwin things
    else
     # Do things for others
    endif
    

    它对我来说效果很好,但我是获得系统类型的可靠方法 . 至少它是关于 MinGW 的可靠性,这就是我所需要的,因为它不需要在Windows中使用 uname 命令或 MSYS 包 .

    总而言之, uname 为您提供了正在编译的系统 ongcc -dumpmachine 为您提供您正在编译的系统 for .

  • 15

    git makefile包含许多如何在没有autoconf / automake的情况下进行管理的示例,但仍然可以在多个unixy平台上运行 .

  • 241

    更新:我现在认为这个答案已经过时了 . 我发布了一个新的完美解决方案 .

    如果您的makefile可能在非Cygwin Windows上运行,则 uname 可能不可用 . 这很尴尬,但这是一个潜在的解决方案 . 您必须先检查Cygwin以排除它,因为它的 PATH 环境变量中也有WINDOWS .

    ifneq (,$(findstring /cygdrive/,$(PATH)))
        UNAME := Cygwin
    else
    ifneq (,$(findstring WINDOWS,$(PATH)))
        UNAME := Windows
    else
        UNAME := $(shell uname -s)
    endif
    endif
    
  • 7

    's the job that GNU' s automake / autoconf旨在解决 . 您可能想要调查它们 .

    或者,您可以在不同的平台上设置环境变量,并使Makefile成为条件 .

  • 18

    我今天遇到了这个问题,我在Solaris上需要它,所以这是一个POSIX标准的方法(非常接近) .

    #Detect OS
    UNAME = `uname`
    
    # Build based on OS name
    DetectOS:
        -@make $(UNAME)
    
    
    # OS is Linux, use GCC
    Linux: program.c
        @SHELL_VARIABLE="-D_LINUX_STUFF_HERE_"
        rm -f program
        gcc $(SHELL_VARIABLE) -o program program.c
    
    # OS is Solaris, use c99
    SunOS: program.c
        @SHELL_VARIABLE="-D_SOLARIS_STUFF_HERE_"
        rm -f program
        c99 $(SHELL_VARIABLE) -o program program.c
    
  • 3

    请注意,Makefile对间距非常敏感 . 这是一个Makefile的例子,它在OS X上运行一个额外的命令,可以在OS X和Linux上运行 . 总的来说,autoconf / automake是一种非常重要的方式 .

    UNAME := $(shell uname -s)
    CPP = g++
    CPPFLAGS = -pthread -ansi -Wall -Werror -pedantic -O0 -g3 -I /nexopia/include
    LDFLAGS = -pthread -L/nexopia/lib -lboost_system
    
    HEADERS = data_structures.h http_client.h load.h lock.h search.h server.h thread.h utility.h
    OBJECTS = http_client.o load.o lock.o search.o server.o thread.o utility.o vor.o
    
    all: vor
    
    clean:
        rm -f $(OBJECTS) vor
    
    vor: $(OBJECTS)
        $(CPP) $(LDFLAGS) -o vor $(OBJECTS)
    ifeq ($(UNAME),Darwin)
        # Set the Boost library location
        install_name_tool -change libboost_system.dylib /nexopia/lib/libboost_system.dylib vor
    endif
    
    %.o: %.cpp $(HEADERS) Makefile
        $(CPP) $(CPPFLAGS) -c $
    
  • 2

    另一种方法是使用“configure”脚本 . 如果你已经在makefile中使用了一个,你可以使用uname和sed的组合来解决问题 . 首先,在您的脚本中,执行:

    UNAME=uname
    

    然后,为了把它放在你的Makefile中,从Makefile.in开始,它应该有类似的东西

    UNAME=@@UNAME@@
    

    在里面 .

    UNAME=uname 位之后的configure脚本中使用以下sed命令 .

    sed -e "s|@@UNAME@@|$UNAME|" < Makefile.in > Makefile
    

    现在你的makefile应该根据需要定义 UNAME . 如果/ elif / else语句都是剩下的!

  • 114

    这是一个简单的解决方案,可以检查您是在Windows还是类似Posix(Linux / Unix / Cygwin / Mac)的环境中:

    ifeq ($(shell echo "check_quotes"),"check_quotes")
       WINDOWS := yes
    else
       WINDOWS := no
    endif
    

    它利用了echo类似于posix和Windows环境的事实,并且在Windows中shell不会过滤引号 .

  • 30

    我终于找到了解决这个问题的完美解决方案 .

    ifeq '$(findstring ;,$(PATH))' ';'
        UNAME := Windows
    else
        UNAME := $(shell uname 2>/dev/null || echo Unknown)
        UNAME := $(patsubst CYGWIN%,Cygwin,$(UNAME))
        UNAME := $(patsubst MSYS%,MSYS,$(UNAME))
        UNAME := $(patsubst MINGW%,MSYS,$(UNAME))
    endif
    

    UNAME变量设置为Linux,Cygwin,MSYS,Windows,FreeBSD,NetBSD(或者可能是Solaris,Darwin,OpenBSD,AIX,HP-UX)或Unknown . 然后可以在Makefile的其余部分中对其进行比较,以分离任何OS敏感变量和命令 .

    关键是Windows使用分号分隔PATH变量中的路径,而其他所有人都使用冒号 . (它's possible to make a Linux directory with a '; ' in the name and add it to PATH, which would break this, but who would do such a thing?) This seems to be the least risky method to detect native Windows because it doesn'需要一个shell调用.Cygwin和MSYS PATH使用冒号,因此为它们调用uname .

    请注意,OS环境变量可用于检测Windows,但不能区分Cygwin和本机Windows . 测试引号的回显有效,但需要shell调用 .

    不幸的是,Cygwin在uname的输出中添加了一些版本信息,因此我添加了'patsubst'次调用以将其更改为'Cygwin' . 此外,MSYS的uname实际上有三个可能的输出,从MSYS或MINGW开始,但我也使用patsubst将所有输出转换为'MSYS' .

    如果在路径上区分具有和不包含某些uname.exe的本机Windows系统很重要,则可以使用此行代替简单分配:

    UNAME := $(shell uname 2>NUL || echo Windows)
    

    当然,在所有情况下都需要GNU make,或者支持所用函数的其他make .

相关问题