Skip to content

Commit

Permalink
Docs: miscellaneous formatting and typo fixes in embedding chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao committed May 6, 2015
1 parent 41befb3 commit ba5e5a9
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions doc/manual/embedding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Embedding Julia
**************************

As we have seen (:ref:`man-calling-c-and-fortran-code`) Julia has a simple and efficient way to call functions written in C. But there are situations where the opposite is needed: calling Julia function from C code. This can be used to integrate Julia code into a larger C/C++ project, without the need to rewrite everything in C/C++. Julia has a C API to make this possible. As almost all programming languages have some way to call C functions, the Julia C API can also be used to build further language bridges (e.g. calling Julia from Python or C#).
As we have seen in :ref:`man-calling-c-and-fortran-code`, Julia has a simple and efficient way to call functions written in C. But there are situations where the opposite is needed: calling Julia function from C code. This can be used to integrate Julia code into a larger C/C++ project, without the need to rewrite everything in C/C++. Julia has a C API to make this possible. As almost all programming languages have some way to call C functions, the Julia C API can also be used to build further language bridges (e.g. calling Julia from Python or C#).


High-Level Embedding
Expand All @@ -33,24 +33,26 @@ We start with a simple C program that initializes Julia and calls some Julia cod
return 0;
}

In order to build this program you have to put the path to the Julia header into the include path and link against ``libjulia``. For instance, when Julia is installed to ``$JULIA_DIR``, one can compile the above test program ``test.c`` with gcc using::
In order to build this program you have to put the path to the Julia header into the include path and link against ``libjulia``. For instance, when Julia is installed to ``$JULIA_DIR``, one can compile the above test program ``test.c`` with ``gcc`` using::

gcc -o test -I$JULIA_DIR/include/julia -L$JULIA_DIR/usr/lib -ljulia test.c

Alternatively, look at the ``embedding.c`` program in the julia source tree in the ``examples/`` folder. The file ``ui/repl.c`` program is another simple example of how to set ``jl_compileropts`` options while linking against libjulia.
Alternatively, look at the ``embedding.c`` program in the Julia source tree in the ``examples/`` folder. The file ``ui/repl.c`` program is another simple example of how to set ``jl_compileropts`` options while linking against ``libjulia``.

The first thing that has to be done before calling any other Julia C function is to initialize Julia. This is done by calling ``jl_init``, which takes as argument a C string (``const char*``) to the location where Julia is installed. When the argument is ``NULL``, Julia tries to determine the install location automatically.

The second statement in the test program evaluates a Julia statement using a call to ``jl_eval_string``.

Before the program terminates, it is strongly recommended to call ``jl_atexit_hook``. The above example program calls this before returning from ``main``.

*Note: Currently, dynamically linking with the ``libjulia`` shared library requires passing the ``RTLD_GLOBAL`` option, in Python, this looks like::
.. note::

>>> julia=CDLL('./libjulia.dylib',RTLD_GLOBAL)
>>> julia.jl_init.argtypes = [c_char_p]
>>> julia.jl_init('.')
250593296
Currently, dynamically linking with the ``libjulia`` shared library requires passing the ``RTLD_GLOBAL`` option. In Python, this looks like::

>>> julia=CDLL('./libjulia.dylib',RTLD_GLOBAL)
>>> julia.jl_init.argtypes = [c_char_p]
>>> julia.jl_init('.')
250593296

Using julia-config to automatically determine build parameters
--------------------------------------------------------------
Expand Down Expand Up @@ -86,7 +88,7 @@ combination of 3 flags::
Usage: julia-config [--cflags|--ldflags|--ldlibs]

If the above example source is saved in the file *embed_exmaple.c*, then the following command will compile it into a running program on Linux and Windows (MSYS2 environment),
or if on OS/X, then substitute clang for gcc.::
or if on OS/X, then substitute ``clang`` for ``gcc``.::

/usr/local/julia/share/julia/julia-config.jl --cflags --ldflags --ldlibs | xargs gcc embed_example.c

Expand Down Expand Up @@ -120,7 +122,7 @@ Real applications will not just need to execute expressions, but also return the
printf("sqrt(2.0) in C: %e \n", ret_unboxed);
}

In order to check whether ``ret`` is of a specific Julia type, we can use the ``jl_is_...`` functions. By typing ``typeof(sqrt(2.0))`` into the Julia shell we can see that the return type is ``Float64`` (``double`` in C). To convert the boxed Julia value into a C double the ``jl_unbox_float64`` function is used in the above code snippet.
In order to check whether ``ret`` is of a specific Julia type, we can use the ``jl_is_...`` functions. By typing ``typeof(sqrt(2.0))`` into the Julia shell we can see that the return type is :obj:`Float64` (``double`` in C). To convert the boxed Julia value into a C double the ``jl_unbox_float64`` function is used in the above code snippet.

Corresponding ``jl_box_...`` functions are used to convert the other way::

Expand Down Expand Up @@ -284,7 +286,7 @@ This call will appear to do nothing. However, it is possible to check whether an
if (jl_exception_occurred())
printf("%s \n", jl_typeof_str(jl_exception_occurred()));

If you are using the Julia C API from a language that supports exceptions (e.g. Python, C#, C++), it makes sense to wrap each call into libjulia with a function that checks whether an exception was thrown, and then rethrows the exception in the host language.
If you are using the Julia C API from a language that supports exceptions (e.g. Python, C#, C++), it makes sense to wrap each call into ``libjulia`` with a function that checks whether an exception was thrown, and then rethrows the exception in the host language.


Throwing Julia Exceptions
Expand All @@ -296,7 +298,7 @@ When writing Julia callable functions, it might be necessary to validate argumen
jl_type_error(function_name, (jl_value_t*)jl_float64_type, val);
}

General exceptions can be raised using the funtions::
General exceptions can be raised using the functions::

void jl_error(const char *str);
void jl_errorf(const char *fmt, ...);
Expand Down

0 comments on commit ba5e5a9

Please sign in to comment.