Page 1 of 1

[6.10] Always error with C Backend

Posted: Wed Apr 10, 2024 7:48 am
by al2791
With this code I have an error ;

Code: Select all

CompilerIf #PB_Compiler_ExecutableFormat<>#PB_Compiler_Console 
  CompilerError "Exec-Format must be Console in Compiler-Option"
CompilerEndIf  

! int fact(int n) {
!   if(n>=1) 
!     return n*fact(n-1); 
!   else return 1; 
! }

OpenConsole()
! int number;
! printf("Enter a positive integer: ");
! scanf("%d",&number);
! printf("Factorial of %d = %d", number, fact(number));
Input()
CloseConsole()
Error ; Unresolved external symbol ‘scanf’ – referenced from
‘PureBasic.obj’,
POLINK : fatal error : 1 unresolved external(s)


With 6.04 LTS C backend this program is ok

Re: [6.10] Always error with C Backend

Posted: Wed May 22, 2024 6:19 am
by jassing
Not ideal..

Code: Select all

OpenConsole()
! printf("Enter a positive integer: ");
number=Val(Input())
! printf("Factorial of %d = %d", v_number, fact(v_number));
Input()
CloseConsole()
A lot of what is considered "standard" functions are not available in PB's C

Re: [6.10] Always error with C Backend

Posted: Wed May 22, 2024 6:51 am
by Fred
This is not a bug, using such functions without proper declaration doesn't always work.

Re: [6.10] Always error with C Backend

Posted: Wed May 22, 2024 9:22 am
by infratec
Have you ever tried to run this code

Code: Select all

int fact(int n) {
  if(n>=1) 
    return n*fact(n-1); 
  else return 1; 
}


int main(void) {

 int number;
 printf("Enter a positive integer: ");
 scanf("%d",&number);
 printf("Factorial of %d = %d", number, fact(number));
 return 0;
}
With a C compiler :?:

Tell us the error messages.

Re: [6.10] Always error with C Backend

Posted: Wed May 22, 2024 11:06 am
by juergenkulow
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04):

Code: Select all

Enter a positive integer: 5 
Factorial of 5 = 120k

Re: [6.10] Always error with C Backend

Posted: Wed May 22, 2024 10:19 pm
by jassing
Fred wrote: Wed May 22, 2024 6:51 am This is not a bug, using such functions without proper declaration doesn't always work.
Even with declaration, it's still not linked.

Code: Select all

! int __cdecl scanf(const char * restrict, ...);
! int __cdecl printf(const char * restrict, ...);
printf() links fine.