dave
New Member
Posts: 5
|
Post by dave on Jul 6, 2023 17:12:31 GMT 1
Hi all, Since this is a question about how to use "PRAGMA INCLUDE" and "PROTO" in Bacon, I hope that I posted correctly in the Documentation section. Does anyone have any links to information on what those two directives do? Here is an example.... On this thread basic-converter.proboards.com/thread/191/parallel-port-control bigbass posted a pgm to write to the parallel port (about half way down the page). He used those two directives and included sys/io.h So why the need to use "PRAGMA INCLUDE" and "PROTO" ? BTW, I see <sys/io.h> also has "inb" and "inw" (byte and word) input from parallel port. Thanks.
|
|
|
Post by Pjot on Jul 7, 2023 8:14:53 GMT 1
Hi dave, When using BaCon, we must realize that there are multiple stages in the process of creating a binary. ConversionDuring this stage, BaCon will try to convert the BASIC statements to C code. However, it can only recognize its predefined set of statements and functions. These are described in the documentation. So, if BaCon encounters a statement or function name which is unknown, it will simply stop the process of conversion and generate an error message. But sometimes however, we really would like to use a function from an external C library. To make sure BaCon also understands such an external function name, we can use PROTO. CompilationWhen BaCon has completed the conversion process, it will start the actual compilation of the generated C code. This should create the binary executable for your program. However, the C compiler also needs to know where to fetch the information for an external function. Otherwise it cannot create the binary. The keyword PRAGMA can define where to look for those definitions, and also which library needs to be used when linking the program. HTH Peter
|
|
dave
New Member
Posts: 5
|
Post by dave on Jul 7, 2023 22:49:20 GMT 1
Thank you Peter.
So if I understand you correctly,
PRAGMA INCLUDE sys/io.h Tells Bacon it needs to "include" some or all of the functions in io.h Then,
PROTO outb PROTO ioperm
Tells Bacon which functions it must include in the conversion and also tells GCC which to compile. I'm assuming that those functions listed after PROTO will be statically compiled (included in the final executable). So it's treating io.h just like it would any other "external" Bacon function/s that we may have as separate .bac files.
This then, is also different to importing/using a function in an already compiled shared lib (.so) which will be dynamically linked at run time?
Thanks, I just need to get my head around this.
|
|
|
Post by Pjot on Jul 8, 2023 8:17:21 GMT 1
So if I understand you correctly, PRAGMA INCLUDE sys/io.h Tells Bacon it needs to "include" some or all of the functions in io.h Correct. Then, PROTO outb PROTO ioperm Tells Bacon which functions it must include in the conversion and also tells GCC which to compile. Not quite. The PROTO statement is solely meant for the parser to not choke on non-BaCon keywords. So, keywords mentioned by PROTO are being "skipped" in the parsing process. The PROTO statement does not mention anything to GCC. I'm assuming that those functions listed after PROTO will be statically compiled (included in the final executable). No. By default, GCC tries to link dynamically. The resulting binary then will only work if those external libraries are available on your system. You can instruct GCC to link statically by passing the "-static" compile flag to GCC, e.g.: # bacon -l -static <your_program.bac>
This then, is also different to importing/using a function in an already compiled shared lib (.so) which will be dynamically linked at run time? Yes it is different, but no, IMPORT does not link to an external library. The IMPORT statement uses the dlopen API from libc to "import" external functions. It does so during runtime. So only when your program starts running, IMPORT will try to find the external library to see if it can "import" an external function. It also will announce that external keyword to the BaCon parser (similar as PROTO). There is always a lot of misunderstanding when it comes to distinguishing the different stages in BaCon. In fact, there are three stages, two of which are mentioned in my previous reply. - conversion time - PROTO is active here
- compile time - PRAGMA is active here
- run time - IMPORT is active here
For more information on these stages, see the Notes on transcompiling in the documentation. HTH Peter
|
|
dave
New Member
Posts: 5
|
Post by dave on Jul 8, 2023 9:28:41 GMT 1
Many thanks for the detailed explanation, it has certainly made things much clearer now. I didn't know that GCC will by default use dynamic linking which can be overridden with the -static option. That then gives one the option of having a smaller executable but dependent on the system having the lib available, versus static linking which reduces dependencies but results in a bigger file.
Going to read up more via the links you provided.
|
|