Thread: library with struct definitions

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    library with struct definitions

    How would I define some structs in a seperate c file and have them exported?

    I have inside c file stuff that looks like this:
    types.c
    Code:
    #include "types.h"
    static struct s_pos{
      ...
      };
    extern struct s_cell{
      ...
      };
    and in header, types.h

    Code:
    struct s_cell;
    things are declared in this
    Code:
    #include "types.h"
    extern struct s_cell map[256][256];
    When I compile, I get error
    [warning] storage size of `map` isn't known

    so they must not be exported correctly.

    Could someone explain or point me to a helpful page, thanks.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Include the file in which struct cell is defined.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    storage size of map is unknown because any declaration with extern is not a definition. What itCbitC said was true:

    Code:
    /* types.h */
    
    extern struct s_cell;
    
    /* types.c */
    
    struct s_cell { /* ... */ };
    
    struct s_cell map[256][256];
    This sort of thing can be useful for hiding implementations and so forth, though.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    What I want to do is have the data types defined in a seperate file that will compile to an object alone and then include into another file and set up variables using those data types.

    I want struct s_cell and struct s_cell map[x][y] to be in source different files.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well as long as you compile types.c with the source that defines map, then you should be ok. Make sure to include the header in both types.c and the other source file. From what I've shown you, nothing needs to be different.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Should I be able to do something like this?

    Code:
     > types.c
    struct s_cell {
      int foo;
      int bar;
      };
    
     > types.h
    struct s_cell;
    
     > defs.c
    #include "types.h"
    struct s_cell map[256][256];
    map[0][0].foo = 4;
    map[0][0].bar = 3;
    
     > defs.h
    extern struct s_cell map[256][256];
    
     > main.c
    #include "defs.h"
    int main()
    {map[0][0].foo = map[0][0].bar;}
    Sorry, I must be misunderstanding someone.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Why not declare and define the struct in the header file?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    That's not what I want to do. :P

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not really sure how more explicit I can be, but this is starting to look completely wrong.

    > defs.c
    #include "types.h"
    struct s_cell map[256][256];
    map[0][0].foo = 4;
    map[0][0].bar = 3;

    First of all, you cannot put anything but declarations or definitions outside of a function, so even if map was correct in definition, you would need to use an array initializer, which is probably not what you want. Declaration and definition also mean separate things. While a declaration can be a definition, only the definition sets aside space for a variable. Meaning that you actually have a variable in global scope, which is, more often than not, a symptom of bad design.

    Likewise, cramming all your declarations in one file does not help in any capacity, it only reaffirms that your design is poor. Learn about scope.

    Now that we have an understanding, let me assume that map indeed should be global.

    The extern keyword when applied to an identifier merely declares the identifier as a specific type of thing (for map, a 256-element array of int [256], if that makes sense). The definition occurs later, in another source file.

    The static keyword has several meanings, determine if any of them apply to your situation.

    But if map is indeed global, then defs.c should probably be scrapped and map itself not statically defined. If map is not global, then this whole thread is a nonissue, and you need to redesign your program.
    Last edited by whiteflags; 02-28-2009 at 02:12 AM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by seaking1 View Post
    Should I be able to do something like this?
    .
    yes except the contents of the types.c should be moved to types.h
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Yes, map is a global.
    Last edited by seaking1; 02-28-2009 at 02:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM