MSVC Crossplatform Development Part 6
Swig Now it is time to look at things from the C# side. We will use Swig to generate the wrapper code.
Swig
Download Swig and unpack in the solution directory to a folder called swig
.
Add a new project Generate.NET
to the Libraries
folder.
Add -> New Project -> Visual C++ -> Empty Project
Add a new file called DemoTools.def
by right-clicking on the project.
Add -> New Item -> visual C++ -> Code -> Module-Definition File
Paste the following content into the file. This defines what header files Swig is supposed to parse. There’s a lot more you can do here and you should read the manual when you start on your own project, but it will do for now.
%module DemoTools
%{
#include "..\DemoTools\Counter.h"
#include "..\DemoTools\PlatformID.h"
%}
%include "..\DemoTools\Counter.h"
%include "..\DemoTools\PlatformID.h"
- Open the file’s properties, and make sure all configurations and platforms are selected.
- Change Item Type to
Custom Build Tool
. - Apply, and select the Custom Build Tool. Make these changes:
- Command Line:
..\swig\swig.exe -csharp -c++ -namespace DemoTools -importall -outdir ..\NetDemoTools\ ..\Generate.NET\DemoTools.def
- Description:
Generating SWIG Wrapper for DemoTools
- Outputs:
DemoTools_wrap.cxx
- Treat Output as Content: Yes
- Command Line:
NetDemoTools
Create another project at the root of the solution. This will be a Visual C# Shared Project called NetDemoTools
which will be added next to the C++ Shared Project.
With this project in place, we can build the Generate.NET
project. If everything goes according to plan, you will get a few warnings. The build output looks somewhat like this:
So there’s a warning about those operator+
methods we added to our Counter class. And we should care about that warning because it is the only way we have to change the counter value. And indeed, C# does not do operator overloading. That’s why Swig cannot convert these methods.
Swig can be configured to cope with this. We’ll just have to instruct it to rename the operator method to a normal method by changing its name. Change DemoTools.def so that it looks like this:
%module DemoTools
%{
#include "..\DemoTools\Counter.h"
#include "..\DemoTools\PlatformID.h"
%}
%rename(Add) operator+;
%include "..\DemoTools\Counter.h"
%include "..\DemoTools\PlatformID.h"
After that build again and the warnings will be gone. Right-click the NetDemoTools project and choose Add Existing Items
. Add all 4 .cs
files in the folder. (These were generated by building Generate.NET
).
It would be nice if newly generated files could be added automatically in the future. To do this I opened up NetDemoTools.projitems
in a text editor, and change the last ItemGroup to:
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)*.cs" />
</ItemGroup>
Wrapper
There is another file that got generated by building Generate.NET
. In that project’s folder, you will find DemoTools_wrap.cxx
. This file contains the C++ wrapper code that is needed by every .NET library we will build. You’ll need to add this file to several projects:
Generate.NET
: Because this file gets updated on a new build, this will trigger the build again. Or in other words: we need a file that changes every time in order for the custom build tool to trigger after the first build.DemoTools.Windows.Native
DemoTools.Android.Native
Or in other words, add it to every native library project.
Leave a Comment