0

I am trying to compile randommodule.c but am having a problem in trying to resolve this error:

error: redefinition of 'Random_Type'

It is first being defined at the top of the file,

static PyTypeObject Random_Type;

Then type information about it is needed here,

static PyObject *
random_new(PyTypeObject *type, ...)
{

    if (type == &Random_Type);
        return NULL;
    ...
}

Lastly, it is redefined here, which is where the bug is.

static PyTypeObject Random_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_random.Random",                   /*tp_name*/
    sizeof(RandomObject),               /*tp_basicsize*/
    0,                                  /*tp_itemsize*/
    /* methods */
    0,                                  /*tp_dealloc*/
    0,                                  /*tp_print*/
    0,                                  /*tp_getattr*/
    0,                                  /*tp_setattr*/
    0,                                  /*tp_compare*/
    0,                                  /*tp_repr*/
    0,                                  /*tp_as_number*/
    0,                                  /*tp_as_sequence*/
    0,                                  /*tp_as_mapping*/
    0,                                  /*tp_hash*/
    0,                                  /*tp_call*/
    0,                                  /*tp_str*/
    PyObject_GenericGetAttr,            /*tp_getattro*/
    0,                                  /*tp_setattro*/
    0,                                  /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /*tp_flags*/
    random_doc,                         /*tp_doc*/
    0,                                  /*tp_traverse*/
    0,                                  /*tp_clear*/
    0,                                  /*tp_richcompare*/
    0,                                  /*tp_weaklistoffset*/
    0,                                  /*tp_iter*/
    0,                                  /*tp_iternext*/
    random_methods,                     /*tp_methods*/
    0,                                  /*tp_members*/
    0,                                  /*tp_getset*/
    0,                                  /*tp_base*/
    0,                                  /*tp_dict*/
    0,                                  /*tp_descr_get*/
    0,                                  /*tp_descr_set*/
    0,                                  /*tp_dictoffset*/
    0,                                  /*tp_init*/
    0,                                  /*tp_alloc*/
    random_new,                         /*tp_new*/
    _PyObject_Del,                      /*tp_free*/
    0,                                  /*tp_is_gc*/
};
...

This C array makes reference to random_new so it doesn't seem like there is an easy to way to rearrange the order and make sure there is only one declaration of Random_Type before random_new.

Does someone know how to fix this?

Thanks for your input.

jackw11111
  • 1,457
  • 1
  • 17
  • 34
  • Does this answer your question? [Forward declarations for variables?](https://stackoverflow.com/questions/3714808/forward-declarations-for-variables) (Summary: make sure you are compiling in C, not C++, or make the value `extern`.) – Dúthomhas Oct 05 '22 at 06:30
  • @Dúthomhas I think that is it, but it doesn't make reference to `static` usage. Is it just `extern static PyTypeObject Random_Type[40];` and then `Random_Type = ...` or ? Thanks – jackw11111 Oct 05 '22 at 06:45
  • Oh `extern PyTypeObject Random_Type;` got it, thanks. I am lost when it comes to anything `static`. – jackw11111 Oct 05 '22 at 06:57
  • Just remember, if you declare it `extern` you must _also_ have another `.c` file somewhere where it is actually instanced, and not `static`. That big definition in your last code block needs to be moved to the other `.c` file. (`static` means that an object is not made available to the linker.) – Dúthomhas Oct 05 '22 at 07:01
  • But will that work for the reference `random_new`? It is `static` and declared in the original file but is referenced in that code block. – jackw11111 Oct 05 '22 at 07:51
  • If you declare it `extern`, then _**yes**_, it will absolutely work correctly. I am unsure about the language lawyering otherwise. I always avoid writing weird constructs like that, because I assume it _might_ be referencing different objects. IIUC, the compiler joins them to a single object so it isn’t a problem, but I don’t know that for sure. (That is, to be sure declare the first instance `extern` and do not declare the second `extern`. And now that my brain is working better again, I don’t think you actually have to move it to a separate `.c` file...) – Dúthomhas Oct 05 '22 at 07:55
  • Thats okay, thanks alot for your help. Yes setting the first to `extern` and second without did indeed work. I am getting a runtime error telling me some function is missing, which may or may not be related, so I am trying to see if compiling it with c compiler and linking will fix this issue, but thanks again. – jackw11111 Oct 05 '22 at 08:05
  • @Dúthomhas It was an unrelated bug which is now sorted, thanks again. – jackw11111 Oct 05 '22 at 08:48

0 Answers0