Accessing C constants from Racket

The scenario: Trying to use the Racket FFI to call a C functions that have arguments whose values should be taken from one or more #define'd constants, that might be different numbers on different OSes, making just hard-coding the values in Racket out of the question.

What's the best way of getting at these values? I found a c-defs package that looks promising (It basically compiles and runs a small C program that prints out the values of macros you want to get) but is there a way to automatically compile and run a script using it (To generate a Racket source file with all the values defined ahead of time that can be reused in that particular installation) when doing a raco install instead of compiling a C program every time my module is loaded?

Or if I make it an extension written in C that provides an interface directly instead of using the FFI, how do you make raco automate compiling a shared object? (Not looking forward to this idea as the documentation for writing extensions against the CS backend seems very scant; after skimming the Inside: Racket C API document I don't even see how to define a new variable or procedure from C)

Or is there some better way I'm overlooking?

1 Like

Finally figured out how to make raco setup run code at compile time -

(define pre-install-collection "foo.rkt")

in info.rkt will call a function named pre-installer in that file/module.

Still curious on if there's a better way.

1 Like

The best way would be to convince the authours of the C library to
add a function from the defined name to its value.

An alternative is to generate an alternative representation of the constants.
In Raylib there is a parser for .h files that generates json:

Oh. I forgot your requirement: "that might be different numbers on different OSes, making just hard-coding the values in Racket out of the question." In that case the Raylib approach doesn't work.

Yes, I'll get on the POSIX committee about that...

EDIT: But speaking about C constants that have different values on different systems, it's frustrating that unsafe/ffi has a function to map an errno symbolic value like EAGAIN to a number, but not the other way around, or a way to export all known symbols...

Yes, I'll get on the POSIX committee about that...

If it is an existing POSIX library, maybe someone else aready has implemented
some sort of reflection?

Not that I can find.

Context: I'm working on a Racket version of SRFI-170: POSIX API for my extra-srfi-libs collection (Though I might spin it out into its own package because it doesn't make much sense to have on Windows)

Unfortunately, AFAIK, there is no portable equivalent to glibc's strerrorname_np. Probably it would be reasonably possible to do what you suggest for at least for what lookup-errno supports, but note that lookup-errno on Racket CS relies on hard-coded data.

If you want something fully robust, you probably have to compile a little C library like the unix-signals package does: