unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Guile dynamic FFI, C function expecting pointer
@ 2020-11-22 22:50 Tim Meehan
  2020-11-23  0:16 ` Matt Wette
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Meehan @ 2020-11-22 22:50 UTC (permalink / raw)
  To: guile-user

I tried to boil this question down to the most simple thing that
represented what I needed to understand. I have had luck getting C
functions that expect arguments "by value," but "by reference" has been
problematic.

The failure mode is "Segmentation Fault," so I gather that I may not be
using the right Guile call at all.

The Guile user manual is usually quite excellent, but I seem to be missing
something important.

Thanks,

;;----------------------------------------------------------------------------;;
;; C source for "libstuff.so":
;; file stuff.c, compiled as:
;; gcc stuff.c -o libstuff.so -fPIC -shared
#|
void int_ptr_example1(int *a) {
    *a = 5;
}
|#

;;----------------------------------------------------------------------------;;
;; Test loading and using the library.
(use-modules (system foreign))

(define libstuff (dynamic-link "./libstuff.so"))

(define int-ptr-example1
    (pointer->procedure
        void
        (dynamic-func "int_ptr_example1" libstuff)
        (list '*)))

;; Following:
;;
https://nalaginrut.com/archives/2015/03/27/do-some-quick-and-dirty-with-guile-ffi
(let ([a %null-pointer])
    (int-ptr-example1 a)
    (display a)
    (newline))

;;----------------------------------------------------------------------------;;
;; Sadly, when it runs, I get a segmentation fault at the call to
;; int-ptr-example1 :(


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Guile dynamic FFI, C function expecting pointer
  2020-11-22 22:50 Guile dynamic FFI, C function expecting pointer Tim Meehan
@ 2020-11-23  0:16 ` Matt Wette
  0 siblings, 0 replies; 2+ messages in thread
From: Matt Wette @ 2020-11-23  0:16 UTC (permalink / raw)
  To: guile-user



On 11/22/20 2:50 PM, Tim Meehan wrote:
> I tried to boil this question down to the most simple thing that
> represented what I needed to understand. I have had luck getting C
> functions that expect arguments "by value," but "by reference" has been
> problematic.
>
> The failure mode is "Segmentation Fault," so I gather that I may not be
> using the right Guile call at all.
>
> The Guile user manual is usually quite excellent, but I seem to be missing
> something important.
>
> Thanks,
>
> ;;----------------------------------------------------------------------------;;
> ;; C source for "libstuff.so":
> ;; file stuff.c, compiled as:
> ;; gcc stuff.c -o libstuff.so -fPIC -shared
> #|
> void int_ptr_example1(int *a) {
>      *a = 5;
> }
> |#
You'll need to make-bytevector a bytevector that holds sizeof(int) bytes.
Then pass (bytevector->pointer <obj>) as the argument.

(let ((obj (make-bytevector (sizeof int))))
   (int-ptr-example (bytevector->pointer obj)))

Now the 5 should be in the bytevector.  You will need to extract it.






^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-23  0:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-22 22:50 Guile dynamic FFI, C function expecting pointer Tim Meehan
2020-11-23  0:16 ` Matt Wette

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).