Re: Scoped external function declaration

2015-01-02 Thread ketmar via Digitalmars-d-learn
On Fri, 02 Jan 2015 10:40:22 +
novice2 via Digitalmars-d-learn 
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. ;-)


signature.asc
Description: PGP signature


Re: Scoped external function declaration

2015-01-02 Thread novice2 via Digitalmars-d-learn

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.


Re: Scoped external function declaration

2015-01-01 Thread ketmar via Digitalmars-d-learn
On Thu, 01 Jan 2015 17:51:45 +
novice2 via Digitalmars-d-learn 
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.


signature.asc
Description: PGP signature


Re: Scoped external function declaration

2015-01-01 Thread Daniel Kozak via Digitalmars-d-learn

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();
}


Scoped external function declaration

2015-01-01 Thread novice2 via Digitalmars-d-learn

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