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:
-
Access a global integer variable:
julia> cglobal((:my_global_int, "mylibrary"), Cint) Ptr{Cint} @0x00007f9d23b20140This example obtains a pointer to the global integer variable
my_global_intin the shared library"mylibrary". The type of the variable is specified asCint. -
Access a global float variable:
julia> cglobal((:my_global_float, "mylibrary"), Cfloat) Ptr{Cfloat} @0x00007f9d23b20160This example obtains a pointer to the global float variable
my_global_floatin the shared library"mylibrary". The type of the variable is specified asCfloat. -
Access a global variable with default type (Ptr{Void}):
julia> cglobal((:my_global_var, "mylibrary")) Ptr{Void} @0x00007f9d23b20180If no type argument is supplied, the
cglobalfunction returns a pointer of typePtr{Void}by default. In this example, we obtain a pointer to the global variablemy_global_varin 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.