Check for runtime binary compatibility



I am considering adding something like the below to GTK+. Should I put
it inside #ifdef G_OS_WIN32, or is it good to check this on other
platforms, too? (Do all the C (and C++) compilers for the same Unix
platforms always implement the exact same struct field alignment and
size allocation rules?) (32- vs 64-bit code issues are not relevant
here, that is catched in the link phase, isn't it?)

In gtk/gtkmain.h:

void	   gtk_init		 (int	       *argc,
				  char	     ***argv);

void	   gtk_init_abi_check    (int	       *argc,
				  char	     ***argv,
				  size_t        sizeof_GtkWidget);

#define gtk_init(argc, argv) gtk_init_abi_check (argc, argv, sizeof (GtkWidget), sizeof (FILE))

In gtkmain.c:

void
gtk_init_abi_check (int     *argc,
  		    char ***argv,
		    size_t  sizeof_GtkWidget)
{
  if (sizeof_GtkWidget != sizeof (GtkWidget))
    g_error ("Incompatible build! Your code thinks GtkWidget is of different\n"
             "size than this build of GTK+ does.\n"
#ifdef G_OS_WIN32
	     "On Windows, this probably means that you have compiled\n"
	     "your code with gcc without the -fnative-struct switch.\n"
#else
	     "This probably means that you use an unsupported compiler\n"
	     "or the wrong compiler switches."
#endif
	     );

  gtk_init (argc, argv);
}

(Hmm, actually I'm not sure if it was GtkWidget that is one of the
problematic structs (with certain kinds of bitfields that MSVC
allocates differently than gcc without -fnative-struct), have to
check, but you get the idea.)

--tml






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]