Testing binary compatibility in C++


The task of developing libraries is always associated with the concern of binary compatibility with future releases of the library. Here is a nice summary of rules to ensure binary compatibility: http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++

Nonetheless, I wanted to make a simple framework test environment to quickly test out a particular type of change in a library.

~/bincompat/lib $ cat fun.h

  1. int fun(int a);

~/bincompat/lib $ cat fun.cc

  1. #include "fun.h"
  2.  
  3. int fun(int a) {
  4.   return a*a;
  5. }

~/bincompat/lib $ cat Makefile

  1. libfun.so.1: fun.o
  2.         g++ -shared -Wl,-soname,libfun.so -o libfun.so.1 fun.o
  3.         ln -s libfun.so.1 libfun.so
  4.  
  5. fun.o: fun.cc fun.h
  6.         g++ -fPIC -g -c -Wall fun.cc
  7.  
  8. clean:
  9.         rm -f *.o *.so*

~/bincompat $ cat testfun.cc

  1. #include "lib/fun.h"
  2. #include <iostream>
  3.  
  4. int main() {
  5.   cout < < fun(2) << endl;
  6.   return 0;
  7. }

~/bincompat $ cat Makefile

  1. testfun: testfun.cc
  2.         g++ testfun.cc -o testfun -L./lib -lfun
  3.  
  4. clean:
  5.         rm -f testfun

~/bincompat $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib
~/bincompat $ ./testfun
4

Now lets change the library by adding a variant of the function fun() without breaking compatibility and run our test program again:

~/bincompat/lib $ cat fun.h

  1. int fun(int a, int b); // added
  2. int fun(int a);

~/bincompat/lib $ cat fun.cc

  1. #include "fun.h"
  2.  
  3. int fun(int a) {
  4.   return fun(a, 1); // call the more specific version with default value for the missing param
  5. }
  6. int fun(int a, int b) {
  7.   return a*a + b*b;
  8. }

~/bincompat $ ./testfun
5

  1. #1 by Ajit on September 27, 2009 - 10:12 pm

    How is this a framework to test bin compat?

  2. #2 by therider on September 27, 2009 - 10:31 pm

    By framework I meant a quick environment to test if a change will work. This means a minimum series of tasks gathered in one place. I am sure I will look for this article sometime in future when I have to test something in less than 5 minutes.

(will not be published)