0

I'm new to C, and I was reading about extern. I used it with built-in data types and it worked fine, however when I tried to use it with structs, it gave the following error. What am I doing wrong?

bar.c

struct MyX
{
    int x;
} X;

foo.c

extern struct MyX X;
int main()
{
    X.x=80;
    return 0;
}

gcc -o barfoo foo.c bar.c error: invalid use of undefined type 'struct MyX'

 X.x=80;
 ^
1
  • 2
    Please put more effort in your question title. Nobody will find this question with this title. Commented Nov 13, 2014 at 11:29

1 Answer 1

2

Because gcc -o barfoo foo.c bar.c (which should really be gcc -Wall -Wextra -g foo.c bar.c -o barfoo; you should always enable all warnings and debugging info when compiling your code) is compiling two compilation units (foo.c and bar.c) then linking their object files together.

Every compilation unit (or translation unit) is "self-sufficient" regarding C declarations; you should declare every -non-predefined- type (e.g. struct) you are using in a translation unit, often in some common header file.

So you should have

struct MyX {
  int x;
};

and

extern struct MyX X;

in both foo.c & bar.c. To avoid copy-pasting, you probably want to put that in some myheader.h and use #include "myheader.h" at start of both foo.c and bar.c, i.e. use

// file myheader.h
#ifndef MYHEADER_INCLUDED
#define MYHEADER_INCLUDED
struct MyX {
  int x;
};
extern struct MyX X;
#endif /* MYHEADER_INCLUDED */

Notice the conventional use of an include guard. Read more about the C preprocessor, e.g. documentation of GNU cpp.

Some programming languages (e.g. Ocaml, Rust, Go, ... but not C, and not yet C++) have modules or packages to deal with that issue.

PS. You should study the source code of some free software coded in C. You'll learn a lot.

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

1 Comment

Still showing same error. How to declare this non-predefined type struct in header file?

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.