操作系统检测makefile [英] OS detecting makefile
问题描述
我通常在多台不同的计算机和几种不同的操作系统上工作,这些计算机是Mac OS X,Linux或Solaris.对于我正在从事的项目,我从远程git存储库中提取代码.
I routinely work on several different computers and several different operating systems, which are Mac OS X, Linux, or Solaris. For the project I'm working on, I pull my code from a remote git repository.
无论我在哪个终端上,我都希望能够从事我的项目.到目前为止,我已经找到了通过在每次切换计算机时更改makefile来解决操作系统更改的方法.但是,这很繁琐,并且会引起很多头痛.
I like to be able to work on my projects regardless of which terminal I'm at. So far, I've found ways to get around the OS changes by changing the makefile every time I switch computers. However, this is tedious and causes a bunch of headaches.
如何修改我的makefile,以便它检测到我正在使用的操作系统并相应地修改语法?
How can I modify my makefile so that it detects which OS I'm using and modifies syntax accordingly?
这是makefile:
Here is the 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
推荐答案
这里已经有很多不错的答案,但是我想分享一个更完整的示例,两者:
There are many good answers here already, but I wanted to share a more complete example that both:
- 不假定
uname
在Windows上存在 - 还检测处理器
- doesn't assume
uname
exists on Windows - also detects the processor
这里定义的CCFLAGS不一定是推荐的或理想的;这正是我要添加OS/CPU自动检测的项目所使用的.
The CCFLAGS defined here aren't necessarily recommended or ideal; they're just what the project to which I was adding OS/CPU auto-detection happened to be using.
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
这篇关于操作系统检测makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!