Page 1 of 1

Fibonacci

PostPosted: Sun Aug 22, 2021 1:58 pm
by Antonio Linares
Code: Select all  Expand view
function Main()

   local n

   for n = 0 to 25
      ? fibonacci( n )
   next          

return nil

function fibonacci( n )

  if n <= 1
     return 1
  endif      

return fibonacci( n - 1 ) + fibonacci( n - 2 )

Re: Fibonacci

PostPosted: Thu Aug 26, 2021 10:57 am
by George
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
   local n, nStart, nDelay, nFibonacci

   set decimal to 8
   nStart := Seconds()
   n = 32
   nFibonacci := C_FIBONACCI( n )
   nDelay := Seconds() - nStart
   ? "n, Fibonacci, delay = ", n, nFibonacci, nDelay

   RETURN nil

//--------------------------------------
// C function
//-------------------------------------
#pragma BEGINDUMP

#include <hbapi.h>
#include <math.h>
long long C_FIBONACCI( int n );

HB_FUNC( C_FIBONACCI )

   {
      int n;
      n = hb_parni(1);
      if ( n == 0 )
      hb_retnll(0);
      else if ( n == 1 )
         hb_retnll(1);
      else
         hb_retnll ( C_FIBONACCI(n-1) + C_FIBONACCI(n-2) );
   }

#pragma ENDDUMP


El compilador MS-Visual Studio 2019 funciona sin generar error.
El problema viene cuando trata de linkear la funcion C_FIBONACCI.

Aqui esta la pantalla completa del proceso:
┌────────────────────────────────────────────────────────────────────────────┐
│ FWH 64 for Harbour 21.06 (VS64bits) Jun. 2021 Harbour development power │▄
│ (c) FiveTech 1993-2021 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 │█
└────────────────────────────────────────────────────────────────────────────┘█
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.3
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
Compiling...
Harbour 3.2.0dev (r2011030937)
Copyright (c) 1999-2020, https://harbour.github.io/
Compiling 'c_fib3.prg' and generating preprocessed output to 'c_fib3.ppo'...
Lines 5038, Functions/Procedures 1
Generating C source output to 'c_fib3.c'... Done.
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30038.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

c_fib3.c
c_fib3.obj : error LNK2019: unresolved external symbol C_FIBONACCI referenced in function HB_FUN_C_FIBONACCI
c_fib3.exe : fatal error LNK1120: 1 unresolved externals
* Linking errors *

¿Alguna sugerencia de como se debe declarar la funcion prototipo de C_FIBONACCI(), en Harbour, antes de llamar la funcion recursiva en C?

Saludos,

George

Re: Fibonacci

PostPosted: Thu Aug 26, 2021 2:25 pm
by Enrico Maria Giordano
Try this:

Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
   local n, nStart, nDelay, nFibonacci

   set decimal to 8
   nStart := Seconds()
   n = 32
   nFibonacci := C_FIBONACCI( n )
   nDelay := Seconds() - nStart
   ? "n, Fibonacci, delay = ", n, nFibonacci, nDelay

   RETURN nil

//--------------------------------------
// C function
//-------------------------------------
#pragma BEGINDUMP

#include <hbapi.h>
#include <math.h>
long long C_Fibonacci( int n )
{
      if ( n == 0 )
      return (0);
      else if ( n == 1 )
         return (1);
      else
         return ( C_Fibonacci(n-1) + C_Fibonacci(n-2) );
}

HB_FUNC( C_FIBONACCI )

   {
      int n;
      n = hb_parni(1);
      hb_retnll ( C_Fibonacci(n) );
   }

#pragma ENDDUMP


EMG

Re: Fibonacci

PostPosted: Thu Aug 26, 2021 3:05 pm
by George
Excellent! Thanks Enrico for you help.

Regards,

George