Putting Box2D in a DLL

I've seen a few people asking about building Box2D as a DLL, and the answer has been approximately "you have to wrap the whole thing, it doesn't natively build as a DLL."

I came up with a slightly easier way. First, build it as a regular static library. Make sure the 'dumpbin' binary is in your path (a "visual C++ command prompt" from the VC++ tools menu will have this). You also need a Perl interpreter installed and in your path, and this hideous perl script:
Code:
my $n=1;

my $headersdir="../../../Box2D/";

our @headers=();
use File::Find;
sub Wanted {
$_=~/\.h$/ or return;
$headers[$#headers+1]=$File::Find::name;
}
File::Find::find(\&Wanted,$headersdir);

my $allheaders;
foreach $h (@headers) {
if (open(FILE,$h)) {
  while (my $line=<FILE>) {
   $allheaders.=$line;
  }
}
}

print "LIBRARY Box2D\n";
print "EXPORTS\n";
my %symbols;
while (my $line=<STDIN>) {
  if ($line=~/External     \| ([^\s]*)/ and $1) {
    $symbols{$1}=1;
  }
}

foreach $sym (keys %symbols) {
  if ($sym=~/b2/ #only export b2Functions
    and !($sym=~/^__/) #not ones that begin with __ as those are system
    and !($sym=~/^\?\?_G/) #not ones with _G, those are scalar deleting destructors which cause warnings if exported
    and (!($sym=~/\@\@([A-Z])/) or $1=~/[Q-Z]/) #not private or protected functions
    and !($sym=~/^\?\?[^A-Za-z0-9]/) #I don't even know, ones beginning ??$ or ??_ don't seem necessary
  ) {
    $sym=~/^[^A-Za-z]*([A-Za-z0-9_]+)/;
    if (index($allheaders,$1)!=-1) { #don't export any functions whose name doesn't appear in a header file
      print "  $sym \@$n\n";
      $n++;
    }
  }
}



Go to the directory with the lib in it, and execute this command:
Code:
DUMPBIN /SYMBOLS Box2D.lib | perl that_script_filename.pl > Box2D.def


This will create a .def file containing all the Box2D functions which need to be exported (probably more than really necessary, but eh, good enough.)

Now simply make a new set of project settings set to build the library as a DLL, and in project settings -> Linker -> Input, set "Module Definition File" to the filename and location of the .def file that was created. Now when you build it, you get the DLL and the necessary .lib file to link to it.

Added bonus method - if you have your own things you want in a DLL wherever you're using Box2D, instead of adding new Box2D project settings, you can make the Box2D functions export from your DLL by importing the static library, and setting your DLL's module definition file to Box2D.def - this works so long as your own exported functions are exported using __declspec(dllexport) rather than using the .def file. (You should also change the first line of the .def file from Box2D to whatever your DLL's filename is.)

Another way to achieve the same goal more minimally would be to just put
Code:
LIBRARY Box2D
EXPORTS
in your .def file, build your project, and then manually copy in the mangled name of each function the compiler reports as an unresolved external symbol (eg. ??1b2World@@QAE@XZ is one of the b2World constructors' mangled name) - doing it this way, my current test project only needs 21 functions exported, versus the 480ish the script exports, but I expect this way you'll also need to add more exports as the project grows, which can be frustrating.

(These instructions are Visual C++ oriented, but I think other compilers that make DLLs can be directed to use def files too.)

Original thread: https://box2d.org/forum/viewtopic.php?t=7749