How to share file with C?

My program contains some libraries written in C, and both the Julia part and the C part will write to the same log file. How can I share the file between Julia and C?
I didn’t find a way to use FILE* in Julia. The file descriptor can be used by open(fd::OS_HANDLE) -> IO, but the document claims that Do not call this on a handle that's already owned by some other part of the system, so I suspect it cannot be used either?

The easiest solution is not to share a file handle. Either write a function to write to log in C and call that function from Julia, or do it the other way round.

1 Like

The docstring also says “Call open(Libc.dup(fd)) to avoid the ownership capture of the original handle” so try that?

Did you see Libc.FILE?

That should do exactly what you want. Open the file io = open(...) on the Julia side, and pass Libc.File(io) as a FILE* to the C side so that C can use it for writing. Close it on the Julia side when you are done.

1 Like