使用ccall重定向Julia对C函数的调用产生的标准输出

使用ccall重定向Julia对C函数的调用产生的标准输出,julia,Julia,我正在为C/C++库制作Julia包装器。我包装的C/C++函数将写入标准输出。有没有一种方法可以在不注释/删除C/C++代码中的write语句的情况下从Julia端重定向这些消息?您可以使用redirect\u stdout oldstd = stdout redirect_stdout(somewhere_else) ccall(:printf, Cint, (Cstring,), "Hello World!") Base.Libc.flush_cstdio() # it might be

我正在为C/C++库制作Julia包装器。我包装的C/C++函数将写入标准输出。有没有一种方法可以在不注释/删除C/C++代码中的write语句的情况下从Julia端重定向这些消息?

您可以使用
redirect\u stdout

oldstd = stdout
redirect_stdout(somewhere_else)
ccall(:printf, Cint, (Cstring,), "Hello World!")
Base.Libc.flush_cstdio() # it might be necessary to flush C stdio to maintain the correct order of outputs or forcing a flush
redirect_stdout(oldstd) # recover original stdout
您可能希望改用
重定向\u stdout(f::Function,stream)
方法。在这里,
f
应该是一个不带参数的函数(例如,
()->do_something(…)
)。此方法自动将流恢复到
stdout
。使用
do
语法

redirect_stdout(somewhere) do
    ccall(:printf, Cint, (Cstring,), "Hello World!")
    Base.Libc.flush_cstdio() # might be needed
end

您可以使用JuliaIO的
抑制器。@suppress
宏:他们的宏使用下面的流重定向。您能否提供一个简单的代码示例,说明如何进行包装?