Question

I have the following snippet of Scheme code for Chicken:

(require-extension bind)

(bind* "double int_exp(double, int);")
(bind* "double square(double);")

(begin
    (print (int_exp 1.2 1))
    (print (square 2.0)))

int_exp and square are just small test functions (written in C) that I made up for testing. This code works fine; however, as soon as I remove the asterisks, the compiled program prints bogus values (and the bogus values depend on whether I compiled the C code using clang or gcc.) The bind documentation simply says "[bind* is] similar to bind, but also embeds the code in the generated Scheme expansion using foreign-declare" and "[foreign-declare includes] given strings verbatim into header of generated file" - neither is particularly helpful given that I'm new to Chicken (and indeed Scheme.) What do they actually mean, what are the differences between bind and bind* and when should I use either?

Was it helpful?

Solution

From what I can tell, you use bind if the function you're calling has already been declared in a header file you've previously #included (including anything automatically included by chicken.h, which all Chicken programs include). If the function you're calling is not already declared, then you need to use bind* to emit a declaration also.

So, this would work (math.h is already included by chicken.h):

(use bind extras)
(bind "double cbrt(double)")
(format #t "cbrt(~a) = ~a~%" 27 (cbrt 27))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top