cglobal

cglobal((symbol, library) [, type=Void])

Obtain a pointer to a global variable in a C-exported shared library, specified exactly as in ccall. Returns a Ptr{Type}, defaulting to Ptr{Void} if no Type argument is supplied. The values can be read or written by unsafe_load or unsafe_store!, respectively.

Examples

In the Julia programming language, the function cglobal((symbol, library) [, type=Void]) is used to obtain a pointer to a global variable in a C-exported shared library. The function signature is as follows:

cglobal((symbol, library) [, type=Void]) -> Ptr{Type}

Here are some examples of how to use the cglobal function:

  1. Access a global integer variable:

    julia> cglobal((:my_global_int, "mylibrary"), Cint)
    Ptr{Cint} @0x00007f9d23b20140

    This example obtains a pointer to the global integer variable my_global_int in the shared library "mylibrary". The type of the variable is specified as Cint.

  2. Access a global float variable:

    julia> cglobal((:my_global_float, "mylibrary"), Cfloat)
    Ptr{Cfloat} @0x00007f9d23b20160

    This example obtains a pointer to the global float variable my_global_float in the shared library "mylibrary". The type of the variable is specified as Cfloat.

  3. Access a global variable with default type (Ptr{Void}):

    julia> cglobal((:my_global_var, "mylibrary"))
    Ptr{Void} @0x00007f9d23b20180

    If no type argument is supplied, the cglobal function returns a pointer of type Ptr{Void} by default. In this example, we obtain a pointer to the global variable my_global_var in the shared library "mylibrary".

Common mistake example:

julia> cglobal((:nonexistent_var, "mylibrary"))
ERROR: could not find symbol "nonexistent_var" in library "mylibrary"

In this example, the function call fails because the symbol nonexistent_var does not exist in the specified library. Make sure to provide the correct symbol name and ensure that the library contains the desired global variable.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: