www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scoped external function declaration

reply "novice2" <sorry noem.ail> writes:
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
Jan 01 2015
next sibling parent "Daniel Kozak" <kozzi11 gmail.com> writes:
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(); }
Jan 01 2015
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.
=20
 Is my wish correct?
 Reduced code:
=20
=20
=20
 extern (C) int getch();
 void main() {
    getch();
 }
 //compiled OK
=20
=20
=20
 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.
Jan 01 2015
parent reply "novice2" <sorry noem.ail> writes:
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.
Jan 02 2015
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 02 Jan 2015 10:40:22 +0000
novice2 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Thanx Daniel, thanx Ketmar.
=20
 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. ;-)
Jan 02 2015