Fibonacci

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 42736
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 99 times
Been thanked: 108 times
Contact:

Fibonacci

Post by Antonio Linares »

Code: Select all | Expand

function Main()   local n    for n = 0 to 25      ? fibonacci( n )   next          return nilfunction fibonacci( n )   if n <= 1      return 1  endif      return fibonacci( n - 1 ) + fibonacci( n - 2 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
George
Posts: 726
Joined: Tue Oct 18, 2005 6:49 pm

Re: Fibonacci

Post by George »

Code: Select all | Expand

#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
User avatar
Enrico Maria Giordano
Posts: 8776
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 8 times
Contact:

Re: Fibonacci

Post by Enrico Maria Giordano »

Try this:

Code: Select all | Expand

#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
George
Posts: 726
Joined: Tue Oct 18, 2005 6:49 pm

Re: Fibonacci

Post by George »

Excellent! Thanks Enrico for you help.

Regards,

George
Post Reply