LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-01-2011, 08:39 PM   #1
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Rep: Reputation: 3
Question How to get the OS name with C


Hi people!

You knows how can I get the OS name?

I'd like to know this because I want to make a C application who runs on Linux and Windows.

Thanks!
 
Old 04-01-2011, 08:47 PM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
There are predefined operating system macros for doing such tasks, look here: http://predef.sourceforge.net/preos.html
 
1 members found this post helpful.
Old 04-01-2011, 09:03 PM   #3
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
Sorry but, I don't understand....

I look the site, but I don't understand how it works...

Can you talk in a "poor programmer" language? =P

Thanks for your time!!
 
Old 04-01-2011, 09:30 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I'm not sure if you can get the OS name during runtime, but when you are compiling, you can assign a variable that can be used to store the deduced OS name. For example:
Code:
#include <stdio.h>

#if defined(_WIN32) || defined(_WIN64)
        const char* os = "Windoze";
#else
#ifdef __linux
        const char* os = "Linux";
#else
        const char* os = "Unknown";
#endif
#endif

int main(void)
{
   printf("os = %s\n", os);
   return 0;
}
You can use similar ifdef statements in your code for it to call different library functions that are available under Linux and Windows. Of course, you should stick to standard (ISO) C as much as possible.

Last edited by dwhitney67; 04-01-2011 at 09:36 PM.
 
Old 04-01-2011, 09:34 PM   #5
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
Quote:
Originally Posted by MarcosPauloBR View Post
I'd like to know this because I want to make a C application who runs on Linux and Windows.
This may not be a popular answer but if all you want to accomplish is cross platform compatibility then you might try C#. All (XP+) windows machines have .NET pre-installed as part of the OS and you can use Mono in Linux. The C# language is so easy to read, use, and understand compared to C/C++
 
Old 04-01-2011, 10:33 PM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by MarcosPauloBR View Post
Sorry but, I don't understand....
You need to read this: http://gcc.gnu.org/onlinedocs/cpp/Macros.html

I remember faintly, for checking the Endianness we did something like this, previously, you'll get an idea, see below:
Code:
#if defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(_X86_)
        #define _LITTLE_ENDIAN 1

#elif defined(__ppc__) || defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) || defined(_M_PPC)
	#define _BIG_ENDIAN 1

#else
	#error "Please define system Endianess"

#endif

Last edited by Aquarius_Girl; 04-01-2011 at 10:36 PM.
 
Old 04-01-2011, 11:02 PM   #7
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
Quote:
I remember faintly, for checking the Endianness we did something like this, previously, you'll get an idea, see below:
Wouldn't you only need to bother checking endianness if you wanted to port across CPU architectures? The OP didn't specify anything other than to be able to run on Windows and Linux, and since Windows is pretty much x86(_64) only, checking for endianness would be meaningless (since it would be little-endian all the way).

As for checking the OS itself, I'm with dwhitney67 for using #ifdef statements to check for defined constants dependent upon the OS (e.g. _WIN32 for Windows, etc.)
 
Old 04-01-2011, 11:08 PM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by MrCode View Post
Wouldn't you only need to bother checking endianness if you wanted to port across CPU architectures? The OP didn't specify anything other than to be able to run on Windows and Linux, and since Windows is pretty much x86(_64) only, checking for endianness would be meaningless (since it would be little-endian all the way)
That Endianness code was an example for SYNTAX, only that's why I even wrote there:
Quote:
you'll get an idea

Last edited by Aquarius_Girl; 04-01-2011 at 11:13 PM.
 
Old 04-02-2011, 12:23 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
you might try C#
Kind of like somebody asking for driving directions, and your suggesting he take the plane

Besides, Mono is a freakin' 3000lb elephant. And the only decent development environment that does *everything* you want for .Net is Visual Studio. For Windows only. Non-Microsoft products NOT invited. I mean, theoretically, you can develop .Net programs with "vi" (or notepad). But in practice, for .Net languages (*unlike* C, C++, Python or most other common languages) you *need* an IDE to effectively develop for .Net. And, unfortunately, MonoDevelop is no Visual Studio

But we digress

The original question:
Quote:
Who knows how can I get the OS name?
1. If you want to get the OS name at *compile time*, you can use the macros Anisha Kaul suggested.


2. "Compile time" means:
a) When you build the program, you can "conditionally" compile for BSD vs. Solaris, or Linux vs. Mac OSX.
b) But if you copy the program to another PC, it will still say the same thing (e.g. "Linux", even if you tried to run the binary executable on a BSD host).

