Author Topic: QB64 calls C-file  (Read 3304 times)

0 Members and 1 Guest are viewing this topic.

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
QB64 calls C-file
« 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)
{}
« Last Edit: November 16, 2020, 06:12:41 pm by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

FellippeHeitor

  • Guest
Re: QB64 calls C-file
« Reply #1 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

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: QB64 calls C-file
« Reply #2 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: QB64 calls C-file
« Reply #3 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);
}
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: QB64 calls C-file
« Reply #4 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!

Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: QB64 calls C-file
« Reply #5 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?
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“