www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to declare C's static function?

reply aki <aki google.com> writes:
Hello,

When I porting legacy app. written in C to D,
I have a problem.

file a.d:
extern (C) private void foo() {}

file b.d:
extern (C) private void foo() {}

  Error 1: Previous Definition Different : _foo

In C language, "static void foo(){}" does not
export the symbol out side the compilation unit.
In D, the function foo() above conflicts even if
it is private. How can I declare C's static function?

Regards,
Aki.
Mar 27 2016
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
Do you need it to use extern(C)?
Because if you don't, just drop that. D's mangling will fix it.
Mar 27 2016
parent reply aki <aki google.com> writes:
On Monday, 28 March 2016 at 04:12:56 UTC, Rikki Cattermole wrote:
 Do you need it to use extern(C)?
 Because if you don't, just drop that. D's mangling will fix it.
Yes, I do need extern(C) for some reason like: alias Hook = extern(C) void function(); void sethook(Hook func) { ... } ... sethook(&foo) ... Some function expects such a function as an argument. Aki.
Mar 27 2016
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On Monday, 28 March 2016 at 04:27:27 UTC, aki wrote:
 On Monday, 28 March 2016 at 04:12:56 UTC, Rikki Cattermole 
 wrote:
 Do you need it to use extern(C)?
 Because if you don't, just drop that. D's mangling will fix it.
Yes, I do need extern(C) for some reason like: alias Hook = extern(C) void function(); void sethook(Hook func) { ... } ... sethook(&foo) ... Some function expects such a function as an argument. Aki.
Okay so you're needing extern(C). I was assuming they were just internal functions. You have two choices. Change the name in code (so manual mangling) or use pragma(mangle, ...) to change it instead.
Mar 27 2016
parent reply aki <aki google.com> writes:
On Monday, 28 March 2016 at 04:33:06 UTC, Rikki Cattermole wrote:
 You have two choices. Change the name in code (so manual 
 mangling) or use pragma(mangle, ...) to change it instead.
So... You mean there are no way to declare functions without exporting the symbol? There are so many instances to change.. But thanks any way. Aki.
Mar 27 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote:
 So... You mean there are no way to declare functions
 without exporting the symbol?
alas, no, even if it is private it can conflict on the outside (so stupid btw). Is it all the same function being referenced? Just importing from there would be ok.
Mar 28 2016
next sibling parent aki <aki google.com> writes:
On Monday, 28 March 2016 at 14:40:40 UTC, Adam D. Ruppe wrote:
 On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote:
 So... You mean there are no way to declare functions
 without exporting the symbol?
alas, no, even if it is private it can conflict on the outside (so stupid btw). Is it all the same function being referenced? Just importing from there would be ok.
Thank you for clarify. Now I know I have to change the name as a result. Thanks, aki.
Mar 28 2016
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 28 March 2016 at 14:40:40 UTC, Adam D. Ruppe wrote:
 On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote:
 So... You mean there are no way to declare functions
 without exporting the symbol?
alas, no, even if it is private it can conflict on the outside (so stupid btw).
Seems to be fixed in the latest beta (finally): http://dlang.org/changelog/2.071.0.html#dip22
Mar 28 2016
parent aki <aki google.com> writes:
On Tuesday, 29 March 2016 at 01:04:50 UTC, Mike Parker wrote:
 On Monday, 28 March 2016 at 14:40:40 UTC, Adam D. Ruppe wrote:
 On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote:
 So... You mean there are no way to declare functions
 without exporting the symbol?
alas, no, even if it is private it can conflict on the outside (so stupid btw).
Seems to be fixed in the latest beta (finally): http://dlang.org/changelog/2.071.0.html#dip22
Good news!
Mar 28 2016
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2016-03-28 06:02, aki wrote:
 Hello,

 When I porting legacy app. written in C to D,
 I have a problem.

 file a.d:
 extern (C) private void foo() {}

 file b.d:
 extern (C) private void foo() {}

   Error 1: Previous Definition Different : _foo

 In C language, "static void foo(){}" does not
 export the symbol out side the compilation unit.
 In D, the function foo() above conflicts even if
 it is private. How can I declare C's static function?
Can you declare the function as "package" in one module and import it into the other module? -- /Jacob Carlborg
Mar 28 2016