Scoped external function declaration

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 1 17:05:38 PST 2015


On Thu, 01 Jan 2015 17:51:45 +0000
novice2 via Digitalmars-d-learn <digitalmars-d-learn at 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150102/fa96b600/attachment.sig>


More information about the Digitalmars-d-learn mailing list