C++ libs & ABI compatilibilty
May 14, 2014
When you’re building a server product that will support C++ APIs you need to consider your ABI – the binary interface. Typically, C++ APIs are distributed as headers and libs. If the functions your API exports include parameters that use, for instance, std::string, you immediately have a problem, as you’re requiring client code to use the same STL implementation as you did to build the lib. That’s OK if client code has access to the source, and can rebuild. But commercial, proprietary products, tend not to distribute source. So how to avoid forcing dependencies on API client code? I went searching for some resources, and found two especially good ones I had to flag.
Here’s Thiago Macieira on binary compatibility: an excellent presentation with guidelines for library authors. Here’s a summary of Thiago’s recommendations…
- Use pimpl idiom to hide object size
- Use plain old data types in function signatures
- Don’t hand out ptrs or refs to internals
- No inline funcs
- All classes need one non inline virtual; probably the dtor
- Avoid virtuals in template classes
- Do not reorder or remove public members, or change access levels
2 means no STL or Boost types in function parameters. I’ll address 6 by avoiding templates in my API.
This article by Agner Fog is a superb detailed survey of data sizes & alignments, stack alignment, call conventions for register usage and parameter handling, name mangling schemes and object file formats inc COFF for all the major x86 & x64 OSes and C++ compilers. Strongly recommended.