# Accessing C constants from Racket

**URL:** <https://racket.discourse.group/t/accessing-c-constants-from-racket/1933>\
**Category:** Questions & Answers\
**Created:** [May 9, 2023, 4:12pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933 "2023-05-09T16:12:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [May 9, 2023, 4:12pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/1 "2023-05-09T16:12:41Z")

</div>

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`](https://docs.racket-lang.org/c-defs/index.html) 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?

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [May 9, 2023, 7:43pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/2 "2023-05-09T19:43:46Z")

</div>

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

```scheme
(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.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 10, 2023, 11:54am UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/3 "2023-05-10T11:54:57Z")

</div>

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:

> **[raylib/parser at master · raysan5/raylib](https://github.com/raysan5/raylib/tree/master/parser)**
>
> master/parser

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.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [May 10, 2023, 4:10pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/4 "2023-05-10T16:10:43Z")

</div>

> [@soegaard](#):
>
> The best way would be to convince the authours of the C library to  
> add a function from the defined name to its value.

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...

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 10, 2023, 4:44pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/5 "2023-05-10T16:44:09Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [May 10, 2023, 4:52pm UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/6 "2023-05-10T16:52:59Z")

</div>

Not that I can find.

Context: I'm working on a Racket version of [SRFI-170: POSIX API](https://srfi.schemers.org/srfi-170/srfi-170.html) for my [`extra-srfi-libs` collection](https://docs.racket-lang.org/extra-srfi-libs/index.html) (Though I might spin it out into its own package because it doesn't make much sense to have on Windows)

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [May 21, 2023, 6:53am UTC](https://racket.discourse.group/t/accessing-c-constants-from-racket/1933/7 "2023-05-21T06:53:49Z")

</div>

> [@shawnw](#):
>
> 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...

Unfortunately, AFAIK, there is no portable equivalent to glibc's [`strerrorname_np`](https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html#index-strerrorname_005fnp). 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](https://github.com/racket/racket/blob/0abb98f922030b222aa36756e40ce3d109c18da1/racket/src/cs/rumble/errno-data.ss#L1).

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

> <https://github.com/tonyg/racket-unix-signals/blob/d51fb7eb7a36c10369fbcbde09f11af66f121870/unix-signals/private/racket_unix_signals.c#L80-L89>
