C Standard Library
Base.Libc.malloc
— Functionmalloc(size::Integer) -> Ptr{Cvoid}
从 C 标准库调用 malloc
。
Base.Libc.calloc
— Functioncalloc(num::Integer, size::Integer) -> Ptr{Cvoid}
从 C 标准库调用 calloc
。
Base.Libc.realloc
— Functionrealloc(addr::Ptr, size::Integer) -> Ptr{Cvoid}
从 C 标准库调用 realloc
。
Base.memcpy
— Functionmemcpy(dst::Ptr, src::Ptr, n::Integer) -> Ptr{Cvoid}
从 C 标准库调用 memcpy
。
对 memcpy
的支持至少需要 Julia 1.10。
Base.memmove
— Functionmemmove(dst::Ptr, src::Ptr, n::Integer) -> Ptr{Cvoid}
从 C 标准库调用 memmove
。
对 memmove
的支持至少需要 Julia 1.10。
Base.memset
— Functionmemset(dst::Ptr, val, n::Integer) -> Ptr{Cvoid}
调用来自 C 标准库的 memset
。
对 memset
的支持至少需要 Julia 1.10。
Base.memcmp
— Functionmemcmp(a::Ptr, b::Ptr, n::Integer) -> Int
调用 C 标准库中的 memcmp
。
对 memcmp
的支持至少需要 Julia 1.9。
Base.Libc.free
— Functionfree(addr::Ptr)
调用来自 C 标准库的 free
。仅在从 malloc
获取的内存上使用此函数,而不是在从其他 C 库检索的指针上使用。来自 C 库的 Ptr
对象应由该库中定义的 free 函数释放,以避免在系统上存在多个 libc
库时出现断言失败。
Base.Libc.errno
— Functionerrno([code])
获取 C 库的 errno
值。如果指定了参数,则用于设置 errno
的值。
errno
的值仅在对设置它的 C 库例程的 ccall
之后立即有效。具体来说,您不能在 REPL 的下一个提示符中调用 errno
,因为在提示符之间执行了大量代码。
Base.Libc.strerror
— Functionstrerror(n=errno())
将系统调用错误代码转换为描述性字符串
Base.Libc.GetLastError
— FunctionGetLastError()
调用 Win32 GetLastError
函数 [仅在 Windows 上可用]。
Base.Libc.FormatMessage
— FunctionFormatMessage(n=GetLastError())
将 Win32 系统调用错误代码转换为描述性字符串 [仅在 Windows 上可用]。
Base.Libc.time
— Methodtime(t::TmStruct) -> Float64
将 TmStruct
结构转换为自纪元以来的秒数。
Base.Libc.strftime
— Functionstrftime([format], time)
将时间(以自纪元以来的秒数或 TmStruct
给出)转换为使用给定格式的格式化字符串。支持的格式与标准 C 库中的格式相同。
Base.Libc.strptime
— Functionstrptime([format], timestr)
将格式化的时间字符串解析为 TmStruct
,提供秒、分钟、小时、日期等信息。支持的格式与标准 C 库中的格式相同。在某些平台上,时区可能无法正确解析。如果此函数的结果将传递给 time
以转换为自纪元以来的秒数,则 isdst
字段应手动填写。将其设置为 -1
将告诉 C 库使用当前系统设置来确定时区。
Base.Libc.TmStruct
— TypeTmStruct([秒])
将自纪元以来的秒数转换为分解格式,字段包括 sec
、min
、hour
、mday
、month
、year
、wday
、yday
和 isdst
。
Base.Libc.FILE
— TypeFILE(::Ptr)
FILE(::IO)
一个 libc FILE*
,表示一个已打开的文件。
它可以作为 Ptr{FILE}
参数传递给 ccall
,并且支持 seek
、position
和 close
。
可以从普通的 IO
对象构造一个 FILE
,前提是它是一个已打开的文件。之后必须关闭它。
示例
julia> using Base.Libc
julia> mktemp() do _, io
# 使用 libc 中的 `puts(char*, FILE*)` 写入临时文件
file = FILE(io)
ccall(:fputs, Cint, (Cstring, Ptr{FILE}), "hello world", file)
close(file)
# 再次读取文件
seek(io, 0)
read(io, String)
end
"hello world"
Base.Libc.flush_cstdio
— Functionflush_cstdio()
刷新 C 的 stdout
和 stderr
流(这些流可能已被外部 C 代码写入)。
Base.Libc.systemsleep
— FunctionBase.Libc.mkfifo
— Functionmkfifo(path::AbstractString, [mode::Integer]) -> path
在 path
处创建一个 FIFO 特殊文件(命名管道)。成功时返回 path
原样。
mkfifo
仅在 Unix 平台上受支持。
mkfifo
至少需要 Julia 1.11。