views:

58

answers:

3

Hello,

I'm working on a C++ project that produces a lib that other teams use. It's being produced in a few different flavours:

  1. Win32 Debug Dynamic
  2. Win32 Debug Static
  3. Win32 Release Dynamic
  4. Win32 Release Static
  5. x64 Debug Dynamic
  6. x64 Debug Static
  7. x64 Release Dynamic
  8. x64 Release Static

I'm wondering what the best wisdom is on how to name the dlls and what arguments are for different naming conventions. Do I output the libs into different directories, or do I append some letters on the end of the lib to differentiate them, or something else? One concern is that if I use directories, but don't give all the libs different names, users of the library will have problems where they accidentally use the wrong lib. Are these concerns valid?

Thanks very much.

+1  A: 

I output all the different flavours into the same directory, using a naming convention to disambiguate. By having them in the same directory, the linker directories don't have to change between flavour. The libraries then get linked using a set preprocessor directives that select the #pragma (lib,...) directive for the flavour being compiled.

Joe Gauterin
Hmm yes, good point, I didn't think of using #pragma (lib). This would work well to select the correct Platform and Configuration. But, maybe isn't so great for selecting between dynamic and static libs. Perhaps the user could specify a #LIBRARYNAME_STATIC or #LIBRARYNAME_DLL or something in their project settings to make this selection. Do you think that'd work out ok?
Scott Langham
Ah, I thought the dynamic/static referred to whether the library linked to the dynamic CRT or the static CRT. You'd have to use some custom #define if you're trying to choose between static linking and dynamic linking.
Joe Gauterin
+1  A: 

Using the Microsoft convention might be a good idea. The static version is prefixed "lib". The debug version is post-fixed "d". The 64-bit version goes in the amd64 subdirectory. Not the greatest convention but at least it is familiar.

In MSVC, you can use the predefined _DLL macro to detect that the user selected the DLL version of the CRT. The _WIN64 macro is defined if the 64-bit compilation is selected. Enough to generate the required #pragma comment(lib, "name") directive.

Hans Passant
+1  A: 

I think boost handles this by naming the .lib files after the specific platform. I like including the following pieces of information:

  1. compiler major version (i.e. "vc80", "vc91", etc)
  2. runtime version (i.e. "mt", "mdd", etc)
  3. your library version (i.e "1.0", "2.1.1234", etc)

For instance, if your library is named "NetInfo", it is version 1.2.3, it was linked with debug CRT dynamically, and it was built with Visual Studio 2005:

NetInfo_1.2.3_vc80_mdd

The only thing to worry about is how people will consume your library: statically or dynamically. The way I usually do it is as follows:

If your library links to dynamic CRT, your library is provided as a DLL; otherwise, your library is provided as a static lib. The reasoning is that if people are dynamically linking with CRT then it's safe to assume they don't mind dynamically linking with your library. If you want to provide both options then I usually stick a "s" on the end to indicate it is a static library; the lack of a "s" indicates it is a dynamic library.

Example:

NetInfo_1.2.3_vc80_mdds.lib - static library, links with dynamic debug CRT
NetInfo_1.2.3_vc80_mds.lib  - static library, links with dynamic release CRT
NetInfo_1.2.3_vc80_mtds.lib - static library, links with static debug CRT
NetInfo_1.2.3_vc80_mts.lib  - static library, links with static release CRT
NetInfo_1.2.3_vc80_mdd.lib  - import library, links with dynamic debug CRT
NetInfo_1.2.3_vc80_mdd.dll  - dynamic library, links with dynamic debug CRT
NetInfo_1.2.3_vc80_md.lib   - import library, links with dynamic release CRT
NetInfo_1.2.3_vc80_md.dll   - dynamic library, links with dynamic release CRT
NetInfo_1.2.3_vc80_mtd.lib  - import library, links with static debug CRT
NetInfo_1.2.3_vc80_mtd.dll  - dynamic library, links with static debug CRT
NetInfo_1.2.3_vc80_mt.lib   - import library, links with static release CRT
NetInfo_1.2.3_vc80_mt.dll   - dynamic library, links with static release CRT

This method is a little extra work, but it covers all the bases. If you are providing different platforms then you should also stick "x86" and "x64" on there.

Then in your header file you can use _WIN64, _DLL, and _DEBUG macros to figure out which library to pull in. If you go all out and provide static and dynamic libraries for all options then you will need an additional define like NETINFO_USE_STATIC_LIB to determine whether to pull in the dynamic or static flavor.

This method allows you to keep all the files in the same directory and lets you know the specifics just by looking at the name of the file. The downside is that some people might complain about loading a verbosely named dll instead of a simple "NetInfo.dll" (especially if they are using LoadLibrary), but that's really a minor issue. Doesn't seem to deter people from using boost.

Luke