當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Julia @ccall用法及代碼示例

用法:

@ccall library.function_name(argvalue1::argtype1, ...)::returntype
@ccall function_name(argvalue1::argtype1, ...)::returntype
@ccall $function_pointer(argvalue1::argtype1, ...)::returntype

調用 C-exported 共享庫中的函數,由 library.function_name 指定,其中 library 是字符串常量或文字。該庫可以省略,在這種情況下,function_name 在當前進程中被解析。或者,@ccall 也可用於調用函數指針 $function_pointer ,例如由 dlsym 返回的函數指針。

通過自動插入對 unsafe_convert(argtype, cconvert(argtype, argvalue)) 的調用,每個 argvalue@ccall 都將轉換為相應的 argtype 。 (有關更多詳細信息,另請參閱 unsafe_convert cconvert 的文檔。)在大多數情況下,這隻會導致調用 convert(argtype, argvalue)

例子

@ccall strlen(s::Cstring)::Csize_t

這將調用 C 標準庫函數:

size_t strlen(char *)

使用名為 s 的 Julia 變量。另見ccall

以下約定支持可變參數:

@ccall printf("%s = %d"::Cstring ; "foo"::Cstring, foo::Cint)::Cint

分號用於將必需的參數(其中必須至少有一個)與可變參數分開。

使用外部庫的示例:

# C signature of g_uri_escape_string:
# char *g_uri_escape_string(const char *unescaped, const char *reserved_chars_allowed, gboolean allow_utf8);

const glib = "libglib-2.0"
@ccall glib.g_uri_escape_string(my_uri::Cstring, ":/"::Cstring, true::Cint)::Cstring

如果需要,也可以在函數名之前直接使用字符串文字 "libglib-2.0".g_uri_escape_string(...

相關用法


注:本文由純淨天空篩選整理自julialang.org 大神的英文原創作品 Base.@ccall — Macro。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。