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.
- Start Visual Studio 2010
- File, New, Project from Existing Code...
- Select "Visual C++" project type and click Next.
- Enter project file location where the .cpp/.h/.i files are.
- For Project Name, choose the name used in %module statement in your .i file (case matters).
- Select project type "Dynamically linked library (DLL) project" and click Next.
- Add to Include search paths the path to the Python.h file, usually something like "C:\Python27\include" and click Next.
- Click Finish.
- Right-click the Project in Solution Explorer, Add, Existing Item..., and select your .i file.
- Right-click the .i file, Properties, and select Configuration "All Configurations".
- Change Item Type to "Custom Build Tool" and click Apply.
- Select "Custom Build Tool" in Properties (it will appear after Apply above).
- 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).
- In Outputs enter "%(Filename)_wrap.cxx;$(Outdir)%(Filename).py".
- Click OK.
- Right-click the .i file, and select Compile.
- Right-click the project, Add, New Filter, name it "Generated Files".
- 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).
- Right-click "Generated Files", Add, Exiting Item and select the _wrap.cxx file that was generated by the compile.
- Right-click the project, Properties.
- Select Configuration "All Configurations".
- Select Configuration Properties, Linker, General, Additional Library Directories and add the path to the python libraries, typically "C:\Python27\libs".
- Select Configuration Properties, General and set TargetName to "_$(ProjectName)".
- Set Target Extension to ".pyd".
- Build the "Release" version of the project. You can't build the Debug version unless you build a debug version of Python itself.
- 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:
- 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.
- Create a folder in the C# project called Generated.
- 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
.
- Note the whole class has to be exported. Replace
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"
- Select
cpp_file.i
, Properties, General, Item Type as Custom Build Tool. - Select Apply to create the Custom Build Tool property group.
- In Custom Build Tool, General, Command Line enter:
swig -csharp -c++ -outdir GeneratedFolderPath cpp_file.i - In Outputs, enter cpp_file_wrap.cxx, and click OK to close the dialog.
- Right-click
cpp_file.i
and Compile. This should create four files: three in the C# Generated folder and one in the C++ project. - Create a Generated Files filter in the C++ project and add cpp_file_wrap.cxx to it.
- Add the three Generated files to the C# project's Generated folder.
- Right-click the C# project and add the C++ project as a dependency.
- 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. - In the C# project's
Main
, add the lines:var cpp = new cpp_file();
Console.WriteLine(cpp.times2(5));
- Build the solution.
- 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
Are Destructors Run When Calling Exit()
How to Restart Linux from Inside a C++ Program
Checking for Duplicates in a Vector
What Is The Reason for Having Unreserved Identifiers as Built-In Macros in Gcc
How to Debug Segmentation Fault
Could Not Load Spatialite Extension in Qsqlite ( Qt 5.9)
Cmake: How to Change Properties on Subdirectory Project Targets
Adding Header and .Cpp Files in a Project Built with Cmake
Getopt Fails to Detect Missing Argument for Option
Advantages of Using Arrays Instead of Std::Vector
Read and Write on Serial Port in Ubuntu with C/C++ and Libserial
When Did "And" Become an Operator in C++
What Does the |= Operator Mean in C++