简体   繁体   中英

c: unknown type name

I have the following code (in main.c) to initialise a typedef struct that is in a header file (phOsal.h).

main.c

#include <phOsal.h>
...
phOsal_RPi_DataParams_t osal;
...

phOsal.h

/**
* \brief RPi osal parameter stucture
*/
typedef struct
{
    uint16_t wId;            /**< ID of this component, do not modify */
    /* Other stuff */
    uint8_t * bLEDPins;
    uint8_t bLEDCount;

    uint8_t * bDIPSWPins;
    uint8_t bDIPSWCount;

    uint8_t bCommDev_ResetPin;
} phOsal_RPi_DataParams_t;

when I compile this using the cmake commands cmake ./Source and make I get a compile error.

error: unknown type name 'phOsal_RPi_DataParams_t'

If I comment it out the program compiles fine. Also in main.c there are other DataParams that are declared and used but do not throw the compiler error.

I have read a number of questions on here and none of them seem to be what is wrong and have tried the following.

  • Checked whether the #ifndef and #define are correct
  • Changed the layout to be struct dataparams {...}; and then called it in main.c using struct dataparams osal;
  • Changed the include to have "" instead of <>

Any help would be much appreciated.

An option would be to declare your struct as:
typedef struct phOsal_RPi_DataParams_t { ... }phOsal_RPi_DataParams_t;
Look here when your aliasing a struct with typedef at the declaration example:
http://en.wikipedia.org/wiki/Struct_(C_programming_language)#typedef

if you want to you the "dataparams" alias then do the following:

typedef struct phOsal_RPi_DataParams_t
{

 ...
} dataparams;

Then in main.c ditch the struct and just declar it as:

dataparams osal;

The ordering of where you declare it and your main may be wrong as the commenters suggested, and yes the include should be in "" keep that. Here's a good model to follow:
Unknown type name with typedef struct in C

Edited because of comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM