LinuxQuestions.org
Help answer threads with 0 replies.
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-2005, 02:53 PM   #1
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
Question about macros


Hello there, take a look at this code:
Code:
#define macro(a,a1,b,b1) \
#ifdef a \
a1;
#endif \
#ifdef b \
b1; \
#endif
I can't compile this code, i'm sure it's wrong, but it says what i want to do, i.e. :
Code:
macro(Q, printf("Q is defined\n"), W, printf("W is defined\n"));

/* This will be expanded to */ 

#ifdef Q
printf("Q is defined\n");
#endif
#ifdef W
printf("W is defined\n");
#endif
So, how must i rewrite my macro?

Thanks in advance.
 
Old 04-01-2005, 04:15 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
It's a bit of an unusual thing you are trying here:
You defined a preprocessor macro that expands to new preprocessor directives.

This would require the preprocessor to run recursively over your source code. Though I've never thought of this idea, I would be surprised if the preprocessor works this way.

You could however try to run the preprocessor twice: the first time youself by running the "cpp" command, and have the compiler run it the second time the normal way. I think this would be frowned upon by many people (including me), but it may work.

Last edited by Hko; 04-01-2005 at 04:17 PM.
 
Old 04-01-2005, 05:48 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
cpp will expand macros inside include files.
You could try putting thiose commands inside nested include files.

Although as to why you are doing this I'm with Hko.
There must be a better way.
 
Old 04-02-2005, 03:17 AM   #4
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
So, what's the better way to do that?

Thanks.
 
Old 04-03-2005, 04:34 AM   #5
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
It's already done, i've changed the code's layout. Thanks.

Since these constant are well-known and there are only two, i've done the following:

Code:
#ifdef Q
#define macro(a,b) \
a;
#elif defined(W)
#define macro(a,b) \
b;
#endif
 
  


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
NEdit macros theonebeyond Linux - Software 2 05-29-2018 07:04 PM
Macros In C drumstick Programming 8 11-21-2005 05:37 PM
vi editor macros sailu_mvn Linux - Software 6 10-12-2005 02:44 AM
Macros Twi7ch Linux - General 2 08-27-2005 10:37 PM
keystroke macros squip Programming 1 10-09-2003 01:46 PM

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

All times are GMT -5. The time now is 08:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration