ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386
Rep:
Quote:
Originally Posted by MarcosPauloBR
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++
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.)
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.
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
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386
Rep:
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.
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
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.