我经常在几台不同的电脑和不同的操作系统上工作,比如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

当前回答

下面是一个简单的解决方案,可以检查您是否在Windows或posix-like (Linux/Unix/Cygwin/Mac)环境中:

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

它利用了这样一个事实:echo在posix类环境和Windows环境中都存在,而且在Windows中shell不过滤引号。

其他回答

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

首先是环境变量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提出了一个有趣的替代方案。他的回答是这样的:

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

注:

Command uname is same as uname -s because option -s (--kernel-name) is the default. See why uname -s is better than uname -o. The use of OS (instead of uname) simplifies the identification algorithm. You can still use solely uname, but you have to deal with if/else blocks to check all MinGW, Cygwin, etc. variations. The environment variable OS is always set to "Windows_NT" on different Windows versions (see %OS% environment variable on Wikipedia). An alternative of OS is the environment variable MSVC (it checks the presence of MS Visual Studio, see example using Visual C++).


下面我提供了一个使用make和gcc构建共享库的完整示例:或者*.dll,这取决于平台。为了便于理解,这个例子尽可能简单。

在Windows上安装make和gcc请参见Cygwin或MinGW。

我的示例基于五个文件

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

提醒:Makefile使用表格缩进。注意复制粘贴下面的示例文件。

这两个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指出的自动变量文档。

源代码

h - lib -你好。

#ifndef HELLO_H_
#define HELLO_H_

const char* hello();

#endif

c - lib -你好。

#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

这就是GNU的automake/autoconf设计用来解决的问题。你可能想调查一下。

或者,您可以在不同的平台上设置环境变量,并根据它们设置Makefile条件。

下面是一个简单的解决方案,可以检查您是否在Windows或posix-like (Linux/Unix/Cygwin/Mac)环境中:

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

它利用了这样一个事实:echo在posix类环境和Windows环境中都存在,而且在Windows中shell不过滤引号。

不带参数的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

git makefile包含了许多如何在没有autoconf/automake的情况下进行管理的示例,但仍然可以在许多unix平台上工作。