I am compiling C++ project which is using IJG JPEG library using Visual Studio .NET 2005 (yes, 2008 is yet to come) and it came as a surprise that I stumbled upon struct data alignment issue. I am pretty sure this was not a problem with Visual Studio .NET 2003.
The project includes my C++ files and C source from the library. C++ code includes:
extern "C"
{
#undef FAR
#define XMD_H // Otherwise INT32 is redefined
// NOTE: See jmorecfg.h for condition compilation options
#include "cdjpeg.h"
#include "jversion.h"
};
Running this, I receive an error from the library (thanks for pointing this out early anyway!):
JPEG parameter struct mismatch: library thinks size is 360, caller expects 347.
How comes? Project settings are default and set to use default alignment /Zp. It seems as if compiler uses default value of 1 for my C++ code and uses default value of 8 for C code… with a result of break on runtime.
The workaround is in referencing C code with alignment override using #pragma pack:
extern "C"
{
#undef FAR
#define XMD_H // Otherwise INT32 is redefined
//#pragma pack(show)
#pragma pack(push, 8 )
// NOTE: See jmorecfg.h for condition compilation options
#include "cdjpeg.h"
#include "jversion.h"
#pragma pack(pop)
};