调用 C 和 Fortran 代码

    被调用的代码必须是一个共享库(.so, .dylib, .dll)。大多数 C 和 Fortran 库都已经是以共享库的形式发布的,但在用 GCC 或 Clang 编译自己的代码时,需要添加 -shared-fPIC 编译器选项。由于 Julia 的 JIT 生成的机器码跟原生 C 代码的调用是一样,所以在 Julia 里调用 C/Fortran 库的额外开销与直接从 C 里调用是一样的。在 C 和 Julia 中的非库函数调用都能被内联,因此可能会比调用标准库函数开销更少。当库与可执行文件都由 LLVM 生成时,对程序整体进行优化从而跨越这一限制是可能的,但是 Julia 现在还不支持这种优化。不过在未来,Julia 可能会支持,从而获得更大的性能提升。

    我们可以通过 (:function, "library")("function", "library") 这两种形式来索引库中的函数,其中 function 是函数名,library 是库名。对于不同的平台/操作系统,库的载入路径可能会不同,如果库在默认载入路径中,则可以直接将 library 设为库名,否则,需要将其设为一个完整的路径。

    可以单独使用函数名来代替元组(只用 :function"function")。在这种情况下,函数名在当前进程中进行解析。这一调用形式可用于调用 C 库函数、Julia 运行时中的函数或链接到 Julia 的应用程序中的函数。

    默认情况下,Fortran 编译器会(例如,将函数名转换为小写或大写,通常会添加下划线),要通过 ccall 调用 Fortran 函数,传递的标识符必须与 Fortran 编译器名称修饰之后的一致。此外,在调用 Fortran 函数时,所有输入必须以指针形式传递,并已在堆或栈上分配内存。这不仅适用于通常是堆分配的数组及可变对象,而且适用于整数和浮点数等标量值,尽管这些值通常是栈分配的,且在使用 C 或 Julia 调用约定时通常是通过寄存器传递的。

    最终,你能使用 来实际生成一个对库函数的调用。ccall 的参数如下:

    • 一个 (:function, "library") 元组,必须为常数字面量的形式,

    a :function name symbol or "function" name string, which is resolved in the 当前进程,

    一个函数指针(例如,从 dlsym 获得的指针)。

    • 返回类型(参见下文,将声明的 C 类型对应到 Julia)

      • 当包含的函数已经定义时,参数将会在编译期执行。
    • 输入类型的元组。元组中的类型必须为字面量,而不能是变量或者表达式。

      • 当包含的函数已经定义时,参数将会在编译期执行。
    • 紧接着的参数,如果有的话,将会以参数的实际值传递给函数。

    举一个完整而简单的例子:从 C 的标准库函数中调用 clock 函数。

    clock 不接收任何参数,它会返回一个类型为 的值。一个常见的问题是必须要用尾随的逗号来写一个单元组。例如,要通过 getenv 函数来获取一个指向环境变量值的指针,可以像这样调用:

    1. julia> path = ccall((:getenv, "libc"), Cstring, (Cstring,), "SHELL")
    2. Cstring(@0x00007fff5fbffc45)
    3. julia> unsafe_string(path)
    4. "/bin/bash"

    请注意,参数类型元组必须是 (Cstring,),而不是 (Cstring) 。这是因为 (Cstring) 只是括号括起来的表达式 Cstring,而不是包含 Cstring 的单元组:

    1. julia> (Cstring)
    2. Cstring
    3. julia> (Cstring,)
    4. (Cstring,)

    在实践中,特别是在提供可重用功能时,通常会将 ccall 封装成一个 Julia 函数,此函数负责为 配置参数,且无论 C 或 Fortran 函数以任何方式产生错误,此函数都会对其进行检查并将异常传递给调用者。这一点尤其重要,因为 C 和 Fortran 的 API 在出错时的表现形式和行为极其不一致。例如,C 库函数 getenv 在 Julia 中的封装可以在 env.jl 里找到,该封装的一个简化版本如下:

    1. function getenv(var::AbstractString)
    2. val = ccall((:getenv, "libc"),
    3. Cstring, (Cstring,), var)
    4. if val == C_NULL
    5. error("getenv: undefined variable: ", var)
    6. end
    7. unsafe_string(val)
    8. end

    C 函数 getenv 通过返回 NULL 的方式进行报错,但是其他 C 标准库函数也会通过多种不同的方式来报错,这包括返回 -1,0,1 以及其它特殊值。此封装能够明确地抛出异常信息,即是否调用者在尝试获取一个不存在的环境变量:

    1. julia> getenv("SHELL")
    2. "/bin/bash"
    3. julia> getenv("FOOBAR")
    4. getenv: undefined variable: FOOBAR

    这有一个稍微复杂一点的例子,功能是发现本机的主机名:

    1. function gethostname()
    2. hostname = Vector{UInt8}(undef, 128)
    3. ccall((:gethostname, "libc"), Int32,
    4. (Ptr{UInt8}, Csize_t),
    5. hostname, sizeof(hostname))
    6. hostname[end] = 0; # ensure null-termination
    7. return unsafe_string(pointer(hostname))
    8. end

    这个例子首先分配一个由bytes组成的数组,然后调用C库函数gethostname用主机名填充数组,获取指向主机名缓冲区的指针,假设它是一个以NUL终止的C字符串,并将指针转换为指向Julia字符串的指针。 这种使用这种需要调用者分配内存以传递给被调用者来填充的模式对于C库函数很常见。 Julia中类似的内存分配通常是通过创建一个未初始化的数组并将指向其数据的指针传递给C函数来完成的。 这就是我们不在这里使用Cstring类型的原因:由于数组未初始化,它可能包含NUL字节。转换到Cstring作为 的一部分会检查包含的NUL字节,因此可能会抛出转换错误。

    可以将Julia函数传递给接受函数指针参数的原生C函数。例如,要匹配满足下面的C原型:

    1. typedef returntype (*functiontype)(argumenttype, ...)

    @cfunction 生成可兼容C的函数指针,来调用Julia函数。 的参数如下:

    • 一个Julia函数
    • 返回类型
    • 一个与输入类型相同的字面量元组与 ccall 一样,所有的参数将会在编译期执行,也就是包含的函数被定义时执行。

    目前仅支持平台默认的C调用转换。这意味着@cfunction产生的指针不能在WINAPI需要stdcall的32位Windows上使用,但是能在(stdcall独立于C调用转换的)WIN64上使用。

    一个典型的例子就是标准C库函数qsort,定义为:

    1. void qsort(void *base, size_t nmemb, size_t size,
    2. int (*compare)(const void*, const void*));

    base 是一个指向长度为nmemb,每个元素大小为size的数组的指针。compare 是一个回调函数,它接受指向两个元素ab的指针并根据a应该排在b的前面或者后面返回一个大于或小于0的整数(如果在顺序无关紧要时返回0)。现在,假设在Julia中有一整个1维数组A的值我们希望用qsort(或者Julia的内置sort函数)函数进行排序。在我们关心调用qsort及其参数传递之前,我们需要写一个为每对值之间进行比较的函数(定义<)。

    1. julia> function mycompare(a, b)::Cint
    2. return (a < b) ? -1 : ((a > b) ? +1 : 0)
    3. end
    4. mycompare (generic function with 1 method)

    注意,我们需要小心返回值的类型:qsort期待一个返回C语言int类型的函数,所以我们需要标出这个函数的返回值类型来确保它返回Cint

    为了传递这个函数给C,我们可以用宏 @cfunction 取得它的地址:

    1. julia> mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble}));

    @cfunction 需要三个参数: Julia函数 (mycompare), 返回值类型(Cint), 和一个输入参数类型的值元组, 此处是要排序的Cdouble() 元素的数组.

    qsort的最终调用看起来是这样的:

    1. julia> A = [1.3, -2.7, 4.4, 3.1]
    2. 4-element Array{Float64,1}:
    3. 1.3
    4. -2.7
    5. 4.4
    6. 3.1
    7. julia> ccall(:qsort, Cvoid, (Ptr{Cdouble}, Csize_t, Csize_t, Ptr{Cvoid}),
    8. A, length(A), sizeof(eltype(A)), mycompare_c)
    9. julia> A
    10. 4-element Array{Float64,1}:
    11. -2.7
    12. 1.3
    13. 3.1
    14. 4.4

    As can be seen, A is changed to the sorted array [-2.7, 1.3, 3.1, 4.4]. Note that Julia knows how to convert an array into a Ptr{Cdouble}, how to compute the size of a type in bytes (identical to C's sizeof operator), and so on. For fun, try inserting a println("mycompare($a, $b)") line into mycompare, which will allow you to see the comparisons that qsort is performing (and to verify that it is really calling the Julia function that you passed to it).

    Mapping C Types to Julia

    It is critical to exactly match the declared C type with its declaration in Julia. Inconsistencies can cause code that works correctly on one system to fail or produce indeterminate results on a different system.

    Note that no C header files are used anywhere in the process of calling C functions: you are responsible for making sure that your Julia types and call signatures accurately reflect those in the C header file. (The Clang package can be used to auto-generate Julia code from a C header file.)

    Julia automatically inserts calls to the function to convert each argument to the specified type. For example, the following call:

    1. ccall((:foo, "libfoo"), Cvoid, (Int32, Float64), x, y)

    will behave as if the following were written:

    Base.cconvert normally just calls , but can be defined to return an arbitrary new object more appropriate for passing to C. This should be used to perform all allocations of memory that will be accessed by the C code. For example, this is used to convert an Array of objects (e.g. strings) to an array of pointers.

    Base.unsafe_convert handles conversion to types. It is considered unsafe because converting an object to a native pointer can hide the object from the garbage collector, causing it to be freed prematurely.

    类型对应关系

    首先来复习一下 Julia 类型相关的术语:

    Bits Types

    There are several special types to be aware of, as no other type can be defined to behave the same:

    • Float32

    和C语言中的 float 类型完全对应(以及Fortran中的 REAL*4

    • Float64

    和C语言中的 double 类型完全对应(以及Fortran中的 REAL*8

    和C语言中的 complex float 类型完全对应(以及Fortran中的 COMPLEX*8

    • ComplexF64

    和C语言中的 complex double 类型完全对应(以及Fortran中的 COMPLEX*16

    • Signed

    和C语言中的 signed 类型标识完全对应(以及Fortran中的任意 INTEGER 类型) Julia中任何不是Signed 的子类型的类型,都会被认为是unsigned类型。

    • Ref{T}

    Ptr{T} 行为相同,能通过Julia的GC管理其内存。

    • Array{T,N}

    When an array is passed to C as a Ptr{T} argument, it is not reinterpret-cast: Julia requires that the element type of the array matches T, and the address of the first element is passed.

    因此,如果一个 Array 中的数据格式不正确,它必须被显式地转换 ,通过类似 trunc(Int32, a) 的函数。

    若要将一个数组 A 以不同类型的指针传递,而不提前转换数据, (比如,将一个 Float64 数组传给一个处理原生字节的函数时),你 可以将这一参数声明为 Ptr{Cvoid}

    如果一个元素类型为 Ptr{T} 的数组作为 Ptr{Ptr{T}} 类型的参数传递, 将会首先尝试进行 null-terminated copy(即直到下一个元素为null才停止复制),并将每一个元素使用其通过 Base.cconvert 转换后的版本替换。 这允许,比如,将一个 argv 的指针数组,其类型为 Vector{String} ,传递给一个类型为 Ptr{Ptr{Cchar}} 的参数。

    在所有我们当前支持的系统上,C/C++中的基本值类型都能以如下的方式对应到Julia类型。每一个C类型也有一个对应的同名Julia类型,添加一个‘C’前缀。 这在写可移植代码时将非常有用(注意到,C中的 int 类型和Julia的 Int 不同)。

    与系统独立的:

    The type is essentially a synonym for Ptr{UInt8}, except the conversion to Cstring throws an error if the Julia string contains any embedded NUL characters (which would cause the string to be silently truncated if the C routine treats NUL as the terminator). If you are passing a char* to a C routine that does not assume NUL termination (e.g. because you pass an explicit string length), or if you know for certain that your Julia string does not contain NUL and want to skip the check, you can use Ptr{UInt8} as the argument type. Cstring can also be used as the ccall return type, but in that case it obviously does not introduce any extra checks and is only meant to improve readability of the call.

    依赖于系统的:

    Note

    Warning

    For string arguments (char) the Julia type should be Cstring (if NUL- terminated data is expected) or either Ptr{Cchar} or Ptr{UInt8} otherwise (these two pointer types have the same effect), as described above, not String. Similarly, for array arguments (T[] or T), the Julia type should again be Ptr{T}, not Vector{T}.

    Warning

    Julia's Char type is 32 bits, which is not the same as the wide character type (wchar_t or wint_t) on all platforms.

    Warning

    A return type of Union{} means the function will not return i.e. C++11 [[noreturn]] or C11 _Noreturn (e.g. jl_throw or longjmp). Do not use this for functions that return no value (void) but do return, use Cvoid instead.

    Note

    For wchar_t* arguments, the Julia type should be (if the C routine expects a NUL-terminated string) or Ptr{Cwchar_t} otherwise. Note also that UTF-8 string data in Julia is internally NUL-terminated, so it can be passed to C functions expecting NUL-terminated data without making a copy (but using the Cwstring type will cause an error to be thrown if the string itself contains NUL characters).

    Note

    C functions that take an argument of the type char** can be called by using a Ptr{Ptr{UInt8}} type within Julia. For example, C functions of the form:

    1. int main(int argc, char **argv);

    can be called via the following Julia code:

    1. argv = [ "a.out", "arg1", "arg2" ]
    2. ccall(:main, Int32, (Int32, Ptr{Ptr{UInt8}}), length(argv), argv)

    Note

    For Fortran functions taking variable length strings of type character(len=*) the string lengths are provided as hidden arguments. Type and position of these arguments in the list are compiler specific, where compiler vendors usually default to using Csizet as type and append the hidden arguments at the end of the argument list. While this behaviour is fixed for some compilers (GNU), others _optionally permit placing hidden arguments directly after the character argument (Intel,PGI). For example, Fortran subroutines of the form

    1. subroutine test(str1, str2)
    2. character(len=*) :: str1,str2

    can be called via the following Julia code, where the lengths are appended

    1. str1 = "foo"
    2. str2 = "bar"
    3. ccall(:test, Void, (Ptr{UInt8}, Ptr{UInt8}, Csize_t, Csize_t),
    4. str1, str2, sizeof(str1), sizeof(str2))

    Warning

    Fortran compilers may also add other hidden arguments for pointers, assumed-shape (:) and assumed-size (*) arrays. Such behaviour can be avoided by using ISO_C_BINDING and including bind(c) in the definition of the subroutine, which is strongly recommended for interoperable code. In this case there will be no hidden arguments, at the cost of some language features (e.g. only character(len=1) will be permitted to pass strings).

    Note

    A C function declared to return Cvoid will return the value nothing in Julia.

    Struct Type correspondences

    Composite types, aka struct in C or TYPE in Fortran90 (or STRUCTURE / RECORD in some variants of F77), can be mirrored in Julia by creating a struct definition with the same field layout.

    When used recursively, isbits types are stored inline. All other types are stored as a pointer to the data. When mirroring a struct used by-value inside another struct in C, it is imperative that you do not attempt to manually copy the fields over, as this will not preserve the correct field alignment. Instead, declare an isbits struct type and use that instead. Unnamed structs are not possible in the translation to Julia.

    Packed structs and union declarations are not supported by Julia.

    You can get a near approximation of a union if you know, a priori, the field that will have the greatest size (potentially including padding). When translating your fields to Julia, declare the Julia field to be only of that type.

    Arrays of parameters can be expressed with NTuple:

    in C:

    1. struct B {
    2. int A[3];
    3. };
    4. b_a_2 = B.A[2];

    in Julia:

    1. struct B
    2. A::NTuple{3, Cint}
    3. end
    4. b_a_2 = B.A[3] # 请注意索引上的不同(Julia 中为 1-based 索引,C 中为 0-based 索引)

    Arrays of unknown size (C99-compliant variable length structs specified by [] or [0]) are not directly supported. Often the best way to deal with these is to deal with the byte offsets directly. For example, if a C library declared a proper string type and returned a pointer to it:

    1. struct String {
    2. int strlen;
    3. char data[];
    4. };

    In Julia, we can access the parts independently to make a copy of that string:

    1. str = from_c::Ptr{Cvoid}
    2. len = unsafe_load(Ptr{Cint}(str))
    3. unsafe_string(str + Core.sizeof(Cint), len)

    The type arguments to ccall and @cfunction are evaluated statically, when the method containing the usage is defined. They therefore must take the form of a literal tuple, not a variable, and cannot reference local variables.

    This may sound like a strange restriction, but remember that since C is not a dynamic language like Julia, its functions can only accept argument types with a statically-known, fixed signature.

    However, while the type layout must be known statically to compute the intended C ABI, the static parameters of the function are considered to be part of this static environment. The static parameters of the function may be used as type parameters in the call signature, as long as they don't affect the layout of the type. For example, f(x::T) where {T} = ccall(:valid, Ptr{T}, (Ptr{T},), x) is valid, since Ptr is always a word-size primitive type. But, g(x::T) where {T} = ccall(:notvalid, T, (T,), x) is not valid, since the type layout of T is not known statically.

    SIMD 值

    Note: This feature is currently implemented on 64-bit x86 and AArch64 platforms only.

    If a C/C++ routine has an argument or return value that is a native SIMD type, the corresponding Julia type is a homogeneous tuple of VecElement that naturally maps to the SIMD type. Specifically:

    For instance, consider this C routine that uses AVX intrinsics:

    1. #include <immintrin.h>
    2. __m256 dist( __m256 a, __m256 b ) {
    3. return _mm256_sqrt_ps(_mm256_add_ps(_mm256_mul_ps(a, a),
    4. _mm256_mul_ps(b, b)));
    5. }

    The following Julia code calls dist using ccall:

    1. const m256 = NTuple{8, VecElement{Float32}}
    2. a = m256(ntuple(i -> VecElement(sin(Float32(i))), 8))
    3. b = m256(ntuple(i -> VecElement(cos(Float32(i))), 8))
    4. function call_dist(a::m256, b::m256)
    5. ccall((:dist, "libdist"), m256, (m256, m256), a, b)
    6. end
    7. println(call_dist(a,b))

    The host machine must have the requisite SIMD registers. For example, the code above will not work on hosts without AVX support.

    内存所有权

    malloc/free

    Memory allocation and deallocation of such objects must be handled by calls to the appropriate cleanup routines in the libraries being used, just like in any C program. Do not try to free an object received from a C library with Libc.free in Julia, as this may result in the free function being called via the wrong libc library and cause Julia to crash. The reverse (passing an object allocated in Julia to be freed by an external library) is equally invalid.

    何时使用 T、Ptr{T} 以及 Ref{T}

    In Julia code wrapping calls to external C routines, ordinary (non-pointer) data should be declared to be of type T inside the , as they are passed by value. For C code accepting pointers, Ref{T} should generally be used for the types of input arguments, allowing the use of pointers to memory managed by either Julia or C through the implicit call to . In contrast, pointers returned by the C function called should be declared to be of output type Ptr{T}, reflecting that the memory pointed to is managed by C only. Pointers contained in C structs should be represented as fields of type Ptr{T} within the corresponding Julia struct types designed to mimic the internal structure of corresponding C structs.

    In Julia code wrapping calls to external Fortran routines, all input arguments should be declared as of type Ref{T}, as Fortran passes all variables by pointers to memory locations. The return type should either be Cvoid for Fortran subroutines, or a T for Fortran functions returning the type T.

    Mapping C Functions to Julia

    For translating a C argument list to Julia:

    • T, where T is one of the primitive types: char, int, long, short, float, double, complex, enum or any of their typedef equivalents

      • T, where T is an equivalent Julia Bits Type (per the table above)
      • if T is an enum, the argument type should be equivalent to Cint or Cuint
      • argument value will be copied (passed by value)
    • struct T (including typedef to a struct)

      • T, where T is a Julia leaf type
      • argument value will be copied (passed by value)
    • void*

      • depends on how this parameter is used, first translate this to the intended pointer type, then determine the Julia equivalent using the remaining rules in this list
      • this argument may be declared as Ptr{Cvoid}, if it really is just an unknown pointer
    • jl_value_t*

      • Any
      • argument value must be a valid Julia object
    • jl_value_t**

      • Ref{Any}
      • argument value must be a valid Julia object (or C_NULL)
    • T*

      • Ref{T}, where T is the Julia type corresponding to T
      • argument value will be copied if it is an isbits type otherwise, the value must be a valid Julia object
    • T (*)(…) (e.g. a pointer to a function)

      • Ptr{Cvoid} (you may need to use explicitly to create this pointer)
    • (e.g. a vararg)

      • T…, where T is the Julia type
      • currently unsupported by @cfunction
    • va_arg

      • not supported by ccall or @cfunction

    ccall / @cfunction return type translation guide

    For translating a C return type to Julia:

    • void

      • Cvoid (this will return the singleton instance nothing::Cvoid)
      • T, where T is an equivalent Julia Bits Type (per the table above)
      • if T is an enum, the argument type should be equivalent to Cint or Cuint
      • argument value will be copied (returned by-value)
    • struct T (including typedef to a struct)

      • T, where T is a Julia Leaf Type
      • argument value will be copied (returned by-value)
    • void*

      • depends on how this parameter is used, first translate this to the intended pointer type, then determine the Julia equivalent using the remaining rules in this list
      • this argument may be declared as Ptr{Cvoid}, if it really is just an unknown pointer
    • jl_value_t*

      • Any
      • argument value must be a valid Julia object
    • jl_value_t**

      • Ptr{Any} (Ref{Any} is invalid as a return type)
      • argument value must be a valid Julia object (or C_NULL)
    • T*

      • If the memory is already owned by Julia, or is an type, and is known to be non-null:

        • Ref{T}, where T is the Julia type corresponding to T
        • a return type of Ref{Any} is invalid, it should either be Any (corresponding to jl_value_t) or Ptr{Any} (corresponding to jl_value_t*)
      • If the memory is owned by C:

        • Ptr{T}, where T is the Julia type corresponding to T
    • T (*)(…) (e.g. a pointer to a function)

      • Ptr{Cvoid} (you may need to use @cfunction explicitly to create this pointer)

    Passing Pointers for Modifying Inputs

    Because C doesn't support multiple return values, often C functions will take pointers to data that the function will modify. To accomplish this within a , you need to first encapsulate the value inside a Ref{T} of the appropriate type. When you pass this Ref object as an argument, Julia will automatically pass a C pointer to the encapsulated data:

    1. width = Ref{Cint}(0)
    2. range = Ref{Cfloat}(0)
    3. ccall(:foo, Cvoid, (Ref{Cint}, Ref{Cfloat}), width, range)

    Upon return, the contents of width and range can be retrieved (if they were changed by foo) by width[] and range[]; that is, they act like zero-dimensional arrays.

    Special Reference Syntax for ccall (deprecated):

    The & syntax is deprecated, use the Ref{T} argument type instead.

    A prefix & is used on an argument to to indicate that a pointer to a scalar argument should be passed instead of the scalar value itself (required for all Fortran function arguments, as noted above). The following example computes a dot product using a BLAS function.

    The meaning of prefix & is not quite the same as in C. In particular, any changes to the referenced variables will not be visible in Julia unless the type is mutable (declared via mutable struct). However, even for immutable structs it will not cause any harm for called functions to attempt such modifications (that is, writing through the passed pointers). Moreover, & may be used with any expression, such as &0 or &f(x).

    When a scalar value is passed with & as an argument of type Ptr{T}, the value will first be converted to type T.

    Some Examples of C Wrappers

    Here is a simple example of a C wrapper that returns a Ptr type:

    1. mutable struct gsl_permutation
    2. end
    3. # The corresponding C signature is
    4. # gsl_permutation * gsl_permutation_alloc (size_t n);
    5. function permutation_alloc(n::Integer)
    6. output_ptr = ccall(
    7. (:gsl_permutation_alloc, :libgsl), # name of C function and library
    8. Ptr{gsl_permutation}, # output type
    9. (Csize_t,), # tuple of input types
    10. n # name of Julia variable to pass in
    11. )
    12. if output_ptr == C_NULL # Could not allocate memory
    13. throw(OutOfMemoryError())
    14. end
    15. return output_ptr
    16. end

    The GNU Scientific Library (here assumed to be accessible through :libgsl) defines an opaque pointer, gsl_permutation *, as the return type of the C function gsl_permutation_alloc. As user code never has to look inside the gsl_permutation struct, the corresponding Julia wrapper simply needs a new type declaration, gsl_permutation, that has no internal fields and whose sole purpose is to be placed in the type parameter of a Ptr type. The return type of the is declared as Ptr{gsl_permutation}, since the memory allocated and pointed to by output_ptr is controlled by C (and not Julia).

    The input n is passed by value, and so the function's input signature is simply declared as (Csize_t,) without any Ref or Ptr necessary. (If the wrapper was calling a Fortran function instead, the corresponding function input signature should instead be (Ref{Csize_t},), since Fortran variables are passed by pointers.) Furthermore, n can be any type that is convertible to a Csize_t integer; the ccall implicitly calls .

    Here is a second example wrapping the corresponding destructor:

    1. # The corresponding C signature is
    2. # void gsl_permutation_free (gsl_permutation * p);
    3. function permutation_free(p::Ref{gsl_permutation})
    4. ccall(
    5. (:gsl_permutation_free, :libgsl), # name of C function and library
    6. Cvoid, # output type
    7. (Ref{gsl_permutation},), # tuple of input types
    8. p # name of Julia variable to pass in
    9. )
    10. end

    Here, the input p is declared to be of type Ref{gsl_permutation}, meaning that the memory that p points to may be managed by Julia or by C. A pointer to memory allocated by C should be of type Ptr{gsl_permutation}, but it is convertible using Base.cconvert and therefore can be used in the same (covariant) context of the input argument to a . A pointer to memory allocated by Julia must be of type Ref{gsl_permutation}, to ensure that the memory address pointed to is valid and that Julia's garbage collector manages the chunk of memory pointed to correctly. Therefore, the Ref{gsl_permutation} declaration allows pointers managed by C or Julia to be used.

    If the C wrapper never expects the user to pass pointers to memory managed by Julia, then using p::Ptr{gsl_permutation} for the method signature of the wrapper and similarly in the ccall is also acceptable.

    Here is a third example passing Julia arrays:

    1. # The corresponding C signature is
    2. # int gsl_sf_bessel_Jn_array (int nmin, int nmax, double x,
    3. # double result_array[])
    4. function sf_bessel_Jn_array(nmin::Integer, nmax::Integer, x::Real)
    5. if nmax < nmin
    6. throw(DomainError())
    7. end
    8. result_array = Vector{Cdouble}(undef, nmax - nmin + 1)
    9. errorcode = ccall(
    10. (:gsl_sf_bessel_Jn_array, :libgsl), # name of C function and library
    11. Cint, # output type
    12. (Cint, Cint, Cdouble, Ref{Cdouble}),# tuple of input types
    13. nmin, nmax, x, result_array # names of Julia variables to pass in
    14. )
    15. if errorcode != 0
    16. error("GSL error code $errorcode")
    17. end
    18. return result_array
    19. end

    The C function wrapped returns an integer error code; the results of the actual evaluation of the Bessel J function populate the Julia array result_array. This variable can only be used with corresponding input type declaration Ref{Cdouble}, since its memory is allocated and managed by Julia, not C. The implicit call to unpacks the Julia pointer to a Julia array data structure into a form understandable by C.

    Note that for this code to work correctly, result_array must be declared to be of type Ref{Cdouble} and not Ptr{Cdouble}. The memory is managed by Julia and the Ref signature alerts Julia's garbage collector to keep managing the memory for result_array while the ccall executes. If Ptr{Cdouble} were used instead, the may still work, but Julia's garbage collector would not be aware that the memory declared for result_array is being used by the external C function. As a result, the code may produce a memory leak if result_array never gets freed by the garbage collector, or if the garbage collector prematurely frees result_array, the C function may end up throwing an invalid memory access exception.

    垃圾回收安全

    When passing data to a ccall, it is best to avoid using the function. Instead define a convert method and pass the variables directly to the ccall. automatically arranges that all of its arguments will be preserved from garbage collection until the call returns. If a C API will store a reference to memory allocated by Julia, after the ccall returns, you must arrange that the object remains visible to the garbage collector. The suggested way to handle this is to make a global variable of type Array{Ref,1} to hold these values, until the C library notifies you that it is finished with them.

    Whenever you have created a pointer to Julia data, you must ensure the original data exists until you are done with using the pointer. Many methods in Julia such as and String make copies of data instead of taking ownership of the buffer, so that it is safe to free (or alter) the original data without affecting Julia. A notable exception is which, for performance reasons, shares (or can be told to take ownership of) the underlying buffer.

    The garbage collector does not guarantee any order of finalization. That is, if a contained a reference to b and both a and b are due for garbage collection, there is no guarantee that b would be finalized after a. If proper finalization of a depends on b being valid, it must be handled in other ways.

    A (name, library) function specification must be a constant expression. However, it is possible to use computed values as function names by staging through eval as follows:

    1. @eval ccall(($(string("a", "b")), "lib"), ...

    This expression constructs a name using string, then substitutes this name into a new expression, which is then evaluated. Keep in mind that eval only operates at the top level, so within this expression local variables will not be available (unless their values are substituted with $). For this reason, eval is typically only used to form top-level definitions, for example when wrapping libraries that contain many similar functions. A similar example can be constructed for @cfunction.

    However, doing this will also be very slow and leak memory, so you should usually avoid this and instead keep reading. The next section discusses how to use indirect calls to efficiently accomplish a similar effect.

    非直接调用

    The first argument to can also be an expression evaluated at run time. In this case, the expression must evaluate to a Ptr, which will be used as the address of the native function to call. This behavior occurs when the first ccall argument contains references to non-constants, such as local variables, function arguments, or non-constant globals.

    For example, you might look up the function via dlsym, then cache it in a shared reference for that session. For example:

    1. macro dlsym(func, lib)
    2. z = Ref{Ptr{Cvoid}}(C_NULL)
    3. quote
    4. let zlocal = $z[]
    5. if zlocal == C_NULL
    6. zlocal = dlsym($(esc(lib))::Ptr{Cvoid}, $(esc(func)))::Ptr{Cvoid}
    7. $z[] = $zlocal
    8. end
    9. zlocal
    10. end
    11. end
    12. end
    13. mylibvar = Libdl.dlopen("mylib")
    14. ccall(@dlsym("myfunc", mylibvar), Cvoid, ())

    Closure cfunctions

    The first argument to can be marked with a $, in which case the return value will instead be a struct CFunction which closes over the argument. You must ensure that this return object is kept alive until all uses of it are done. The contents and code at the cfunction pointer will be erased via a finalizer when this reference is dropped and atexit. This is not usually needed, since this functionality is not present in C, but can be useful for dealing with ill-designed APIs which don't provide a separate closure environment parameter.

    1. function qsort(a::Vector{T}, cmp) where T
    2. isbits(T) || throw(ArgumentError("this method can only qsort isbits arrays"))
    3. callback = @cfunction $cmp Cint (Ref{T}, Ref{T})
    4. # Here, `callback` isa Base.CFunction, which will be converted to Ptr{Cvoid}
    5. # (and protected against finalization) by the ccall
    6. ccall(:qsort, Cvoid, (Ptr{T}, Csize_t, Csize_t, Ptr{Cvoid}),
    7. a, length(a), Base.elsize(a), callback)
    8. # We could instead use:
    9. # GC.@preserve callback begin
    10. # use(Base.unsafe_convert(Ptr{Cvoid}, callback))
    11. # end
    12. # if we needed to use it outside of a `ccall`
    13. return a
    14. end

    关闭库

    It is sometimes useful to close (unload) a library so that it can be reloaded. For instance, when developing C code for use with Julia, one may need to compile, call the C code from Julia, then close the library, make an edit, recompile, and load in the new changes. One can either restart Julia or use the Libdl functions to manage the library explicitly, such as:

    1. lib = Libdl.dlopen("./my_lib.so") # 显式打开库
    2. sym = Libdl.dlsym(lib, :my_fcn) # 获得用于调用函数的符号
    3. ccall(sym, ...) # 直接用指针 `sym` 而不是 (symbol, library) 元组,其余参数保持不变
    4. Libdl.dlclose(lib) # 显式关闭库

    Note that when using ccall with the tuple input (e.g., ccall((:my_fcn, "./my_lib.so"), …)), the library is opened implicitly and it may not be explicitly closed.

    调用规约

    The second argument to can optionally be a calling convention specifier (immediately preceding return type). Without any specifier, the platform-default C calling convention is used. Other supported conventions are: stdcall, cdecl, fastcall, and thiscall (no-op on 64-bit Windows). For example (from base/libc.jl) we see the same gethostnameccall as above, but with the correct signature for Windows:

    1. hn = Vector{UInt8}(undef, 256)
    2. err = ccall(:gethostname, stdcall, Int32, (Ptr{UInt8}, UInt32), hn, length(hn))

    请参阅 来获得更多信息。

    There is one additional special calling convention llvmcall, which allows inserting calls to LLVM intrinsics directly. This can be especially useful when targeting unusual platforms such as GPGPUs. For example, for , we need to be able to read the thread index:

    1. ccall("llvm.nvvm.read.ptx.sreg.tid.x", llvmcall, Int32, ())

    As with any ccall, it is essential to get the argument signature exactly correct. Also, note that there is no compatibility layer that ensures the intrinsic makes sense and works on the current target, unlike the equivalent Julia functions exposed by Core.Intrinsics.

    Global variables exported by native libraries can be accessed by name using the cglobal function. The arguments to are a symbol specification identical to that used by ccall, and a type describing the value stored in the variable:

    1. julia> cglobal((:errno, :libc), Int32)
    2. Ptr{Int32} @0x00007f418d0816b8

    The result is a pointer giving the address of the value. The value can be manipulated through this pointer using and unsafe_store!.

    Accessing Data through a Pointer

    The following methods are described as "unsafe" because a bad pointer or type declaration can cause Julia to terminate abruptly.

    Given a Ptr{T}, the contents of type T can generally be copied from the referenced memory into a Julia object using unsafe_load(ptr, [index]). The index argument is optional (default is 1), and follows the Julia-convention of 1-based indexing. This function is intentionally similar to the behavior of and setindex! (e.g. [] access syntax).

    The return value will be a new object initialized to contain a copy of the contents of the referenced memory. The referenced memory can safely be freed or released.

    If T is Any, then the memory is assumed to contain a reference to a Julia object (a jl_value_t), the result will be a reference to this object, and the object will not be copied. You must be careful in this case to ensure that the object was always visible to the garbage collector (pointers do not count, but the new reference does) to ensure the memory is not prematurely freed. Note that if the object was not originally allocated by Julia, the new object will never be finalized by Julia's garbage collector. If the Ptr itself is actually a jl_value_t, it can be converted back to a Julia object reference by . (Julia values v can be converted to jl_value_t* pointers, as Ptr{Cvoid}, by calling pointer_from_objref(v).)

    The reverse operation (writing data to a Ptr{T}), can be performed using . Currently, this is only supported for primitive types or other pointer-free (isbits) immutable struct types.

    Any operation that throws an error is probably currently unimplemented and should be posted as a bug so that it can be resolved.

    If the pointer of interest is a plain-data array (primitive type or immutable struct), the function unsafe_wrap(Array, ptr,dims, own = false) may be more useful. The final parameter should be true if Julia should "take ownership" of the underlying buffer and call free(ptr) when the returned Array object is finalized. If the own parameter is omitted or false, the caller must ensure the buffer remains in existence until all access is complete.

    Arithmetic on the Ptr type in Julia (e.g. using +) does not behave the same as C's pointer arithmetic. Adding an integer to a Ptr in Julia always moves the pointer by some number of bytes, not elements. This way, the address values obtained from pointer arithmetic do not depend on the element types of pointers.

    线程安全

    Some C libraries execute their callbacks from a different thread, and since Julia isn't thread-safe you'll need to take some extra precautions. In particular, you'll need to set up a two-layered system: the C callback should only schedule (via Julia's event loop) the execution of your "real" callback. To do this, create an object and wait on it:

    1. cond = Base.AsyncCondition()
    2. wait(cond)

    传递给 C 的回调应该只通过 将 cond.handle 作为参数传递给 :uv_async_send 并调用,注意避免任何内存分配操作或与 Julia 运行时的其他交互。

    注意,事件可能会合并,因此对 uv_async_send 的多个调用可能会导致对该条件的单个唤醒通知。

    关于 Callbacks 的更多内容

    C++

    如需要直接易用的C++接口,即直接用Julia写封装代码,请参考 Cxx。如需封装C++库的工具,即用C++写封装/胶水代码,请参考。