www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Where are declared and how to make build these c identifiers?

reply Cleverson Casarin Uliana <clul mm.st> writes:
Hi all, I'm revisiting D after some years. I'd like to test a code 
snipped from RosettaCode, which is suposed to check a key press, as follows:

extern (C) {
     void _STI_conio();
     void _STD_conio();
     int kbhit();
     int getch();
}

void main() {
     _STI_conio();
     char c;
     if (kbhit())
         c = cast(char)getch();
     _STD_conio();
}

However, I'd like to know how all these functions really work, so I'd 
like to know where they are declared and/or defined. Since there are 
that extern (C) line at the beginning of the snippet, I assume them to 
be c code, so my question is, is there any kinda C library refference 
where I can find such declarations, similar to the Phobos library 
refference in D? In principle, I couldn't find e.g. _sti_conio by 
searching on Google.

In addition, I went to try building the code above with:
ldc2 -release -Os keypress.d
but received this error message:
lld-link: error: undefined symbol: _STI_conio
 referenced by keypress.obj:(_Dmain)
lld-link: error: undefined symbol: _STD_conio
 referenced by keypress.obj:(_Dmain)
Error: linking with LLD failed As far as I recall, such kind of error indicates a library is missing on the command line. Does anyone know what it is please? Thanks for your help, Cleverson
Feb 23
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 23 February 2019 at 21:10:19 UTC, Cleverson Casarin 
Uliana wrote:
 However, I'd like to know how all these functions really work, 
 so I'd like to know where they are declared and/or defined.
Those functions are extensions from the Digital Mars C runtime library (inspired by something Borland offered back in the day). Only real docs for them are here: https://digitalmars.com/rtl/conio.html The STI_conio one isn't documented, I think that is the internal initializer function, and STD_conio is the destructor function.
 lld-link: error: undefined symbol: _STD_conio
 referenced by keypress.obj:(_Dmain)
Those functions are only present in the DMC library, which is only used when using `dmd` to make a 32 bit build on Windows (and even then, only if you do NOT use `-m32mscoff`). I would suggest using different functions... like my terminal.d <https://github.com/adamdruppe/arsd/blob/master/terminal.d>library which has similar api (see example: http://dpldocs.info/experimental-docs/arsd.terminal.html#single-key ) What do you need the program to actually do?
Feb 23
next sibling parent Cleverson Casarin Uliana <clul mm.st> writes:
Hi Adam, many thanks for all informations. To answer your questions:

Em 23/02/2019 20:08, Adam D. Ruppe via Digitalmars-d-learn escreveu:
 I would suggest using different functions... like my terminal.d 
 <https://github.com/adamdruppe/arsd/blob/master/terminal.d>library which 
 has similar api (see example: 
 http://dpldocs.info/experimental-docs/arsd.terminal.html#single-key )
 
OK, do I need to install the entire arsd package? I've saved the terminal.d file on an arsd directory I've just created inside the import directory, but trying to build it gives me lots of undefined symbol errors. Admittedly, I haven't yet learned how to deal with D packages, so maybe I should do just that.
 What do you need the program to actually do?
I want a generic function that returns what key I have pressed, including keys like the arrows, so I can for example play a sound on a given key press. If the method is portable among Windows, Linux and others, so better. BTW, although not strictly on topic, I'd also apreciate portable functions to play and stop sounds assynchronously, similar to PlaySound on Windows... Greetings, Cleverson
Feb 23
prev sibling next sibling parent destructionator gmail.com writes:
On Sat, Feb 23, 2019 at 09:53:09PM -0300, Cleverson Casarin Uliana via
Digitalmars-d-learn wrote:
 OK, do I need to install the entire arsd package? I've saved the terminal.d
 file on an arsd directory I've just created inside the import directory, but
 trying to build it gives me lots of undefined symbol errors.
You just need to pass the terminal.d file to your build too. dmd yourfile.d terminal.d so it knows to include it.
 I want a generic function that returns what key I have pressed, including
 keys like the arrows, so I can for example play a sound on a given key
 press. If the method is portable among Windows, Linux and others, so better.
Yeah, terminal.d can do that, I don't remember if the getch function specifically handles arrows... I think it does though, but I know for sure the other functions do (you can do a full event loop with it)
 BTW, although not strictly on topic, I'd also apreciate portable functions
 to play and stop sounds assynchronously, similar to PlaySound on Windows...
so i have that but i'm not happy with it and it isn't documented...
Feb 23
prev sibling parent reply Cleverson Casarin Uliana <clul mm.st> writes:
Em 24/02/2019 00:19, Adam D. Ruppe via Digitalmars-d-learn escreveu:
 You just need to pass the terminal.d file to your build too.
 
 dmd yourfile.d terminal.d
 
 so it knows to include it.
 
OK, it built the executable, but it doesn't work for every key, e.g. when I try with arrow and function keys, it always returns the same symbol, so I'm unable to distinguish what key has been pressed: Press any key to continue... You pressed 󰁰 Changing the code page on the console doesn't help either. I'm using the command prompt on a Windows 10 Pro system. In Powershell, it behaves the same. Greetings, Cleverson
Feb 24
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 24 February 2019 at 12:37:21 UTC, Cleverson Casarin 
Uliana wrote:
 OK, it built the executable, but it doesn't work for every key, 
 e.g. when I try with arrow and function keys, it always returns 
 the same symbol, so I'm unable to distinguish what key has been 
 pressed:
Those don't have printable characters associated, so they all look the same. Try writeln(cast(int) code) - casting it to int so you can see the number - and you should see the different results. Or even terminal.writeln("You pressed ", cast(KeyboardEvent.Key) ch); might be a better demo, i should change the docs, since that will print the name of most keys! supported key list: http://dpldocs.info/experimental-docs/arsd.terminal.KeyboardEvent.Key.html (lowest common denominator between windows and linux)
Feb 24
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 24 February 2019 at 14:34:45 UTC, Adam D. Ruppe wrote:
 supported key list:
well, obviously, plus the normal printable characters like letters, numbers, tab, space, enter... but those have char values so no need to list in an enum.
Feb 24
prev sibling parent Cleverson Casarin Uliana <clul mm.st> writes:
Em 24/02/2019 11:34, Adam D. Ruppe via Digitalmars-d-learn escreveu:
 Those don't have printable characters associated, so they all look the 
 same. Try writeln(cast(int) code)  - casting it to int so you can see 
 the number - and you should see the different results.
 
 Or even
 
   terminal.writeln("You pressed ", cast(KeyboardEvent.Key) ch);
 
Thanks much, it works great. Greetings, Cleverson
Feb 24