How to Create a Dll with Swig from Visual Studio 2010

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!

Creating a DLL from a wrapped cpp file with SWIG

If you look in the libs directory of your Python installation I suspect you will find a python27.lib and not a python27_d.lib. I believe that the _d.lib is the debug version of the Python library and your Python installation didn't include it. Elsewhere I've seen it suggested that the simplest way around this is to download the Python sources and build the release and debug versions yourself but I've never tried this. Alternatively change you build to use the release version of the Python .lib. You should be able to debug your own code but not the Python code then.

Using a compiled DLL in Java with SWIG and Visual Studio 2015

The answer from axalis was absolutly right. Here some details:

  1. Add example.cpp
  2. Load the precompiled DLL (example.dll) with LoadLibrary and save a reference
  3. Load methods from there with GetProcAddress and save a reference for each method
  4. Write an "implemantation" for each method where you just call the method loaded before
  5. Compile and link everything to exampleWrap.dll and load this dll in the Java project

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.

use swig -go on windows with Visual Studio

According to SWIG Tutorial and SWIG Comparability, it works just with 32 bit versions of Windows:

SWIG also works perfectly well under all known 32 bit versions of Windows including 95/98/NT/2000/XP.



Related Topics



Leave a reply



Submit