JNA stdout and the REPL

102 views
Skip to first unread message

Sonny To

unread,
Feb 14, 2020, 5:30:51 AM2/14/20
to Clojure
I am using JNA to call native C code. calling native code that outputs to stdout does not show up in the Clojure REPL. stdout is redirected somewhere that is not attached to *out*

How can I get stdout from native code outputting to *out* in the REPL? Without this, it makes it difficult to see what went wrong in the C code.

thanks,
Sonny

Sonny To

unread,
Feb 14, 2020, 5:40:48 AM2/14/20
to Clojure
here is an example of 

                                                                                                                                                                                 
package foo; 
                                                                                                                                                                                                                 
import com.sun.jna.Library;                                                                                                                                                                                        
import com.sun.jna.Native;                                                                                                                                                                                         
import com.sun.jna.Pointer;                                                                                                                                                                                        
                                                                                                                                                                                                                   
public interface C extends Library {                                                                                                                                                                               
    C INSTANCE = (C) Native.load("c",C.class);                                                                                                                                                                     
                                                                                                                                                                                                                   
    void printf(String format, Object... args);                                                                                                                                                                    
}                                                                                                                                                                                                                  
         

calling this from a java main like this works as expected at the command line
  C.INSTANCE.printf("hello\n");

but from the clojure REPL,

(.printf foo.C/INSTANCE "hello "(into-array []))  

does not show anything

Jason Felice

unread,
Feb 14, 2020, 9:52:17 AM2/14/20
to clo...@googlegroups.com
I imagine you'll have to call the Java System.out.println from C.  I forget how exactly to do this.

Java streams and ANSI/POSIX C streams aren't compatible.  The C ones usually rely on having an OS file descriptor, and Java allows making new kinds by implementing interfaces.  There's not a general way (at least not a portable one) to get a file descriptor for a user space object.



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/bd950525-defd-4696-948e-ac321dc5220e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages