Skip to content

Odd behavior when trying to capture C functions #3

@mtolly

Description

@mtolly

cfunc.c:

#include <stdio.h>

void cFunc() {
  printf("from C\n");
}

Main.hs:

module Main where

import System.IO.Silently

foreign import ccall unsafe "cFunc"
  cFunc :: IO ()

main :: IO ()
main = do
  capture_ (putStrLn "from haskell") >>= print
  capture_ cFunc >>= print

On my system (macOS 10.12.5, GHC 8.0.2, silently 1.2.5) this produces the output:

"from haskell\n"
""
from C

So I figured that was that, silently only works for Haskell code. Not sure why the C printf comes after the Haskell print but chalked it up to buffering weirdness. But then I modified main to look like this:

main :: IO ()
main = do
  capture_ (putStrLn "from haskell") >>= print
  cFunc
  capture_ cFunc >>= print

And that produces this output:

"from haskell\n"
from C
"from C\n"

So if I have C code print at least once to stdout, then thereafter it can be captured by silently...? I don't understand why at all. Any insight?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions