# Preprocessor query

## Recommended Posts

Anyone got any idea what I am doing wrong here? This is GCC.

#define test(a, b) int a ## b

void f()
{
test(name, __LINE__);
}

This is creating a variable called "name__LINE__" rather than "name124" or whatever.

I'm trying ultimately to do something like:

#define lock_flag(a) flag_lock_object _temp##__COUNTER__(a)

But I can't seem to get the basics of expanding a macro within a macro working? All web research I've done suggests I'm doing it correctly so driving me a little nuts.

Thanks.

##### Share on other sites

Won't this turn __COUNTER__(a) into a string instead of calling macro and turning result into a string?

There's some command line parameter to output preprocessed file, that way you can see result you're getting instead of playing guessing game.

##### Share on other sites

That's an infamous gotcha of trying to concat macros' contents instead of their names, you just need 2x indirection,

#define test_helper(a, b) int a ## b
#define test(a, b) test_helper(a, b)

void f()
{
test(name, __LINE__);
}


##### Share on other sites

Won't this turn __COUNTER__(a) into a string instead of calling macro and turning result into a string?

There's some command line parameter to output preprocessed file, that way you can see result you're getting instead of playing guessing game.

No. Think you are thinking of the # (stringize) operator rather than ## (concatenation) operator.

And I'm not guessing. I can see what is being produced in the errors I get if I create more than one in a scope. But using the flag to inspect is a good idea. Thanks.

##### Share on other sites

That's an infamous gotcha of trying to concat macros' contents instead of their names, you just need 2x indirection,

#define test_helper(a, b) int a ## b#define test(a, b) test_helper(a, b)void f(){    test(name, __LINE__);}
See for full technicalities: http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr
Thank you. I think I tried that after some googling last night but I will try again based on yours and post the full if it fails again.

 Ah, I think I can see something I didn't do in the link you posted. Cheers, much appreciated.

##### Share on other sites
Got it working a treat, thanks.

#define _lock_flag_helper_final(a, b) flag_lock_object _flag_lock_object_##b(a)
#define _lock_flag_helper(a, b) _lock_flag_helper_final(a, b)
#define lock_flag(a) _lock_flag_helper(a, __COUNTER__)


## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627737
• Total Posts
2978880

• 10
• 10
• 21
• 14
• 13