QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Kernelpanic on November 16, 2020, 06:10:02 pm

Title: QB64 calls C-file
Post by: Kernelpanic on November 16, 2020, 06:10:02 pm
I'm trying to call a C file from Basic, but it won't work. It worked with QuickBasic 4.02 and QuickC 2.5. It works, but I don't know if it works with QB64. Maybe it is a problem with the GCC.
Does anyone know why the following example doesn't work - I don't get an error message. Thank you!

Code: QB64: [Select]
  1. 'Calls a function in a C-Program
  2.  
  3. DECLARE addieren% CDECL (Byval N as Integer)
  4.  
  5.  
  6. LOCATE 3, 3
  7. INPUT "Zahl eingeben: ", N
  8. LOCATE 5, 3
  9. PRINT USING "## mit 2 addiert = ###"; N; addieren%(N%)
  10.  
  11.  

The C-Program
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int addieren(int n)
{
return(n * 2);
}

int main(void)
{}
Title: Re: QB64 calls C-file
Post by: FellippeHeitor on November 16, 2020, 06:52:30 pm
We use DECLARE LIBRARY to work with external C code now, please read here: http://www.qb64.org/wiki/DECLARE_LIBRARY
Title: Re: QB64 calls C-file
Post by: RhoSigma on November 16, 2020, 07:06:50 pm
Not tested, but I'm pretty sure it works:
Code: QB64: [Select]
  1.     'Calls a function in a C-Program
  2.      
  3.     DECLARE LIBRARY "MyCHeaderFile" 'dont add .h here
  4.         FUNCTION addieren% (BYVAL N AS INTEGER)
  5.     END DECLARE
  6.      
  7.     DIM N AS INTEGER
  8.      
  9.     CLS
  10.     LOCATE 3, 3
  11.     INPUT "Zahl eingeben: ", N
  12.     LOCATE 5, 3
  13.     PRINT USING "## mit 2 addiert = ###"; N; addieren%(N%)
  14.      
  15.     END
  16.      

Save your C stuff as .h file, also remove the main() function. You may pobably remove the #include lines too, as these are already included by QB64 automatically.

For more examples see my Libraries collection, especially the QB-StdLibs, DES56, MD5 and SHA2 folders. These libraries make extensive use of C code.

PS - Both files (.bas and .h) in the qb64 main folder, or otherwise you've to add a relative path to the DECLARE LIBRARY line.
Title: Re: QB64 calls C-file
Post by: Kernelpanic on November 17, 2020, 09:01:46 am
Thanks FellippeHeitor & RhoSigma, now it works. It works with and without "#include ...".
I have a lot of C programs from before, mostly financial math, pretty complicated. Today I have difficulty understanding that again. That was also my largest program in C, about 12,000 to 13,000 lines using the QuickC graphics library. Let's see if I can wake that up again under qb64.

An example:
Code: QB64: [Select]
  1. 'Call a C-Function, 17. Nov. 2020
  2. 'Returns the Fibonacci number
  3.  
  4. DECLARE LIBRARY "D:\Lab\QuickBasic64\Extern\fibonacci"
  5.   FUNCTION fibo& (BYVAL N AS INTEGER)
  6.  
  7.  
  8. LOCATE 3, 3
  9. INPUT "Berechnet die Fibonacci-Zahl von: ", N
  10. LOCATE 5, 3
  11. PRINT USING "Die Fibonacci-Zahl von ## = ####"; N; fibo&(N%)
  12.  
  13.  

The C-Program:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

long fibo(int n)
{
long x, y;
int i;

x = y = i = 1;
if (n == 0 || n == 1)
{
return(1);
}
else while (i < n)
{
x = x + y; y = x - y; i++;
}
return(x);
}
Title: Re: QB64 calls C-file
Post by: Kernelpanic on November 17, 2020, 04:57:34 pm
For the team:
I think I found a little bug. I keep testing Basic & C and than I encountered  something. If a variable was not declared in the C file, QB64 correctly gives an error message, but afterwards the editor is kind of frozen. It no longer reacts to pressing F5. You have to do something first, like delete a letter, then it comes off again - it's as if the editor were stuck on an edge. Funny!

Title: Re: QB64 calls C-file
Post by: Kernelpanic on November 20, 2020, 10:52:45 am
I noticed a similar mistake. Is that of interest, and if so, where should I describe it, here or under bugs?