C ++ Using new compiler functions to generate code for use by the old compiler

I studied some of the "new" C ++ standards (C ++ 11 and C ++ 14) and it got me thinking. I am currently using the VC ++ 2008 compiler for my projects (for various reasons), which means the newest standard I have access to is C ++ 03, plus TR1. There are some nice things about TR1, but there are features in C ++ 11 and C ++ 14 that would be nice.

My question is this: was there any way to generate some code using a newer compiler (like MSVC2012 or 2013) to build libraries or DLLs using new C ++ 11 and C ++ 14 features and then links what's in my project that the '08 compiler runs?

The only thing I could think it would not work would be anywhere if I had a C ++ 11 or C ++ 14 feature in a header included by my '08 compiler. However, as long as all the "new" ones were hidden behind my interface, shouldn't this work?

+3


source to share


2 answers


Yes, but it will be terrible. Since the ABI is incompatible, you will need to go down to the extern "C" {}

ABIness.

This means that you cannot pass C ++ objects at all, as I said, is painful. This also means that it must be a DLL, since you cannot link in a static lib with a different ABI.



It's up to you, if it's worth wrapping the DLL in a C API just to use a few new features or not, I would recommend that you just update the whole project.

I almost forgot, you probably also can not link lib lib, so you'll have code that uses LoadLibrary

, GetProcAddress

and FreeLibrary

(did I mention that it's ugly / painful?).

+6


source


Unfortunately, what you are trying to do is not possible with MSVC. They intentionally break binary compatibility with every major release, as stated in the MSDN documentation :

To enable new optimization and debug checks, Visual Studio's implementation of the C ++ Standard Library intentionally breaks binary compatibility from one version to another . Therefore, when the C ++ standard library is used, object files and static libraries compiled using different versions cannot be mixed in one binary (EXE or DLL), and C ++ standard library objects cannot be transferred between binaries. which are compiled using different versions. This confusion emits linker errors regarding _MSC_VER mismatches. (_MSC_VER is a macro that contains the major version of the compiler, such as 1800 for Visual C ++ in Visual Studio 2013.) This check cannot detect DLL mixing, and it cannot detect mixing that is associated with Visual C ++ 2008 or earlier.



Your parameters should then only pass POD types or implement COM interfaces for interoperability between DLLs compiled using a different version of the VC compiler, none of which is particularly acceptable.

My advice would be if you have to stick with VS2008 for some legacy applications, suck it and get along with the feature set it supports (at least you have TR1). For newer projects, try and tell your team how to use the newer VC versions.

+1


source







All Articles