Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing and Running Libraries in C
#6
Basically, the idea is to create a ".h" file and write C / C++ functions in them that are QB64 friendly.
See:
DECLARE LIBRARY - QB64 Phoenix Edition Wiki
C Libraries - QB64 Phoenix Edition Wiki

Here is a short example that should print "1".

mybasapp.bas
Code: (Select All)
OPTION _EXPLICIT

DECLARE LIBRARY "myclib"
    FUNCTION bar& (BYVAL a AS LONG, BYVAL b AS LONG)
END DECLARE

PRINT foo(325, 2)

END

FUNCTION foo& (a AS LONG, b AS LONG)
    foo = bar(a, b)
END FUNCTION

myclib.h
Code: (Select All)
#pragma once

int bar(int a, int b)
{
    return a % b;
}

There are many rules. However, a few of them are as follows:
  • Ensure the C/C++ library file has the extension ".h" and do not specify the extension in the DECLARE LIBRARY line.
  • Ensure the C header library is in the same directory as your ".bas" source or in a relative directory as specified in the DECLARE LIBRARY line.
  • Be careful about when to pass arguments to C functions by value or by reference.
  • Be careful about the data types that are used (the links above have a comparison table)

I generally do a lot of mixed-language programming.
See a740g/Toolbox64: A740g's collection of libraries for QB64-PE (github.com) for some examples, and a740g/raylib-64: QB64-PE bindings for raylib (a simple and easy-to-use library to learn videogames programming) (github.com) for something a little more advanced.
Reply


Messages In This Thread
Importing and Running Libraries in C - by krovit - 12-07-2023, 11:29 AM
RE: Importing and Running Libraries in C - by a740g - 12-07-2023, 01:31 PM



Users browsing this thread: 1 Guest(s)