G++3.2.1 and static decls. in C++ Modules

Riendeau, Mike mike.riendeau at analog.com
Thu Mar 6 15:40:55 EST 2003


We recently upgraded our GCC tool set to 3.2.1 from 2.95.2. I have written a C++ module
to parse STDF binary test data files and return the data in python objects. With the older
compiler, including G++, the 'staticforward' and 'statichere' macros functioned without incident;
I was able to link python by specifying the linker as (CXX).  The new G++ (3.2.1) bails on compilation,
complaining about re-definition of the static vars.  Without posting the whole content of 
the module, I was able to work around it in a manner similar to the following examples,
which can be compiled standalone to demo the problem. You don't need extern "C" in
these examples. If any  python guru has a more elegant workaround than the following,
please let me know.
 
 ---- CUT BROKEN ----
 
/* Do nothing demo of GCC vs. G++ and Python. This works
   with GCC,  but G++ won't compile it. G++ bails on the
   redefinition of static vars. MR I Have to use G++ to
   link Python, because my python module is written 
   in C++. The older G++ (2.95.2) did not have a problem
   with this. */
 
#include <stdio.h>
 
/* Python predefines some macros and types like these */
#define staticforward static
#define statichere static
 
/* Define an arbitrary structure type (Like PyMethodDef) */
typedef struct ADef {
  int dummy;
  void (*af)(int i);
}ADef;
 
/* Pre-declare a static array because we need to refer to
   it, prior to its definition. Similar to Python's method arrays */
static ADef sarr[2];
 
/* Create some void funcs which refer to the sarr */
static void bfunc(int i)
{
  printf("bfunc: sarr[%d] dummy is %d\n",i,sarr[i].dummy);
}
 
static void cfunc(int i)
{
  printf("cfunc: sarr[%d] dummy is %d\n",i,sarr[i].dummy);
}
 
/* Define the previously declared static array
   including the funcs in the definition.  */
statichere ADef sarr[2] = {
  { 12, bfunc },
  { 24, cfunc }
};
 
main(int argc, char *argv[])
{
  sarr[0].af(1);
  sarr[1].af(0);
}
 

---- CUT END BROKEN ----
 
---- CUT FIXED ----
/*
  Do nothing demo of GCC vs. G++ and Python. This works
  with G++ and hacks around the problem at run time at the cost
  of an additional static ptr. I turns out Python has to
  call my module's init() process anyway.
 */
 
#include <stdio.h>
 
/* An arbitrary structure */
typedef struct ADef {
  int dummy;
  void (*af)(int i);
}ADef;
 
/* Pre-declare a pointer to a static array, which will
   be assigned at runtime. */
static ADef *sarr_ptr;
 
/* Create some void funcs which refer to the sarr */
static void bfunc(int i)
{
  printf("bfunc: sarr[%d] dummy is %d\n",i,sarr_ptr[i].dummy);
}
 
static void cfunc(int i)
{
  printf("cfunc: sarr[%d] dummy is %d\n",i,sarr_ptr[i].dummy);
}
 
/* Define the previously declared static array
   including the funcs in the definition.  */
static ADef sarr[2] = {
  { 12, bfunc },
  { 24, cfunc }
};
 
main(int argc, char *argv[])
{
  /* In the python module's initmodulename() function,
     I added lines similar to this line for the Obj method arrays etc. */
  sarr_ptr = sarr;
  /**/
 
  sarr[0].af(1);
  sarr[1].af(0);
}

---- CUT END FIXED -----
 
Is this solution the proper one? If not, are there any switches etc. I should use with
G++ to get it to act properly.
 
Regards,
  M. Riendeau

- 

Michael H. Riendeau 
Staff Test Engineer 
Analog Devices ATE Products Group

http://www.analog.c <http://www.analog.com> om

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030306/e67d9ed8/attachment.html>


More information about the Python-list mailing list