LinuxQuestions.org
Help answer threads with 0 replies.
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 10-02-2012, 07:19 AM   #1
wcrandthor
LQ Newbie
 
Registered: Aug 2012
Distribution: OpenSUSE 12.1
Posts: 9

Rep: Reputation: Disabled
#defines inside an enum


I was looking at linux/include/linux/rtnetlink.h

Code:
enum {
	RTM_BASE	= 16,
#define RTM_BASE	RTM_BASE

	RTM_NEWLINK	= 16,
#define RTM_NEWLINK	RTM_NEWLINK
Why are these #defines embedded within the enum here? What's the point of this type of code?
Note I'm not talking about this rtnetlink.h functionality specifically... just in general, the only place I've seen #defines embedded in enum's is in linux code.

Last edited by wcrandthor; 10-02-2012 at 09:49 AM. Reason: clarification
 
Old 10-03-2012, 03:54 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Those #defines seem to be intended to sort of undefine those symbols as preprocessor symbols right after potentially using the preprocessor symbols to define names in the enum.

Out of context, I'm failing to guess the intent.

The preprocessor acts as if it were executed before the rest of compilation, so the preprocessor does not care that the #define happens to land inside the body of the enum definition.

Edit: After using google, I have a better guess (mainly expanding on the answer that you seem to have gotten when asking the same question in a different forum):

Assuming there are many variants of rtnetlink.h floating around (for different OS's etc.) and some of those defined RTM_BASE by #define and some didn't define it at all, then user code depending on rtnetlink.h but wanting to be portable would include code such as
Code:
#ifdef RTM_BASE
to see if RTM_BASE is defined. But then a version, such as you quoted, that defines it by enum would break such user code, so redundantly define it by #define (as itself) to allow that test.

Last edited by johnsfine; 10-03-2012 at 04:02 PM.
 
1 members found this post helpful.
Old 10-03-2012, 04:34 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Preprocesssor does run in a separated process before the compilation, and yes, these defines are meant for #ifdef's.
 
  


Reply

Tags
code, defined, enumerator, preprocessor variable



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
MAC OS X defines son_t Programming 2 07-24-2007 04:20 AM
Questions about enum. What would be a good use of enum? RHLinuxGUY Programming 5 06-11-2006 01:13 PM
typedef defines which type? kpachopoulos Programming 1 11-10-2005 02:46 AM
override defines alaios Programming 1 09-14-2005 10:40 AM
OS Conditional Defines kevin.cowley Programming 1 09-24-2003 03:54 PM

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

All times are GMT -5. The time now is 01:17 AM.

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