I need help setting up a simple C++/C# SWIG project. I am having a hard time putting together a C++ project that uses the SWIG bindings. I'm using Visual Studio 2010 and the most recent version of SWIG.
My code is simply:
cpp_file.h:
#pragma once
class cpp_file
{
public:
cpp_file(void);
~cpp_file(void);
int times2(int arg);
};
cpp_file.cpp
#include "cpp_file.h"
cpp_file::cpp_file(void)
{
}
cpp_file::~cpp_file(void)
{
}
int cpp_file::times2(int arg)
{
return arg * 2;
}
And my SWIG interface file is:
cpp_file.i
/* cpp_file.i */
%module cpp_file
%{
/* Put header files here or function declarations like below */
extern int times2(int arg);
%}
extern int times2(int arg);
I can compile this project fine. I then run the SWIG command to generate the wrapper:
swig -csharp "C:/pathToFile/cpp_file.i"
I don't know what to do at this point. I can't find any tutorials or documentation that explains what to do in Visual Studio 2010.
When I add the *cpp_file_wrap.c* file that SWIG generates to my Visual Studio 2010 project and attempt to build the project, I get two linker errors.
The first error message points to my dll file. The second error points to some object file (*.obj). The symbol that the error message mentions "_times2" is no where to be found in my project.
How can I move forward with my simple project? Is there some tutorial or some documentation that explains this process step-by-step? I can't find anything that involves C++ with SWIG, C#, Visual Studio 2010, and .Net v4.0.
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.
Installation. Apart from that you may need “Microsoft Visual Studio 14.0” or higher to run swig program in windows. To illustrate the use of swig, suppose we have some c function and we want to add it to other languages like Tcl, Perl, Python (I am interacting with python), Java and C#.
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.
Step-by-Step instructions to completely build in the VS2010 IDE:
%module
name to match.<project>
with the name of the project. There will be a preprocessor definition <project>_EXPORTS
already defined for your DLL project (see Project, Properties, C++, Preprocessor). %include <windows.i>
helps SWIG understand certain "Window-isms" like __declspec
. #pragma once
#ifdef <project>_EXPORTS
#define <project>_API __declspec(dllexport)
#else
#define <project>_API __declspec(dllimport)
#endif
class <project>_API cpp_file
{
public:
cpp_file(void);
~cpp_file(void);
int times2(int arg);
};
%module cpp
%{
#include "cpp_file.h"
%}
%include <windows.i>
%include "cpp_file.h"
cpp_file.i
, Properties, General, Item Type as Custom Build Tool.cpp_file.i
and Compile. This should create four files: three in the C# Generated folder and one in the C++ project.bin\Debug
to ..\Debug
or whatever the relative path to the C++ Project output directory is. The .exe and .dll need to be in the same directory.Main
, add the lines:var cpp = new cpp_file();
Console.WriteLine(cpp.times2(5));
Good luck! Let me know if you get it to work. I can expand on anything unclear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With