diff --git a/Doc/Manual/C.html b/Doc/Manual/C.html index 0bab1a479..80146298f 100644 --- a/Doc/Manual/C.html +++ b/Doc/Manual/C.html @@ -1,11 +1,11 @@
--SWIG is normally used to generate scripting language interface to C or C++ libraries. In the process, it performs analysis of library header files, generates intermediary C code, from which a set of language specific functions is constructed, which can be then accessed in the scripting language code. Having the C code needed to generate wrapper functions for specific language module, we are only one step away from being able to generate pure ANSI C interface to the input C or C++ library. Then we can think of C as just any other target language supported by SWIG. +SWIG is normally used to provide access to C or C++ libraries from target languages such as scripting languages or languages running on a virtual machine. +SWIG performs analysis of the input C/C++ library header files from which it generates further code. For most target languages this code consists of two layers; namely an intermediary C code layer and a set of language specific proxy classes and functions on top of the C code layer. +We could also think of C as just another target language supported by SWIG. +The aim then is to generate a pure ANSI C interface to the input C or C++ library and hence the C target language module.
-With wrapper interface generated by SWIG, it is easy to use functionality of C++ libraries inside application code written in C. The module may also be useful to generate custom API for a library, to suit particular needs, e.g. to supply the function calls with error checking or to implement "design by contract" approach. +With wrapper interfaces generated by SWIG, it is easy to use the functionality of C++ libraries inside application code written in C. This module may also be useful to generate custom APIs for a library, to suit particular needs, e.g. to supply function calls with error checking or to implement a "design by contract".
@@ -64,7 +67,7 @@ Flattening C++ language constructs into a set of C-style functions obviously com
-Consider following simple example. Suppose we have an interface file like: +Consider the following simple example. Suppose we have an interface file like:
-To build a C module, run SWIG using the -c option :
+To build a C module (C as the target language), run SWIG using the -c option :%swig -c example.i
-If building C++, add the -c++ option: +The above assumes C as the input language. If the input language is C++ add the -c++ option:
@@ -94,11 +97,15 @@ $ swig -c++ -c example.i
-This will generate example_wrap.c file or, in the latter case, example_wrap.cxx file, along with example_proxy.h and example_proxy.c files. The name of the file is derived from the name of the input file. To change this, you can use the -o option. +Note that -c is the option specifying the target language and -c++ controls what the input language is. +
+ +
+This will generate an example_wrap.c file or, in the latter case, example_wrap.cxx file, along with example_proxy.h and example_proxy.c files. The name of the file is derived from the name of the input file. To change this, you can use the -o option common to all language modules.
-The wrap file contains the wrapper functions, which perform the main functionality of SWIG: they translate input arguments from C to C++, make call to original functions and all the neccessery actions, and translate C++ output back to C data. The proxy header file contains the interface we can use in C application code. The additional .c file contains calls to the wrapper functions, allowing us to preserve names of the original functions. +The wrap file contains the wrapper functions, which perform the main functionality of SWIG: it translates the input arguments from C to C++, makes calls to the original functions and marshalls C++ output back to C data. The proxy header file contains the interface we can use in C application code. The additional .c file contains calls to the wrapper functions, allowing us to preserve names of the original functions.
-The next step is to build dynamically loadable module, which we can link to our application. This can be done easily, for example using gcc compiler (Linux, MINGW, etc.): +The next step is to build a dynamically loadable module, which we can link to our application. This can be done easily, for example using the gcc compiler (Linux, MinGW, etc.):
@@ -153,14 +160,14 @@ $ g++ -shared example_wrap.o -o libexample.so
-Now the shared library module is ready to use. Note that the name of generated module is important: is should be prefixed with lib, and have the specific extension, like .dll for Windows or .so for Unix systems. +Now the shared library module is ready to use. Note that the name of the generated module is important: is should be prefixed with lib on Unix, and have the specific extension, like .dll for Windows or .so for Unix systems.
--The simplest way to use generated shared module is to link it to the application code on the compiling stage. We have to compile the proxy file as well. The process is usually similar to the shown below: +The simplest way to use the generated shared module is to link it to the application code during the compilation stage. We have to compile the proxy file as well. The process is usually similar to this:
@@ -168,14 +175,14 @@ $ gcc runme.c example_proxy.c -L. -lexample -o runme
-This will compile application code (runme.c), along with proxy and link it against the generated shared module. Following the -L option is the path to the directory containing the shared module. The output executable is ready to use. The last thing to do is to supply the operating system the information of location of our module. This is system dependant, for instance Unix systems look for shared modules in certain directories, like /usr/lib, and additionally we can set the environment variable LD_LIBRARY_PATH for other directories. +This will compile the application code (runme.c), along with the proxy code and link it against the generated shared module. Following the -L option is the path to the directory containing the shared module. The output executable is ready to use. The last thing to do is to supply to the operating system the information of location of our module. This is system dependant, for instance Unix systems look for shared modules in certain directories, like /usr/lib, and additionally we can set the environment variable LD_LIBRARY_PATH (Unix) or PATH (Windows) for other directories.
-Wrapping C functions and variables is obviously performed in straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enchance the code with some additional elements, for instance using check typemap or %extend directive. +Wrapping C functions and variables is obviously performed in a straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enchance the code with some additional elements, for instance using check typemap or %extend directive.
-The main reason of having the C module in SWIG is to be able to access C++ from C. In this chapter we will take a look at the rules of wrapping elements of C++ language. +The main reason of having the C module in SWIG is to be able to access C++ from C. In this chapter we will take a look at the rules of wrapping elements of the C++ language.
-Consider the following example. We have a C++ class, and want to refer to it in C. +Consider the following example. We have a C++ class, and want to use it from C code.
@@ -315,11 +322,11 @@ public:
-What we need to do is to create an object of the class, then to be able to manipulate on it, and finally, to be able to destroy it. SWIG generates C functions for this purpose each time a class declaration is encountered in the interface file. +What we need to do is to create an object of the class, manipulate it, and finally, destroy it. SWIG generates C functions for this purpose each time a class declaration is encountered in the interface file.
-The first two generated functions are used to create and destroy instances of class Circle. Such an instances are represented on the C side as pointers to special structs, called SwigObj. They are all "renamed" (via typedef) to the original class names, so that you can refer to the object instances on the C side using pointers like: +The first two generated functions are used to create and destroy instances of class Circle. Such instances are represented on the C side as pointers to special structs, called SwigObj. They are all "renamed" (via typedef) to the original class names, so that you can use the object instances on the C side using pointers like:
@@ -353,7 +360,7 @@ double Circle_area(Circle * self);
-You can see that in order to refer to the generated object we need to provide a pointer to the object instance (struct Circle in this case) as the first function argument. In fact, this struct is basically wrapping pointer to the "real" C++ object. +You can see that in order to use the generated object we need to provide a pointer to the object instance (struct Circle in this case) as the first function argument. In fact, this struct is basically wrapping pointer to the "real" C++ object.