2

Hopefully this is a straightforward question... Here's my process to reproduce this issue. First I create my source file:

bash $ cat t.c
#include "t.h"

int main()
{
  ABC abc;
}

Then I create my corresponding header file:

bash $ cat t.h
#ifdef _T_H
#define _T_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct abc { 
  int a;
} ABC;

#ifdef __cplusplus
}
#endif

#endif

Then, I try to compile it:

bash $ gcc -o t t.c
t.c: In function ‘main’:
t.c:5: error: ‘ABC’ undeclared (first use in this function)
t.c:5: error: (Each undeclared identifier is reported only once
t.c:5: error: for each function it appears in.)
t.c:5: error: expected ‘;’ before ‘abc’

What is going on? If I use 'struct abc' instead of 'ABC' as the type in t.c, then it does compile. Why aren't the typedefs working?

1 Answer 1

9

Try:

#ifndef _T_H
#define _T_H

I happened to notice this because the _T_H didn't line up, and my subconscious brain knew it should.

Sign up to request clarification or add additional context in comments.

2 Comments

Bingo. Wow, yeah, that one little 'n' screwing everything up. Well hopefully somebody else who has that problem will see this thread :)
BTW, do not use things starting with underscore, or containing a double underscore, for your header guards. Such names are reserved for use by the standard library implementation. Just T_H does the trick just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.