English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. C/C++ (https://eab.abime.net/forumdisplay.php?f=118)
-   -   Using libraries and header files (https://eab.abime.net/showthread.php?t=107393)

guy lateur 19 June 2021 19:24

Using libraries and header files
 
Lately I've been taking my first steps in some C development for classic Amiga systems. I was looking for support for regular expressions, and I found this library on Aminet: https://aminet.net/package/util/libs/regex_ranieri I'm using SAS/C for now, so I'll focus on that (might switch to vbcc later, though). The archive has an include folder specifically for SAS/C, containing 4 header files. It's not 100% clear to me what every file does or represents, so I'll present here what I think is going on. It's been more than 20 years since I did any development in C, so please bear with me.. :D

The first file is
proto/regex.h
. This file (in)directly includes all other files, so I'm assuming this is the file I should include in my code. This file declares this:
extern struct Library *RegexBase
. I've seen similar declarations in other libraries, so I guess this must be a standard way of doing things. However, I've never found where this struct actually gets initialised. Is there some code added by the compiler/linker that does this? I feel like there's a bit of black magic going on here.

The file
proto/regex.h
includes the second file,
pragmas/regex-pragmas.h
. This file contains several directives like this one:
#pragma libcall RegexBase regcomp 24 09803
. I'm assuming these connect the C versions of the functions (eg, regcomp) to an entry point/offset in the library. It needs the aforementioned RegexBase for that, which would make sense.

The file
proto/regex.h
also includes the third file,
clib/regex-protos.h
. This contains the regular C function prototypes, like
int regcomp(regex_t *, const char *, int)
. Just like any other prototypes, these are needed for compilation, so it makes sense they have to be in there somewhere.

The third file includes the fourth and last header file,
libraries/regex.h
, which contains some struct definitions like
regex_t
and
regmatch_t
. It also defines some flags for compiling and executing the regular expressions, and some error codes. Makes sense.

Finally, there's a file named
fd/regex_lib.fd
. It contains stuff like
##base _RegexBase
and
##bias 30
. It also contains lines like this:
regcomp(preg,pattern,cflags)(a0/a1,d0)
, which tells which C arguments correspond to which registers. I understand this is needed to be able to call the routines in the library, but I'm not sure how this info is used or when this gets invoked.

So I guess my main question is how this RegexBase thing works. And this fd thing. Also, if anything I said above is wrong or incomplete, please let me know. Like I said earlier, most of what I find in this library, I also come across in other contexts, so it would be helpful if I could get a clear picture of what exactly is going on and why. If you know of any places where I can read up on this subject, please let me know. TIA for any input!

Thomas Richter 19 June 2021 22:25

Quote:

Originally Posted by guy lateur (Post 1491479)
However, I've never found where this struct actually gets initialised. Is there some code added by the compiler/linker that does this? I feel like there's a bit of black magic going on here.


Typically, it is up to your code to initialize library bases, i.e.
Code:

RegexBase = OpenLibrary("regexp.library",version);
followed by a check whether the library open actually worked, and at the end of the program, a matching


Code:

CloseLibrary(RegexBase);

The library name you find in the documentation, and the name of the library base you find in the "proto" file.


For most standard system libraries, some compilers will create automatically code that opens the corresponding libraries if you don't do it yourself. Thus, you will typically get away by *not* having opened "dos.library" because the compiler startup code will do that for you.


As said, this is compiler specific, and it is certainly good practise to open the libraries you need yourself.

phx 20 June 2021 14:50

Some additional comments:

You will never include a compiler-specific pragmas/ or inline/ file.
proto/regex.h
will include all you need and keeps your source easily portable between different compilers. Also the prototypes in
clib/regex_protos.h
are not needed, as
proto/regex.h
takes care of that as well.

The FD file is only needed if you want to build header files for a currently unsupported compiler.

guy lateur 20 June 2021 21:16

Quote:

Originally Posted by Thomas Richter (Post 1491507)
..

Quote:

Originally Posted by phx (Post 1491586)
..

Thank you both for your input, this has certainly cleared up a few things! :great


All times are GMT +2. The time now is 23:48.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.04797 seconds with 11 queries