Thread: extern doesnt go well with structs?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    extern doesnt go well with structs?

    Consider the following:

    myfile.c

    Code:
    #include <stdio.h>
    
    extern struct mystruct ab;
    
    int main()
    {
         ab.a = 'a';
    }
    myfile2.c
    Code:
    #include <stdio.h>
    
    struct mystruct
    {
          char a;
          char b;
    } ab;
    
    
    ....
    c> gcc myfile.c myfile2.c

    When I do this I get the error:
    invalid use of undefined type 'struct mystruct'

    Any help would be appreciated thanks.

  2. #2
    Registered User bboozzoo's Avatar
    Join Date
    Jan 2009
    Posts
    14
    how is the code of myfile supposed to know the how mystruct looks like?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need a header that has the definition of struct mystruct. The two source files should then include this header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by bboozzoo View Post
    how is the code of myfile supposed to know the how mystruct looks like?
    if I change myfile.c to:

    Code:
    #include <stdio.h>
    
    extern struct mystruct
    {
          char a;
          char b;
    } ab;
    
    int main()
    {
         ab.a = 'a';
    }
    it still wont work though.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The general way to do this is the following:

    myfile.c
    Code:
    #include <stdio.h>
    #include "myfile.h"
    
    extern struct mystruct ab;
    
    int main()
    {
         ab.a = 'a';
    }
    myfile2.c
    Code:
    #include <stdio.h>
    #include "myfile.h"
    
    struct mystruct ab;
    myfile.h
    Code:
    struct mystruct
    {
          char a;
          char b;
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  4. extern structs
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 12-03-2002, 08:14 AM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM