Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing and Running Libraries in C
#1
Good morning everyone, 

I would like to try using the C language support to integrate certain functions that do not exist or are difficult to access in QB64.

Apart from the wiki, I have not found a proper guide that can help me understand how to do it, and the examples I have found have not been very helpful.

Can you help me with this?

Even Python, which is currently very popular, would be fantastic if it were implemented in QB64… but I realize I'm asking for too much.
Besides, I don't like Python but I have to acknowledge that it has an incredible amount of libraries.

Thank you!
Reply
#2
(12-07-2023, 11:29 AM)krovit Wrote: Good morning everyone, 

I would like to try using the C language support to integrate certain functions that do not exist or are difficult to access in QB64.
could you be more specific ? what certain functions ?
Reply
#3
At the moment, I don't have anything specific.

I would like to start understanding how it's done! Let me explain myself better: I don't know what to use to write in C (maybe DEV+), I don't know how to generate and save the C code so that it can be imported by QB64 (I see that in many examples there is code in .H files which is not compiled in C)... in short, I lack the basics.

There is a lot available in C and now there are even AI tools that can help...

All of this could greatly expand QB64's possibilities. I have developed a lot (according to my abilities, of course!), especially in terms of interface to handle data input and make the whole project appealing and professional (as much as I can). I lack knowledge in dealing with complex databases and even the less complex ones that I consider fantastic for their simplicity and power (DBF). And also dealing with texts, Word and PDF... In C, everything would be more accessible...

___
For the translation of the text into English, I relied on an AI... I hope I have made myself clear...
Reply
#4
(sigh) Programming in C is seriously not easy for a total beginner. Especially one that has only known how to do things in BASIC. Take it from me, from the 1990's after I had to give up finding work as computer engineer or programmer.

Start with this page:

https://qb64phoenix.com/qb64wiki/index.php/C_Libraries

But please ignore "Fast Math" and below, that stuff should seriously be removed.

There are many things that could be done in QB64 only with "DECLARE LIBRARY" block and a few functions from C.

Coding in Python, Julia, Lua etc. requires a virtual machine(*) that goes between BASIC and the other language. In the very least, will need the library which holds the functions of the other language, and will need at least a header file that describes how to call those functions. I could only speak surely for Lua: on Linux this library is called "liblua.o" and the header file for C is "lua.h". Will have to replicate in "DECLARE LIBRARY" block in QB64 what is expected to be used from the header file. At least half the functions of the "main" library, and a few from the "auxiliary" library. Now I'm not sure if this is apart from "liblua.o". I'm only speaking about Lua.

Otherwise you will have to become more familiar with pointers than ever. A good comprehension of what goes on with _OFFSET in QB64 would help a bit. The C compiler DOES NO RANGE CHECKING and nothing else that catches the programmer making a mistake, while QB64 in almost all cases just throws an Illegal Function Call, or Out of Memory or other error message in a nice beautiful dialog box.

I'm not saying this to discourage anybody. But doing something in C should be seriously studied, to see if it could be done entirely in a BASIC dialect such as QB64.


(*) EDIT: To clarify, in the least there has to be a "chunk" which holds the state of the interpreter that is embedded into QB64. Without that, it's not possible to communicate directly between the interpreter and the underlying C++ program that a QB64 program is eventually converted into. First there is this chunk that has to be initialized, which usually makes sure the "main" library is loaded. This could be a "dot-a" file on Linux or "dot-DLL" on Windows. Not sure about this, because "dot-a" is supposed to be a static library, but the same thing but with "dot-so" suffix is supposed to be the shared version. On Windows respectively this is "LIB" and "DLL" (dynamic-link library). Anyway, all the other functions to call, summoning the interpreter to do stuff, have to go through this "chunk" so that the interpreter could update its own state and be always ready to answer further calls from the "client".

For example with Lua, it could be tedious to have to keep track of the C stack. This is because the stack is where the helper functions expect to obtain arguments to work with, and if they return any values, they will push them through the stack. If the stack is messed up, in the very least, the program won't work properly expecting stuff from the interpreter. At worst, the program could crash because QB64 could only account for its own libraries and routines, not for an external object being introduced via "DECLARE LIBRARY".

I guess with anything that has to be done with C, must worry about a stack. If that is overrun the program crashes shamelessly because the programming language is one of the closest to assembly language: it was designed to give almost all control to the programmer without slowing it down and checking constantly where is the error, where is the overflow, why isn't this working etc.

TL;DR programming in C is not easy!
Reply
#5
A lot of what I would have used C or Assembly for years back is now native to QB64. We've got native mouse use, decent graphics support, and memory galore.  Do you want to import a file...well that's covered too. 
Two or three decades ago being good at my job required programming but I wasn't officially a programmer but I needed the skills to get access to hardware and files formats. I'd test routines in QBasic and PowerBasic before converting them over to C/C++ and I while I look back on those days with nostalgia, I also wouldn't want to do that again.

Dig into the documentation. Find some old books on QuickBasic programming that cover the full-featured compiler options they'll explain library use.
Reply
#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
#7
Much thanks to Samuel for providing a useful response!
Tread on those who tread on you

Reply
#8
Sorry if I haven't replied yet, but I have some family issues to deal with.
Nevertheless, I thank everyone for the numerous contributions I see.
See you soon!
Reply




Users browsing this thread: 1 Guest(s)