Problem splitting program into different files

This is a discussion on Problem splitting program into different files within the C Programming forums, part of the General Programming Boards category; Hi I am splitting my program into different files and some structures from one file use things declared in another ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Question Problem splitting program into different files

    Hi I am splitting my program into different files and some structures from one file use things declared in another file and vice versa. This meens that whichever file I include first I'm stuffed and I get a error about having an "incomplete type". So I was wondering if there is any way around this problem? Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Put the structure definitions in a header file, like so:
    Code:
    #ifndef HEADER_H
    #define HEADER_H 1
    
    struct thestruct {
        int data;
        // ...
    };
    
    #endif
    And in your source files:
    Code:
    #include "header.h"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Put the structures and function prototypes in a shared header file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    Hmm interesting, so in your programs do you have a header just for structure definitions and you put them all in there or do you just put ones which cause a problem in there or what? Thanks

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    in your programs do you have a header just for structure definitions and you put them all in there or do you just put ones which cause a problem in there or what?
    You can put your structures in different files, and whichever ones you like. Presumably you give the compiler enough information to avoid errors.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    114
    I have one more problem, here is an example:

    /* main.c */
    #include "main.h"
    #include "graphics.h"

    /* main.h */
    int lives;
    extern int lives;

    /* graphics.c */
    printf("%d", lives);

    /* graphics.h */
    ..

    And I get an error in graphics.c that lives doesn't exist. How should I have done that?

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    extern int lives;
    That should be in graphics, not main. It says to the compiler, "don't worry that this is undeclared - it's declared somewhere else". But here, you say that right after declaring it, and then it remains undeclared elsewhere.

    Oh, and you should read the sticky at the top of the forum about posting code - it'll teach you how to use code tags to properly format code when you post it.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Quote Originally Posted by kzar
    /* main.h */
    int lives;
    extern int lives;

    /* graphics.c */
    printf("%d", lives);
    You need to read up on the keyword extern. But to use the variable lives in the graphics.c file, you must declare extern int lives in the graphics.c file.

    The extern keywords means that the compiler should look for definition of the declared variable elsewhere.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    114
    I got my program to compile now, I was really confused before but now I think I get it. Thanks for the help everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  4. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21