LinuxQuestions.org
Visit Jeremy's Blog.
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-24-2005, 04:30 PM   #1
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
Guess the kernel and platform the code is compiling on


Hi there. I'm wondering if my source code (by means of gcc, of course) can guess the kind of kernel my machine is using and the platform it's being currently running on.
Something that looks like this:
Code:
#ifdef X86
      ... /* Do something specific of x86 platform */
#elif IA64
      ... /* Do something specific of IA64 platform */
#endif
#ifdef LINUX
      ... /* Do something specific of Linux kernel */
#elif BSD
      ... /* Do something specific of BSD kernel */
#endif
It's like a predefined preprocessor constant by gcc.
Any ideas?

Thanks in advance.
 
Old 04-25-2005, 04:39 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You can use GCC-specific predefined macros. This will work on all platforms, but only under gcc. To get a list, run
Code:
touch /tmp/foo.h gcc -dH /tmp/foo.h
Incedentally, it's usually considered bad practice to use #ifdef unnecessarily like you have done. It would be more readable to write your example as:
Code:
if(X86) {
      ... /* Do something specific of x86 platform */
} else if (IA64) {
      ... /* Do something specific of IA64 platform */
}

if(LINUX) {
      ... /* Do something specific of Linux kernel */
} else if(BSD) {
      ... /* Do something specific of BSD kernel */
}
This generates the same code, but also has the advantage that the compiler will point out errors in code that isn't used.


As an alternative, you could use a makefile, and define some constants:

Code:
CCFLAGS=-DKERN=`uname -s` -DKERN_VER=`uname -v` -DKERN_REL=`uname -r`-DHARDWARE=`uname -m` -DPROC=`uname -p`-DPLATFORM=`uname -p` -DOS=`uname -o`

target : gcc $CCFLAGS src1.c src2.c src3.c
This will work on most (all?) POSIX systems, regardless of the compiler.
 
Old 04-27-2005, 03:49 PM   #3
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
Thanks a lot rjlee. Do you know something more about the first method (gcc method)? A link or anything is appreciated. Thanks ever so much.
 
Old 04-27-2005, 03:55 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
A quick search on google yields up a list with descriptions at http://gcc.gnu.org/onlinedocs/gcc-4....defined-Macros
 
  


Reply


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
Kernel Filter Failed error while compiling packet capturing code on FC2 Prakhardeep Programming 3 08-10-2011 04:55 AM
Compiling Kernel and other source code davegermiquet Conectiva 2 01-26-2005 06:01 PM
Compiling Kernel Code vbhatia_81 Programming 1 06-21-2004 06:53 AM
How can i send message from Linux platform to Windows platform linuxeagle Linux - Networking 1 02-17-2004 12:35 AM
source code and platform question iggymac Linux - Software 3 01-24-2002 05:43 PM

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

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