How to Swig in VS2010

How to create a DLL with SWIG from Visual Studio 2010

Step-by-step instructions. This assumes you have the source and are building a single DLL extension that links the source directly into it. I didn't go back through it after creating a working project, so I may have missed something. Comment on this post if you get stuck on a step. If you have an existing DLL and want to create a Python extension DLL that wraps it, this steps are slightly different. If you need help with that comment on this post and I will extend it.

Edit 8/19/2012: If starting with a C example, don't use -c++ in step 13 and use .c instead of .cxx for the wrap file extension in steps 14 and 19.

  1. Start Visual Studio 2010
  2. File, New, Project from Existing Code...
  3. Select "Visual C++" project type and click Next.
  4. Enter project file location where the .cpp/.h/.i files are.
  5. For Project Name, choose the name used in %module statement in your .i file (case matters).
  6. Select project type "Dynamically linked library (DLL) project" and click Next.
  7. Add to Include search paths the path to the Python.h file, usually something like "C:\Python27\include" and click Next.
  8. Click Finish.
  9. Right-click the Project in Solution Explorer, Add, Existing Item..., and select your .i file.
  10. Right-click the .i file, Properties, and select Configuration "All Configurations".
  11. Change Item Type to "Custom Build Tool" and click Apply.
  12. Select "Custom Build Tool" in Properties (it will appear after Apply above).
  13. Enter Command Line of "swig -c++ -python -outdir $(Outdir) %(Identity)" (this assumes SWIG is in your path and redirects the generated .py file to the Debug or Release directory as needed).
  14. In Outputs enter "%(Filename)_wrap.cxx;$(Outdir)%(Filename).py".
  15. Click OK.
  16. Right-click the .i file, and select Compile.
  17. Right-click the project, Add, New Filter, name it "Generated Files".
  18. Right-click "Generated Files", click Properties, and set "SCC Files" to "False" (if you use source-control, this prevents VS2010 trying to check in the generated files in this filter).
  19. Right-click "Generated Files", Add, Exiting Item and select the _wrap.cxx file that was generated by the compile.
  20. Right-click the project, Properties.
  21. Select Configuration "All Configurations".
  22. Select Configuration Properties, Linker, General, Additional Library Directories and add the path to the python libraries, typically "C:\Python27\libs".
  23. Select Configuration Properties, General and set TargetName to "_$(ProjectName)".
  24. Set Target Extension to ".pyd".
  25. Build the "Release" version of the project. You can't build the Debug version unless you build a debug version of Python itself.
  26. Open a console, go to the Release directory of the project, run python, import your module, and call a function!

How can I build this simple C++/SWIG/C# project in Visual Studio 2010?

Step-by-Step instructions to completely build in the VS2010 IDE:

  1. Create a solution with two projects:
    • C# Console Application
    • C++ Win32 Console Application (Name=cpp, DLL, empty project). If you choose a different name, don't use the name of a class in your project and update the .i file %module name to match.
  2. Create a folder in the C# project called Generated.
  3. Add your .cpp, .h, and .i file to the DLL with the modifications below.

    • Note the whole class has to be exported. Replace <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).
    • The module name cannot match a class name in the module.
    • %include <windows.i> helps SWIG understand certain "Window-isms" like __declspec.

cpp_file.h

#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);
};

cpp_file.i

%module cpp

%{
#include "cpp_file.h"
%}

%include <windows.i>
%include "cpp_file.h"
  1. Select cpp_file.i, Properties, General, Item Type as Custom Build Tool.
  2. Select Apply to create the Custom Build Tool property group.
  3. In Custom Build Tool, General, Command Line enter:

    swig -csharp -c++ -outdir GeneratedFolderPath cpp_file.i
  4. In Outputs, enter cpp_file_wrap.cxx, and click OK to close the dialog.
  5. Right-click cpp_file.i and Compile. This should create four files: three in the C# Generated folder and one in the C++ project.
  6. Create a Generated Files filter in the C++ project and add cpp_file_wrap.cxx to it.
  7. Add the three Generated files to the C# project's Generated folder.
  8. Right-click the C# project and add the C++ project as a dependency.
  9. In the C# project's Properties, Build tab, change the Output Path from 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.
  10. In the C# project's Main, add the lines:

    var cpp = new cpp_file();
    Console.WriteLine(cpp.times2(5));
  11. Build the solution.
  12. Run the C# project.

Good luck! Let me know if you get it to work. I can expand on anything unclear.

Not able to run SWIG C# examples in VS 2010

The solution for me was

  • Move the used dll's to the Release (or Debug) directory
  • Run CorFlags /32Bit+ file.exe

where file.exe is the executable produced by building the code.

For more information on what is going on Í found the following links helpful

http://www.davesquared.net/2008/12/systembadimageformatexception-on-64-bit.html

http://blogs.msdn.com/b/joshwil/archive/2005/05/06/415191.aspx

How to set the environment variable QL_NET in Visual Studio 2010 for QuantLib+SWIG

You don't have to set the environment variable from C# or visual studio.
(For Windows 7) Right click on my Computer->Properties->Advance system settings -> Advanced -> Environment Variable

There create a new variable named (under System variables): QL_DIR and set the value to your installation library (which is may be this C:\Users\JRobinson\Desktop\Quantlib-Resolver\lib in your case)

Wrapping c++ class to use it in Lua using SWIG - need simple example

SWIG is a tool for linking scripting languages to C or C++ code. It works as a preprocess step: you run the SWIG executable on a .swig file, which produces a bit of C or C++ code. You then build that code into whatever project you want to do the linking to that scripting language for.

SWIG's Lua support is most certainly not compatible with Lua 5.2. Indeed, you will find very little code out there that is compatible with Lua 5.2. If you want to actually do something with Lua code, stick with 5.1 at least for the time being.

QuantLib+SWIG+C# 4.0+Visual Studio 2010: TypeInitializationException

There indeed seems to be a problem with the SWIG wrappers as distributed and .Net 4.0.

I'm not working on that platform, so I can't speak based on personal experience. However, the issue was discussed recently on the QuantLib mailing list, and the solution contributed there by Mark Gillis was reported to work. You can read the relevant thread at http://thread.gmane.org/gmane.comp.finance.quantlib.user/8238. Hope this helps...



Related Topics



Leave a reply



Submit