Jonathan Livni
Jonathan Livni

Reputation: 107082

SWIG precompiled libraries

I've received a set of 3rd party .lib files and a single .h file which I want to wrap using SWIG so I can use in another language. All SWIG's examples are with the C\C++ source code, but in my case I don't have the source code.
What should I do different to create the wrapper?

Upvotes: 2

Views: 723

Answers (1)

Jacek Sieka
Jacek Sieka

Reputation: 603

While the SWIG examples may include definitions (source code) to allow the reader to compile and try them, you will notice that all examples of interface files (.i) only contain declarations (what you typically find in a header file), which is all SWIG needs to create a wrapper.

The normal way of writing an interface file goes like this:

/* File : example.i */
%module example
%{
/* This block will end up verbatim in the generated wrapper - include your header
 * so that the wrapper will have access to its definitions 
 */
#include "your_header.h"
%}

/* The definitions in this part are used to generate the wrapper.
 * Copy the definitions you want to export to the other language from the header
 * and put them here 
 */

 extern double variable_from_header;
 extern int    function_from_header(int);

If your header file is simple and you want to export every definition in it, you might get away with an interface file that looks like this:

/* File : example.i */
%module example
%{
#include "your_header.h"
%}

%include "your_header.h"

Notice the %include directive which instructs SWIG to parse the included file as if it were part of the interface definition file. See also Section 5.7 of the manual that discusses this approach.

Once you have your wrapper, you link it with the lib as you would link the objects created from the source code in the examples.

Upvotes: 2

Related Questions