sam*_*moz 237 makefile os-agnostic os-detection
我经常在几台不同的计算机和几种不同的操作系统上工作,这些操作系统是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
Run Code Online (Sandbox Code Playgroud)
Tre*_*son 266
这里有很多好的答案,但我想分享一个更完整的例子:
uname在Windows上存在这里定义的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
Run Code Online (Sandbox Code Playgroud)
dbr*_*708 117
没有参数的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
Run Code Online (Sandbox Code Playgroud)
oli*_*bre 35
OSuname命令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
Run Code Online (Sandbox Code Playgroud)
或者更安全的方式,如果不是在Windows上并且uname不可用:
ifeq ($(OS),Windows_NT)
detected_OS := Windows
else
detected_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
endif
Run Code Online (Sandbox Code Playgroud)
如果你想区分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
Run Code Online (Sandbox Code Playgroud)
然后您可以根据以下内容选择相关内容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
Run Code Online (Sandbox Code Playgroud)
命令uname与uname -s因为option -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的存在,参见使用Visual C++的示例).
下面我提供一个使用make和gcc构建共享库的完整示例:*.so或者*.dll取决于平台.这个例子尽可能简单易懂.
要在Windows上安装make,gcc请参阅Cygwin或MinGW.
??? lib
? ??? Makefile
? ??? hello.h
? ??? hello.c
??? app
??? Makefile
??? main.c
Run Code Online (Sandbox Code Playgroud)
提醒: Makefile使用制表缩进.在示例文件下面复制粘贴时的注意事项.
Makefile文件lib/Makefileifeq ($(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)
Run Code Online (Sandbox Code Playgroud)
app/Makefileifeq ($(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"
Run Code Online (Sandbox Code Playgroud)
lib/hello.h#ifndef HELLO_H_
#define HELLO_H_
const char* hello();
#endif
Run Code Online (Sandbox Code Playgroud)
lib/hello.c#include "hello.h"
const char* hello()
{
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
app/main.c#include "hello.h" //hello()
#include <stdio.h> //puts()
int main()
{
const char* str = hello();
puts(str);
}
Run Code Online (Sandbox Code Playgroud)
修复Makefile(通过一个制表替换前导空格)的复制粘贴.
> sed 's/^ */\t/' -i */Makefile
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
应用程序需要知道共享库的位置.
在Windows上,一个简单的解决方案是复制应用程序所在的库:
> cp -v lib/hello.dll app
`lib/hello.dll' -> `app/hello.dll'
Run Code Online (Sandbox Code Playgroud)
在类Unix操作系统上,您可以使用LD_LIBRARY_PATH环境变量:
> export LD_LIBRARY_PATH=lib
Run Code Online (Sandbox Code Playgroud)
在Windows上运行该命令:
> app/app.exe
hello
Run Code Online (Sandbox Code Playgroud)
在类Unix操作系统上运行命令:
> app/app
hello
Run Code Online (Sandbox Code Playgroud)
phs*_*sym 18
我最近在做实验,以便回答我问自己的问题.以下是我的结论:
因为在Windows中,您无法确定该uname命令是否可用,您可以使用gcc -dumpmachine.这将显示编译器目标.
uname如果要进行某些交叉编译,使用时可能还会出现问题.
以下是可能输出的示例列表gcc -dumpmachine:
您可以在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
Run Code Online (Sandbox Code Playgroud)
它对我来说效果很好,但我不确定这是获得系统类型的可靠方法.至少它对MinGW是可靠的,这就是我所需要的,因为它不需要在Windows中使用uname命令或MSYS包.
综上所述,uname给你的系统上,你正在编译,并gcc -dumpmachine为您提供了系统为您的编译.
Jes*_*erE 16
该git的makefile文件中对如何管理没有了autoconf/automake的,但对unixy平台众多仍然工作的例子不胜枚举.
Ken*_*son 11
更新:我现在认为这个答案已经过时了.我发布了一个新的完美解决方案.
如果您的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
Run Code Online (Sandbox Code Playgroud)
我终于找到了为我解决这个问题的完美解决方案。
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
Run Code Online (Sandbox Code Playgroud)
UNAME 变量设置为 Linux、Cygwin、MSYS、Windows、FreeBSD、NetBSD(或可能是 Solaris、Darwin、OpenBSD、AIX、HP-UX)或未知。然后可以在整个 Makefile 的其余部分进行比较,以分离任何操作系统敏感的变量和命令。
关键是 Windows 使用分号来分隔 PATH 变量中的路径,而其他所有人都使用冒号。(可以在名称中创建一个带有 ';' 的 Linux 目录并将其添加到 PATH 中,这会破坏这一点,但是谁会这样做呢?)这似乎是检测本机 Windows 的风险最低的方法,因为它不需要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)
Run Code Online (Sandbox Code Playgroud)
当然,在所有情况下都需要GNU make,或者另一个支持所使用函数的make。
这是GNU的automake/autoconf旨在解决的工作.您可能想要调查它们.
或者,您可以在不同的平台上设置环境变量,并使Makefile成为条件.
我今天遇到了这个问题,我在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
Run Code Online (Sandbox Code Playgroud)
这是一个简单的解决方案,用于检查您是否在 Windows 或 posix 类(Linux/Unix/Cygwin/Mac)环境中:
ifeq ($(shell echo "check_quotes"),"check_quotes")
WINDOWS := yes
else
WINDOWS := no
endif
Run Code Online (Sandbox Code Playgroud)
它利用了类似 posix 和 Windows 环境中都存在 echo 的事实,并且在 Windows 中,shell 不会过滤引号。
| 归档时间: |
|
| 查看次数: |
146151 次 |
| 最近记录: |