Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 01, 2015 Scoped external function declaration | ||||
---|---|---|---|---|
| ||||
I want to use external or C function. It used only one time from one D function. I want do declare C function inside D function. I don't want to declare C function in global scope. Is my wish correct? Reduced code: extern (C) int getch(); void main() { getch(); } //compiled OK void main() { extern (C) int getch(); getch(); } //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi |
January 01, 2015 Re: Scoped external function declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | On Thursday, 1 January 2015 at 17:51:46 UTC, novice2 wrote:
> I want to use external or C function.
> It used only one time from one D function.
> I want do declare C function inside D function.
> I don't want to declare C function in global scope.
>
> Is my wish correct?
> Reduced code:
>
>
>
> extern (C) int getch();
> void main() {
> getch();
> }
> //compiled OK
>
>
>
> void main() {
> extern (C) int getch();
> getch();
> }
> //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi
you can use local import
cfun.d:
module cfun;
extern (C) int getc();
main.d:
module main;
void main() {
import cfun;
getc();
}
|
January 02, 2015 Re: Scoped external function declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 Attachments: | On Thu, 01 Jan 2015 17:51:45 +0000 novice2 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I want to use external or C function. > It used only one time from one D function. > I want do declare C function inside D function. > I don't want to declare C function in global scope. > > Is my wish correct? > Reduced code: > > > > extern (C) int getch(); > void main() { > getch(); > } > //compiled OK > > > > void main() { > extern (C) int getch(); > getch(); > } > //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi nope, you can't do this. actually, there is `pragma(mangle)`, but it's not working for nested declarations. but you can use it to rename your import: pragma(mangle, "getch") extern (C) int external_getch(); void main() { external_getch(); } this will allow you to avoid name conflicts in your D code, yet still link to external `getch` function. |
January 02, 2015 Re: Scoped external function declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | Thanx Daniel, thanx Ketmar. I just thinked that this is some sort of bug. May be DMD should not change mangled name of external function... Bit i dont know. |
January 02, 2015 Re: Scoped external function declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 Attachments: | On Fri, 02 Jan 2015 10:40:22 +0000 novice2 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Thanx Daniel, thanx Ketmar. > > I just thinked that this is some sort of bug. > May be DMD should not change mangled name of external function... > Bit i dont know. with `extern(C)` it didn't. what you probably not realised is that D has nested functions, so extern which declared inside other function becomes right this: an extern nested function. that's why D compiler mangles it's name. there are no nested functions in C, but D is not C. ;-) |
Copyright © 1999-2021 by the D Language Foundation