[6.10] Always error with C Backend

Just starting out? Need help? Post your questions and find answers here.
al2791
User
User
Posts: 20
Joined: Mon Oct 23, 2017 7:28 am

[6.10] Always error with C Backend

Post 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
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: [6.10] Always error with C Backend

Post 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
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [6.10] Always error with C Backend

Post by Fred »

This is not a bug, using such functions without proper declaration doesn't always work.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [6.10] Always error with C Backend

Post 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.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: [6.10] Always error with C Backend

Post 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
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: [6.10] Always error with C Backend

Post 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.
Post Reply