User avatar
Paeryn
Posts: 3147
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: questions to C libs also for g++

Sat Sep 26, 2020 9:57 am

dsyleixa123 wrote:
Sat Sep 26, 2020 8:46 am
So what about this
Perhaps instead (what I can possibly suspect from Paeryn's post) something like, e.g.,

Code: Select all

#ifdef __cplusplus
   extern "C" { extern int ads1115Setup (int pinBase, int i2cAddress) ; }
#else
   extern int ads1115Setup (int pinBase, int i2cAddress) ; 
#endif
Yes, though if you are declaring a single function with C linkage you'd omit the braces and second extern

Code: Select all

extern "C" int ads1115Setup (int pinBase, int i2cAddress) ;
But nobody in their right mind would write out the whole function declaration twice, that would mean if you ever change anything you'd have to make sure you change both versions. Far better to split the opening and closing parts of the extern "C" {...} so that it surrounds all the declarations it needs to, nicely avoiding duplication of function declarations.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

dsyleixa123
Posts: 1117
Joined: Mon Jun 11, 2018 11:22 am

Re: questions to C libs also for g++

Sat Sep 26, 2020 10:32 am

ok, thanks, now I understand a little more about the intial obfuscations.
Just for clearification:

Code: Select all

 extern "C" { extern int ads1115Setup (int pinBase, int i2cAddress) ; }
is that legal, or MUST I omit the braces and the 2nd "extern"?

For my own purposes I would prefer the duplication though, for the benefit of better readability 8-)

User avatar
Paeryn
Posts: 3147
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: questions to C libs also for g++

Sat Sep 26, 2020 10:55 am

dsyleixa123 wrote:
Sat Sep 26, 2020 10:32 am
ok, thanks, now I understand a little more about the intial obfuscations.
Just for clearification:

Code: Select all

 extern "C" { extern int ads1115Setup (int pinBase, int i2cAddress) ; }
is that legal, or MUST I omit the braces and the 2nd "extern"?

For my own purposes I would prefer the duplication though, for the benefit of better readability 8-)
Yes, you can have one declaration in a block, just that for a single declaration you can omit the braces and inner extern because in this case (and only this case) the extern "C" implies extern. So the following two lines have the same effect.

Code: Select all

extern "C" { extern int ads1115Setup (int pinBase, int i2cAddress) ; }
extern "C" int ads1115Setup (int pinBase, int i2cAddress) ;
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

dsyleixa123
Posts: 1117
Joined: Mon Jun 11, 2018 11:22 am

Re: questions to C libs also for g++

Sat Sep 26, 2020 11:00 am

thanks again, @all you, guys! 8-)

Return to “C/C++”