Hi,

Compiling the following code in MSVC6 gives the linker error 'unresolved
external symbol __ftol'..

// -- Code begin --
#pragma comment(linker, "/entry:Main")
#pragma comment(linker, "/nodefaultlib")
#pragma comment(linker, "/opt:nowin98")

long ftol(double const f)
{ int n;
__asm fld f
__asm fistp n
if(f - n < 0) n--;
return n;
}

extern "C" int const _fltused = 1;

int __stdcall Main(void)
{ double x = 1.0f;
return (int)x; // This line causes the linker error
}
// -- Code end --

..I was wondering if it is possible to make my ftol be the one used
automatically by MSVC6 (so I don't have to do { return ftol(x); } ). The
closest I've come to getting this to work is by adding the following after
the { extern "C" .. _fltused = 1 } line, which stops the linker complaining,
but still doesn't work as the program crashes..

extern "C" long (*_ftol)(double const) = ftol;

..
Thank you for any help,
Kind regards,
Eliott

Re: replace __ftol MSVC6 by Tim

Tim
Sun May 27 21:53:59 CDT 2007

"MRe" <m.r.e.@.d.u.b.l.i.n...i.e> wrote:
>
> Compiling the following code in MSVC6 gives the linker error 'unresolved
>external symbol __ftol'..

Yes, because of the /nodefaultlib. __ftol would ordinarily be supplied by
the C runtime library, but you have suppressed it.

Notice that the cdecl standard adds one underline to the name. Thus, the
symbol __ftol would be written in C as _ftol. So, change this line:

> long ftol(double const f)
to this:
long _ftol( const double f )

For completeness, you probably want:
extern "C"
long __cdecl _ftol( const double f )
{
...
}
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: replace __ftol MSVC6 by Ben

Ben
Mon May 28 10:08:13 CDT 2007


"MRe" <m.r.e.@.d.u.b.l.i.n...i.e> wrote in message
news:egxxmrLoHHA.1220@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> Compiling the following code in MSVC6 gives the linker error 'unresolved
> external symbol __ftol'..
>
> // -- Code begin --
> #pragma comment(linker, "/entry:Main")
> #pragma comment(linker, "/nodefaultlib")
> #pragma comment(linker, "/opt:nowin98")
>
> long ftol(double const f)
> { int n;
> __asm fld f
> __asm fistp n
> if(f - n < 0) n--;
> return n;
> }
>
> extern "C" int const _fltused = 1;
>
> int __stdcall Main(void)
> { double x = 1.0f;
> return (int)x; // This line causes the linker error
> }
> // -- Code end --
>
> ..I was wondering if it is possible to make my ftol be the one used
> automatically by MSVC6 (so I don't have to do { return ftol(x); } ). The
> closest I've come to getting this to work is by adding the following after
> the { extern "C" .. _fltused = 1 } line, which stops the linker
> complaining, but still doesn't work as the program crashes..
>
> extern "C" long (*_ftol)(double const) = ftol;

Although the C syntax is the same for calling a function by name, or through
a pointer, the compiled code is decidedly different. The compiler assumed
that __ftol is a real function, and is calling it accordingly.

Do what Tim Roberts suggests in order to make __ftol the name of the real
function.

>
> ..
> Thank you for any help,
> Kind regards,
> Eliott
>



Re: replace __ftol MSVC6 by MRe

MRe
Mon May 28 18:26:50 CDT 2007

> For completeness, you probably want:
> extern "C"
> long __cdecl _ftol( const double f )
> {
> ...
> }

Thank you for this; it now compiles, and runs without crashing.. however,
the value of parameter 'f' is always 0.0 when calling _ftol implicitly (it's
correct when calling explicitly). I.E. { (int)x } always returns 0, whereas
{ _ftol(x) } returns correct value.

Any idea why?

Thank you again,
Kind regards,
Eliott



Re: replace __ftol MSVC6 by MRe

MRe
Mon May 28 18:33:11 CDT 2007

>> extern "C" long (*_ftol)(double const) = ftol;
>
> Although the C syntax is the same for calling a function by name, or
> through a pointer, the compiled code is decidedly different. The compiler
> assumed that __ftol is a real function, and is calling it accordingly.

Of course, makes sense. Thank you.

> Do what Tim Roberts suggests in order to make __ftol the name of the real
> function.

I gave it a twirl, but came across another problem (detailed in my response
to Tim Roberts); would you know what the problem might be with it?

Thank you again,
Kind regards,
Eliott



Re: replace __ftol MSVC6 by MRe

MRe
Mon May 28 19:34:31 CDT 2007

> the value of parameter 'f' is always 0.0 when calling _ftol implicitly

Sorry, found the problem: (int)x performs the load op before the call, so I
don't need { __asm fld f } in my function.

Thank you again kindly for the help,
Eliott