1

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.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
James PL
  • 11
  • 3
  • Your code looks correct. Try moving the definition of `phOsal_RPi_DataParams_t` to `main.c` and see what happens. Either it's some weird linking/include issue or a stupid typo. – banach-space May 25 '15 at 17:51
  • 2
    I see nothing wrong with the posted code. One possible typo to look for is the `0` versus `O` typo. Otherwise, you need to put together an [MCVE](http://stackoverflow.com/help/mcve) – user3386109 May 25 '15 at 17:56
  • 2
    Your `#include` should use `""` rather than `<>` -- but since you didn't get an error on the `#include` directive, that's not the cause of the problem you're seeing. Did you copy-and-paste your exact code? As @user3386109 suggests, a `0` vs. `O` typo could explain it, but the identifiers in your question are identical; are they identical in your actual code? http://sscce.org/ – Keith Thompson May 25 '15 at 20:29
  • 1
    Possibly the `#include` is finding some other file of the same filename; you could test this by writing `#error foo` in the actual header and rebuilding – M.M May 25 '15 at 21:37

1 Answers1

-2

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.

Community
  • 1
  • 1
codebender
  • 78
  • 5
  • 1
    No, this is incorrect. The code quoted in the question does not give a name to the struct; it's only identifier is the typedef. –  May 25 '15 at 18:25
  • Ah your right there isn't a name to the struct, my apologies. I edited my answer, and I still think the other stack overflow post will be of help. – codebender May 25 '15 at 19:27
  • 1
    I'm afraid that's *still* incorrect. The primary purpose of `typedef` is to allow types to be used by their new name. If you still had to refer to them as their original `struct` name, what's the point? –  May 25 '15 at 19:36
  • What if your trying to provide an Opaque type as described here: http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c The second example in the answer. – codebender May 25 '15 at 19:44
  • Is my answer sufficient now? I agreed that the "struct alias" declaration was incorrect in main, sorry for the confusion. – codebender May 25 '15 at 20:03
  • No, your answer is still incorrect. The code in the question appears to be correct; it defines `phOsal_RPi_DataParams_t` as a typedef. You suggest adding a struct tag; that will make no difference. – Keith Thompson May 25 '15 at 20:27
  • @KeithThompson I never said his code was incorrect I was merely adding to the options he had already tried. I think my links are helpful and I added your comments, I don't see how my answer couldn't be of some help :( – codebender May 25 '15 at 21:19
  • @codebender: Quite simply, it doesn't answer the question. If you want to provide helpful information that isn't an answer, post a comment. (I think you don't have enough reputation to do that yet. Be patient.) – Keith Thompson May 25 '15 at 21:24