Unknown type name in header file prototype

12,762

I am not clear now which files include which. Let me try to recapitulate this:

  • lexer.c includes str.h and lexer.h
  • main.c includes lexer.h

Is that right? In this case, main.c cannot be compiled as, indeed, the definition of the string type is missing.

As lexer.h always requires str.h to be included, it might be a good idea to out #include "str.h" into this header file.

Share:
12,762
Mark Birger
Author by

Mark Birger

I'm study IT at bachelor program. I need more skills. I have fallen in love with Python. Tired to dispute with gcc.

Updated on June 04, 2022

Comments

  • Mark Birger
    Mark Birger almost 2 years

    I have lexer.c file, which should be included to another .c file. It have

    int getToken(string *attribute) {}

    function and same prototype in lexer.h header file. Also i have helper str.c file for simplify work with strings. It has header file with declaration of type string:

    typedef struct {
        char* str;      //string with \0 at the end
        int length;     //length of the string
        int allocated;  //allocated memory size
    } string;
    

    So, lexer.h included from main file. Then lexer.c starts with:

    #include "str.h"
    #include "lexer.h"
    

    As i understanding, after including str.h type string is visible for lexer.c and lexer.h. But i have compilation error at the prototype in header file:

    ./lexer.h:65:14: error: unknown type name 'string'
    int getToken(string *attribute);
                 ^
    1 error generated.
    

    How can i use this type in header file?

  • Mark Birger
    Mark Birger over 10 years
    yes, thats right! thanks for the help, including str.h to main.c solved my problem. works without #include "str.h" in header file
  • glglgl
    glglgl over 10 years
    @MarkBirger Of course it works. But it might help preventing such things, if all header files are "self-contained", i. e. work without requiring that the includer includes the needed things as well.
  • Mark Birger
    Mark Birger over 10 years
    Oh.. i understand, if i'll include all "dependencies" to .h file, i don't need to include str.h to every file.
  • glglgl
    glglgl over 10 years
    Yes. At least, provided that all .h files support to be included multiple times, e. g. with a include guard protection, or being simple enough just to contain statements which may be repeated.