www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking to MSVCRT

reply Robin Allen <r.a3 ntlworld.com> writes:
The Windows C runtime (MSVCRT.DLL) defines some functions, namely
_open_osfhandle and _fdopen, that I need to use in my D program.

However, D doesn't seem to link to the necessary libs by default; the linker
says:

Symbol Undefined __open_osfhandle 8
Symbol Undefined __fdopen 8

I've tried manually linking to snn.lib, which my text editor tells me *does*
include both these symbols, but to no avail. I've also tried generating my own
import library for MSVCRT using implib.exe, but this just results in the
additional error:

Previous Definition Different: _errno

Is there any way I can use these functions from D?
Jan 24 2007
next sibling parent reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Robin Allen" <r.a3 ntlworld.com> wrote in message 
news:ep7tei$26c3$1 digitaldaemon.com...
 The Windows C runtime (MSVCRT.DLL) defines some functions, namely 
 _open_osfhandle and _fdopen, that I need to use in my D program.

 However, D doesn't seem to link to the necessary libs by default; the 
 linker says:

 Symbol Undefined __open_osfhandle 8
 Symbol Undefined __fdopen 8

 Is there any way I can use these functions from D?

By the looks of it, those two seem to be simple wrappers for Win32's CreateFile function. You could simply call CreateFile yourself. If you have Visual Studio installed, you can peek at their source too, and just do whatever those functions do in D. L.
Jan 24 2007
parent Robin Allen <r.a3 ntlworld.com> writes:
Lionello Lunesu Wrote:

 
 "Robin Allen" <r.a3 ntlworld.com> wrote in message 
 news:ep7tei$26c3$1 digitaldaemon.com...
 The Windows C runtime (MSVCRT.DLL) defines some functions, namely 
 _open_osfhandle and _fdopen, that I need to use in my D program.

 However, D doesn't seem to link to the necessary libs by default; the 
 linker says:

 Symbol Undefined __open_osfhandle 8
 Symbol Undefined __fdopen 8

 Is there any way I can use these functions from D?

By the looks of it, those two seem to be simple wrappers for Win32's CreateFile function. You could simply call CreateFile yourself. If you have Visual Studio installed, you can peek at their source too, and just do whatever those functions do in D. L.

They convert from Win32 HANDLEs to FILE*s and back; I was trying to use them to redirect stdout to a console window. I'm just using WriteFile to write the strings instead of printf now, and that works just as well.
Jan 24 2007
prev sibling parent John Reimer <terminal.node gmail.com> writes:
On Wed, 24 Jan 2007 10:19:46 -0500, Robin Allen wrote:

 The Windows C runtime (MSVCRT.DLL) defines some functions, namely
_open_osfhandle and _fdopen, that I need to use in my D program.
 
 However, D doesn't seem to link to the necessary libs by default; the linker
says:
 
 Symbol Undefined __open_osfhandle 8
 Symbol Undefined __fdopen 8
 
 I've tried manually linking to snn.lib, which my text editor tells me *does*
include both these symbols, but to no avail. I've also tried generating my own
import library for MSVCRT using implib.exe, but this just results in the
additional error:
 
 Previous Definition Different: _errno
 
 Is there any way I can use these functions from D?

You can almost always load these functions up at runtime using LoadLibraryA/GetProcAddress calls. Doing it that way usually allows you to get around all those annoying import lib linking problems. It may not be pretty, but at least it guarantees your D program access to almost any C based dll no matter what compiler was used to make it. -JJR
Jan 25 2007