Declaration in C considered definition in C++

Viewed 151

I am working on an open-source C file containing the following declaration

static PyTypeObject Bitarraytype;

followed later by the definition

static PyTypeObject Bitarraytype = {
    /* A bunch of stuff */ 
};

I am porting this code to C++ (-std=C++2a), however the above declaration and definition is no longer allowed, as it claims error: redefinition of 'Bitarraytype'

I'm not sure what's causing this, as the first block above is only a declaration from my understanding. Why doesn't this work in C++ and how can I get around it?

1 Answers

The declaration you show is actually a tentative definition in C. C++ doesn't have that, so you get a multiple definition error.

The declaration should be marked extern to mark it is as declaration:

extern PyTypeObject Bitarraytype;

You'll also need to remove the static keyword, as the two are incompatible.

Related