I have a command that works one way in OSX/Unix and another in Debian/Linux. I want to create a make file for my application but need to detect the OS and issue the command accordingly. How would I go about doing this?
Asked
Active
Viewed 2.2k times
19
-
possible duplicate of [OS detecting makefile](http://stackoverflow.com/questions/714100/os-detecting-makefile) – Anko - inactive in protest Dec 19 '13 at 01:00
4 Answers
32
You could use uname to do this. In your Makefile, you could write something like:
OS := $(shell uname)
ifeq $(OS) Darwin
# Run MacOS commands
else
# check for Linux and run other commands
endif

flexlingie
- 352
- 2
- 3
19
What worked for me
OS := $(shell uname)
ifeq ($(OS),Darwin)
# Run MacOS commands
else
# check for Linux and run other commands
endif

Noémien Kocher
- 1,324
- 15
- 17
1
Use uname and an if statement as you do in shell commands, as suggested here.
.PHONY: foo
OS := $(shell uname)
foo:
@if [ OS = "Darwin" ]; then\
echo "Hello world";\
fi
@if [ OS = "Linux" ]; then\
echo "Hello world";\
fi
Note that the closing; and \ at each line are necessary
(This is because make interpret each line as a separate command unless it ends with )

Gabriel Braico Dornas
- 439
- 3
- 11
-1
Use autotools. It's a standard way of building portable source code packages.

bonsaiviking
- 5,825
- 1
- 20
- 35