For class, I need to take some C++ code and compile it to a .dll file to use with JNI. I have MS Visual C++ 2005 Express (hey, it was free!). Searching around online, I found plenty of things that implied that it was possible to build a DLL with this version of Visual C++, but no specific instructions on how to do it. I actually found some forum posts by other people who were confused like me. Thus this post.
It’s actually pretty easy. Here’s what you do.
- Install Visual C++ 2005 Express Edition, if you have not already.
- If you are using Windows Vista, you should install SP1 for Visual C++ and the Vista compatibility update. These are listed under step 3 on this page, and I believe they also come in through Microsoft Update if you have it enabled.
- Install the Windows Platform SDK and get Visual C++ set up to work with it. As of the time of this writing, instructions for doing this are available here. If you are using Windows Vista, see my note at the bottom of this post.*
- After that’s done, you should notice that you have a Win32 Console Application option available in the New Project dialog of Visual C++. That’s great, but I want a DLL, not a console application. *grumble*
- Here’s the counter-intuitive part. Select the Win32 Console Application option anyway.
- In the Win32 Application Wizard, which shows up after you decide where to put your project, click on Application Settings and select the DLL option. Set any other options here as you like.
- And now you have a new Windows DLL project. You should be set to go.
* If you are running Windows Vista, when you are following the Platform SDK directions, you’ll notice that you need to modify some files in Visual C++’s Program Files directory. Remember that under Windows Vista, if a program that is not elevated attempts to modify a file in the Program Files folder, it will actually not modify that file and create another file in your virtual store with the modification. So, while this modification will always show up when you run (non-elevated) applications, if you want these changes to apply to all users, you should run Notepad or whatever elevated (“Run as Administrator”) to make the modifications.
Tags: C++, Visual Studio