LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2016, 04:14 PM   #1
mastemmer
Member
 
Registered: Jul 2007
Location: Porto Alegre, Brazil
Distribution: Slackware
Posts: 44

Rep: Reputation: 17
Detecting operating system (windos/linux) in Makefile


I am writing a Makefile (to use make) to compile a program that has to be multiplatform.
It will be used in a unix-shell or in a Windows(MSDOS) cmd.
The problem is that some commands are different in windos than in unix. I want to detect the OS and issue the right commands automatically.
If the Makefile has a way to detect whether it is in windows or in unix, I could make up for this differences using ifeq clauses.
The question is how can I detect whether it is running Windos or Linux in the Makefile?
 
Old 04-20-2016, 06:58 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Can you tell the difference in the output of this command between the systems?
Code:
zcat   /proc/config.gz
 
Old 04-20-2016, 09:04 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,773

Rep: Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097Reputation: 5097
If you're using CMake to generate the Makefile, then you can just use:

Code:
IF (APPLE)
ENDIF (APPLE)

IF (UNIX)
ENDIF (UNIX)
For just Make, Google got me these:

linosaurusroot: your answer looks to me like it was intended to be as unhelpful as possible.

Last edited by dugan; 04-20-2016 at 09:08 PM.
 
1 members found this post helpful.
Old 04-20-2016, 11:35 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
in GNU-make:
Code:
PLATFORM := $(shell uname)

ifeq  ($(PLATFORM),Linux)
...
else
...
endif
 
Old 04-21-2016, 02:12 AM   #5
mastemmer
Member
 
Registered: Jul 2007
Location: Porto Alegre, Brazil
Distribution: Slackware
Posts: 44

Original Poster
Rep: Reputation: 17
Smile

Quote:
Originally Posted by dugan View Post
If you're using CMake to generate the Makefile, then you can just use:

Code:
IF (APPLE)
ENDIF (APPLE)

IF (UNIX)
ENDIF (UNIX)
For just Make, Google got me these:

linosaurusroot: your answer looks to me like it was intended to be as unhelpful as possible.
Thank you dougan

The first link provides many useful hints.
There I found this:
Code:
ifeq ($(shell echo "check_quotes"),"check_quotes")
WINDOWS := yes
else
WINDOWS := no
endif
Testing in Linux it worked. I'll back soon after reboot to try it in Windos...
 
Old 04-21-2016, 02:24 AM   #6
mastemmer
Member
 
Registered: Jul 2007
Location: Porto Alegre, Brazil
Distribution: Slackware
Posts: 44

Original Poster
Rep: Reputation: 17
Jolly good! It worked in Windows as well.
Since only Windows uses different commands, this may be used to write a multiplattform Makefile.

It uses the fact that the command echo exists in both OS, but behaves differently.
Posix systems remove quotes, but windos-cmd don't.

Last edited by mastemmer; 04-21-2016 at 02:33 AM.
 
1 members found this post helpful.
Old 01-21-2023, 06:15 PM   #7
Dave Nadler
LQ Newbie
 
Registered: Jan 2023
Posts: 1

Rep: Reputation: 10
Careful! This may not work...

Many development environments under Windows provide a unix-style 'echo' - which defeats this test. Examples: busybox, Eclipse environments from NXP and STM, etc.
Better to check if PATH variable contains semicolon, which it will not under unix.
Just got burned by this...
Hope that helps!
Best Regards, Dave
 
  


Reply

Tags
makefile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem operating command with DUAL operating system with ubuntu & windows 7 gne Linux - Newbie 2 02-01-2015 09:12 AM
hello do they have an windos installer that will replace all of your windos iso files shade sole General 2 03-12-2011 10:31 PM
Tracing Intruders connecting to our system in Redhat Linux operating system vkunasani Linux - Software 4 02-15-2010 07:23 AM
'Operating system not found' Any operating system installed wont work. TechniSlave Linux - Newbie 55 02-09-2009 12:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration