2つの簡単なトリックを使用してオペレーティングシステムを検出します。
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を区別したい場合は、ケンジャクソンが興味深い代替案を提案します。そのような彼の答えを見てください:
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
ノート:
- コマンド- unameは- uname -s、オプション- -s(- --kernel-name)がデフォルトであるため、同じです。がに勝る理由を- uname -s- uname -oご覧ください。
 
- の- OS代わりにを使用すると- uname、識別アルゴリズムが簡素化されます。それでも単独- unameで使用できますが、- if/elseすべてのMinGW、Cygwinなどのバリエーションをチェックするには、ブロックを処理する必要があります。
 
- 環境変数 - OSは常に- "Windows_NT"、異なるWindowsバージョンで設定されます(- %OS%Wikipediaの環境変数を参照))。
 
- の代替- OSは環境変数です- MSVC(MS Visual Studioの存在をチェックします。VisualC ++を使用した例を参照してください)。
 
以下に、私が使用する完全な例を示します makeとgcc共有ライブラリを構築するために:*.soまたは*.dllます。、プラットフォームによって異なります。この例は、理解しやすいようにできるだけ単純です。
インストールmakeしてgccWindowsに CygwinまたはMinGWを参照してください。。
私の例は5つのファイルに基づいています
 ├── lib
 │   └── Makefile
 │   └── hello.h
 │   └── hello.c
 └── app
     └── Makefile
     └── main.c
注意: Makefile表を使用してインデントします。以下のサンプルファイルをコピーして貼り付ける場合の注意。
2つの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でご覧ください。
ソースコード
- 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(先頭のスペースを1つの表に置き換えます)。
> sed  's/^  */\t/'  -i  */Makefile
makeコマンドは、両方のプラットフォームで同じです。与えられた出力はUnixライクなOS上にあります:
> 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ライクなOSでは、LD_LIBRARY_PATH環境変数を使用できます。
> export LD_LIBRARY_PATH=lib
Windowsでコマンドを実行します。
> app/app.exe
hello
UnixライクなOSでコマンドを実行します。
> app/app
hello
               
              
PROCESSOR_ARCHITECTURE、プロセスが32ビットか64ビットかによって、envvarは仮想化されているようです。したがって、make32ビットで64ビットのアプリケーションを構築しようとすると、失敗します。と組み合わせて使用するPROCESSOR_ARCHITEW6432ます(私のために働いたこれを、とすることを)