Building RakNet C# Wrapper with Swig, get an annoying error at build

1.4k Views Asked by At

I know I should post this on the official RakNet forum, but I actually have, but it seems to be dead as hell.

The reason why I'm asking here is because I have no idea of C++, I'm a game developer and we're using Unity3D/C# to develop our new game, it's going to be Multiplayer and as RakNet was Open Sourced I wanted to give it a try.

So, here we go, this is the error log:

raknet_wrap.cxx(15441): error C2558: class 'RakNet::ReliabilityLayer' : no copy constructor available or copy constructor is declared 'explicit'

And the line where the error is is this:

jresult = new ReliabilityLayer((const ReliabilityLayer &)result); 

Full function:

SWIGEXPORT void * SWIGSTDCALL CSharp_RakPeer_RemoteSystemStruct_reliabilityLayer_get(void * jarg1) {
  void * jresult ;
  RakNet::RakPeer::RemoteSystemStruct *arg1 = (RakNet::RakPeer::RemoteSystemStruct *) 0 ;
  ReliabilityLayer result;

  arg1 = (RakNet::RakPeer::RemoteSystemStruct *)jarg1; 
  result =  ((arg1)->reliabilityLayer);
  jresult = new ReliabilityLayer((const ReliabilityLayer &)result); 
  return jresult;
}

Can someone tell me why is it not working and explain it for a completely new person to C++ so I can understand the error?

(Edit) Going to also provide the ReliabilityLayer class! Here it is: http://pastebin.com/qTXedJFw

2

There are 2 best solutions below

0
On

OK to whoever is watching this and doesn't have a successful answer.

This is clearly a compiler bug in MSVC 2013 (that's what I tried).

SWIG generates the file 'RakNet_wrap.cxx' with the following line:

jresult = new ReliabilityLayer((const ReliabilityLayer &)result); 

The quickest way around it is to edit this line so that it reads:

jresult = new ReliabilityLayer(result); 

and hence the casting becomes implicit instead of explicit and MSVC 2013 accepts this.

The only problem is that when you hit Build, SWIG will overwrite this file and introduce the error again. So just remove the SWIG prebuild step (Alt+F7->Build Events->Pre-Build Event remove the PreBuild.bat from the command line) and build again. Now it will successfully compile and link as it should.

Don't forget to put the PreBuild.bat file again if you need to modify the source code and run SWIG again!

Also use SWIG 2.0.12; the latest version I tried (3.0.5); generates broken C# code (undefined HandleRef and IntPtr because System.Runtime.InteropServices is not either explicit nor imported via 'using' keyword). I've raised a ticket to the SWIG project about this: https://github.com/swig/swig/issues/433

0
On

So I finally managed to build the RakNet C# DLL of the newest RakNet version.

And, as I don't want anybody with no experience on C++ to go through the madness I did through, I'm just sharing the DLL for easy use. Just import it to your project and start using it.

Here it is: https://drive.google.com/file/d/0BwuOJwLuDZfnM29DTFlLTWZWOXc/view?usp=sharing