digitalmars.D.learn - Scoped external function declaration
- novice2 (16/16) Jan 01 2015 I want to use external or C function.
- Daniel Kozak (11/27) Jan 01 2015 you can use local import
- ketmar via Digitalmars-d-learn (12/35) Jan 01 2015 On Thu, 01 Jan 2015 17:51:45 +0000
- novice2 (4/4) Jan 02 2015 Thanx Daniel, thanx Ketmar.
- ketmar via Digitalmars-d-learn (8/13) Jan 02 2015 On Fri, 02 Jan 2015 10:40:22 +0000
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
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
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
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
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









"Daniel Kozak" <kozzi11 gmail.com> 