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
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.
I've only used SWIG a small amount but it looks like you're trying to export a function named
times()
in your .i file which doesn't exist. You do have a cpp_file::times()
method but that is not exported. You either need to define the times()
function or export the entire cpp_file
class via SWIG.
I would spend some time reading the official SWIG documentation, particularly the SWIG and C++ section. There is also this question on SO which has some information related to SWIG and VS2010.