C ++ 11 code / library in a non C ++ 11 program

Suppose I compile the code in C ++ 11 (I will use Lambdas) for ".o" or the library ".a". And I have a program in which I will include the previous library and the header file, which I cannot compile with C ++ 11, but the old one (C ++ 98). Will it compile and work fine?

+4
source share
3 answers

Probably not. The reason the name change (why the ABI changes) in C ++ exists is because differences in incompatibility between C ++ versions can make the code unstable if it works at all.

If you have code that does not compile with C ++ 11, you may have to reorganize one of your programs to compile with another compiler. (Most likely, your old code will compile with the new compiler)

If this is not an option, you can try to make a C ++ 11 lib DLL with a C interface or with a COM object interface, but exceptions will be thrown at this boundary, and if you go along the DLL path, you most likely want to write a class- a shell to access the C ++ 11 object so that it acts as an object on your side of the pre C ++ 11 border.

+3
source

It will work fine if:

  • the header (public) does not use any C ++ 11 functions
  • ABI has not changed
    • refer to your platform / compiler on this
  • regular dependency has not changed
    • according to a GCC document linked to Vaughn Cato, this includes a standard library. Anything that generates different layouts of code or an object when compiling with C ++ 11 and is used by both the library and the client can be a problem ... even if it is not used in the interface itself.

If point 3 is your only problem, you can work around it by compiling a dynamic library (depending on the .so or .dynlib platform or DLL, as Adrian suggests) with all the dependencies statically linked internally and not exported. It's a little hairy though.

+2
source

One general approach is to provide the C version of the API ( extern "C" functions) with objects passed using opaque pointers. This is most likely compatible between languages ​​and compilers.

+1
source

Source: https://habr.com/ru/post/1495743/


All Articles