error: expected declaration specifiers or '...' before string constant

15,466

It looks like you're trying to call a function at the top level of a .c file. You can't do that.

The error message could be a little nicer, but it looks like gcc is interpreting this as an attempt to declare a type named PyChar_addGetSetter and/or to declare a global variable of that type, and failing to interpret it either way, and telling you why that attempt failde.

Share:
15,466
Marshall Davis
Author by

Marshall Davis

Web developer and mobile application developer. https://www.scrumalliance.org/community/profile/mdavis107

Updated on June 14, 2022

Comments

  • Marshall Davis
    Marshall Davis almost 2 years

    I am getting an error on a specific line that mimics the usage in another file.

    PyObject *pyCharGetHeight(PyChar *self, void *closure) {
      CHAR_DATA *ch = PyChar_AsChar((PyObject *)self);
      PyObject *height = NULL;
      if(ch != NULL) height = Py_BuildValue("s", charGetHeight(ch));
      return height;
    }
    
    PyChar_addGetSetter("height", pyCharGetHeight, NULL, "Returns character's height");
    

    is returning the following error, PyChar_addGetSetter... line

    error: expected declaration specifiers or â...â before string constant

    I am using the following includes:

    #include <Python.h>
    #include "../scripts/pychar.h"
    

    And pychar.h does define PyChar_addGetSetter() as prototyped here:

    void PyChar_addGetSetter(const char *name, void *g, void *s, const char *doc);
    

    The function is written in ../scripts/pychar.c as follows:

    void PyChar_addGetSetter(const char *name, void *g, void *s, const char *doc) {
      // make sure our list of get/setters is created
      if(pychar_getsetters == NULL) pychar_getsetters = newList();
    
      // make the GetSetter def
      PyGetSetDef *def = calloc(1, sizeof(PyGetSetDef));
      def->name        = strdup(name);
      def->get         = (getter)g;
      def->set         = (setter)s;
      def->doc         = (doc ? strdup(doc) : NULL);
      def->closure     = NULL;
      listPut(pychar_getsetters, def);
    }
    

    It seems like a structure or type is not being recognized, I am guessing my function.