3. "Run time" means it will figure out the platform at runtime.
a) I would recommend using the standard call "utsname()".
b) Another approach is to check for (and read) any of the following "standard" system text files:
- /etc/redhat*, /etc/SusE*, etc:
http://www.novell.com/coolsolutions/feature/11251.html
c) You can also use "fopen()/fgets()" to read /proc/version

'Hope that helps .. PSM

Last edited by paulsm4; 04-02-2011 at 12:24 AM.
 
Old 04-02-2011, 01:06 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by MrCode View Post
checking for endianness would be meaningless (since it would be little-endian all the way).
Perhaps you might like to google "zLinux".
 
Old 04-02-2011, 06:42 AM   #11
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
I think I get it!

So, the macros are "variables" to get some "information" about the system?

And, I've seen this ifdef/endif in the standard libraries on /usr/include.

The ifdef/endif are some tests on these macros?

Thanks for all!!!!

Last edited by MarcosPauloBR; 04-02-2011 at 08:28 AM.
 
Old 04-02-2011, 09:16 AM   #12
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
Quote:
I mean, theoretically, you can develop .Net programs with "vi" (or notepad). But in practice, for .Net languages (*unlike* C, C++, Python or most other common languages) you *need* an IDE to effectively develop for .Net.
Again, I know this isn't going to be popular but that statement isn't true at all. Visual Studio does an outstanding job at integrating the compiler along with intellisense and a design editor but that design editor is only a tool and it isn't used much by enterprise developers. Developers on our team routinely use Eclipse and even notepad to write C# code that gets built and tested during out automated nightly builds.

You absolutely don't need visual studio to create, build, or compile C#.NET programs.

Also, .NET is pre-installed on all windows machines and Mono is pre-installed on most Linux distributions so for me personally not having to struggle with C/C++ to accomplish tasks that take a single line of code in C# is well worth any hassle that might accompany it.
 
Old 04-02-2011, 11:34 AM   #13
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by MarcosPauloBR View Post
So, the macros are "variables" to get some "information" about the system?
No, not at all.

These are evaluated not when you run your program, but when you compile it. Your compiler knows what kind of system you'll be using when you run the program you're compiling at this moment. On the basis of that, things like this will compile one set of code, or another:
Code:
#if defined (__linux)
printf("I knew when I was being compiled that I would be run on a Linux system\n");
#else
#if defined (_WIN32)
printf("I knew when I was being compiled that I would be run on a Microsoft Windows system\n");
#else
printf("I don't know, so I'll write some safe, non-ambitious code here.\n");
#endif
#endif
 
Old 04-03-2011, 09:51 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by MarcosPauloBR View Post
I think I get it!

So, the macros are "variables" to get some "information" about the system?

And, I've seen this ifdef/endif in the standard libraries on /usr/include.

The ifdef/endif are some tests on these macros?

Thanks for all!!!!
They are NOT C code, they are C Preprocessor directives.

The C Preprocessor (CPP) processes the code before it is compiled. It removes comments and substitutes macros. All lines beginning with "#" are directives and never reach the C compiler.

"#include" literally copies and pastes the contents of a file into the code, replacing the "#include" line.

"#define" defines a macro. Whenever CPP comes across it, it replaces it with its contents.

"#ifdef" checks if a macro exists, and if it doesn't, it deletes everything from itself to the matching #endif. I think #else is pretty self-explanatory here.

"#ifndef" is the inverse of #ifdef", it deletes its contents when the macro is defined.

Code:
#include <stdio.h>
#define thing a + b

#ifdef __linux
    char* os = "Linux";
#else
    char* os = "Other";
#endif

int main()
{
    a = 3;
    b = 4;
    printf("%d\n", thing);  // "thing" will be replaced with "a + b", because I defined a macro.
    printf("Your OS: %s\n", os); // note that CPP with remove these comments, too.
    return 0;
}
After passing through CPP under Linux, it looks like this, and this it what's given to the compiler:

Code:
<pretend that the entire contents of stdio.h are here>
char* os = "Linux";
int main()
{
    a = 3;
    b = 4;
    printf("%d\n", a + b);
    printf("Your OS: %s\n", os);
    return 0;
}

Last edited by MTK358; 04-03-2011 at 09:53 AM.
 
2 members found this post helpful.
Old 04-04-2011, 06:39 AM   #15
MarcosPauloBR
Member
 
Registered: Feb 2011
Location: Santa Catarina - Brazil
Distribution: Slackware
Posts: 129

Original Poster
Rep: Reputation: 3
So, thanks for the help of all!

Now, I understand how macros works!

And I could get the OS name sucessfully!

Solved boys!
 
  


Reply



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



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

All times are GMT -5. The time now is 05:48 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