Monday, March 23, 2009

Linking boost libraries to DLLs in mingw

So, I've recently needed to use the boost libraries to create a DLL for use with the JVMTI. I know that it seems like a strange thing to have to do, but the long and the short of it is that I needed portable thread and socket libraries. So, I couldn't just depend on the header file linkage (like you would have with boost's smart pointers). I needed to link the compiled boost libraries to my DLL.

Now, here comes a problem: JVMTI won't automatically load dynamic libraries that are required by your agent. The obvious solution is to try to statically link boost to the agent DLL. I didn't think that this would be a problem. But, I'm using mingw for my C++ work (it has good support with Eclipse's CDT, which I use because this is JVMTI, and Eclipse is my Java environment). I spent a good day trying to figure it out.

Here is the trick: when you build your boost libraries using bjam, it defaults to naming the static libraries as ".lib" becuase you are using windows. However, mingw is a port of gcc, which expects ".a" libraries. So, g++ won't link to the .lib's. The solution is to simply go into your build directory, and rename all of the .lib files to .a files. And viola, you get linkage. It's crazy, but true. Hopefully this can save some other people some pain.