views:

589

answers:

3

I'm trying to create a c++ library for use on windows/MSVC.

My problem is that it seems that in order to link properly, I need to distribute a bunch of different versions, linked against different versions of MSVC's c++ runtimes - single and multi-threaded, debug and release, different compiler versions, various other security and other options.

I'd love to just distribute maybe two, 32 bit and 64 bit.

My idea is to maybe use a different new operator (say, mynew) and custom allocators for all my STL types. When creating the lib, /nodefaultlib. Then, when linking in from a parent project, require them to thunk mynew to new, and my stl allocator to the standard one (or one of their choosing). I guess I'd need to do delete, and a few other functions. Naturally I'd provide an example thunking implementation with the library, but this would hopefully save everyone a lot of headache.

Is this possible? Has anyone ever tried this? Is there a best practices for library creation/distribution on windows/MSVC?

A: 

You don't need to use a custom allocator if you are using C++, and you wrap all allocations around std::tr1::shared_ptr (where you can specify a deallocation function). This ensures that even when clients release the last reference to the shared pointer, it's still code in your library (or your library's CRT) that gets called when the object is to be freed.

This is one way to solve "DLL boundary hell". Hope it helps! :-)

Edit: I think I misread the intent of your question. Rather than not wanting any dependency on a CRT because you are worried about DLL boundary hell, I suppose you just wanted a version of your DLL that you can install anywhere. In that case, you can make your program link to msvcrt.dll. That's available on any Windows system.

You didn't hear this from me, but apparently in the Driver Development Kit you can find some sort of import library that allows newer versions of Visual Studio to link to msvcrt.

Chris Jester-Young
See my comment about msvcrt.dll, correct one of us...
Nick
I agree with your point. I won't edit my post, mostly out of posterity, however, I've upped your post, and other readers should read it too. :-)
Chris Jester-Young
+1  A: 

Link statically to C++ runtime library:

  1. Open project properties.
  2. Go to Configuration Properties | C/C++ | Code Generation section.
  3. Set Runtime Library to Multi-threaded (/MT).
Pavel Chuchuva
+3  A: 

You want static linking, as a general answer.

Quick note on Chris' answer (don't want to de-boost cause it's mostly good, but...):

DO NOT link to msvcrt.dll (the unversioned one); this is the OS-specific version DLL, and if you link to it, your app probably will not work on other versions of Windows. You should always be linking to msvcrt##.dll, as far as I know. The DDK may contain a lib for it, but don't link to it unless you really know what you're doing.

Nick
I've upped your answer. You don't normally want to use msvcrt.dll, and the DDK approach is undocumented and unsupported as far as I know. Hence the "you didn't hear this from me". :-)
Chris Jester-Young