FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index Utilities / Utilidades Fibonacci
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Fibonacci
Posted: Sun Aug 22, 2021 01:58 PM
Code (fw): Select all Collapse
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 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 729
Joined: Tue Oct 18, 2005 06:49 PM
Re: Fibonacci
Posted: Thu Aug 26, 2021 10:57 AM
Code (fw): Select all Collapse
#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
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Fibonacci
Posted: Thu Aug 26, 2021 02:25 PM
Try this:

Code (fw): Select all Collapse
#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
Posts: 729
Joined: Tue Oct 18, 2005 06:49 PM
Re: Fibonacci
Posted: Thu Aug 26, 2021 03:05 PM

Excellent! Thanks Enrico for you help.

Regards,

George

Continue the discussion