Thread: Multiple Source Files with Structures

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    Multiple Source Files with Structures

    i have a big favour to ask, could sombody please give me an example of multiple source files using structures. the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    i have tried before but kept getting "multiple definition" errors and a few others i cant remember, as i kept deleting the projects and adding to my other. i have managed to get functions working in different files before quite comfortably with functions, however i cant seem to manage it with structures.


    thanks,

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this goes into the header
    Code:
    #ifndef STRUCT_EXAMPLE_H
    #define STRUCT_EXAMPLE_H
    
    typedef struct example_tag
    {
       int x;
    }example;
    
    int fillstruct(example* var);
    #endif
    This goes into the c-file
    Code:
    #include "struct_example.h"
    int fillstruct(example* var)
    {
       if(var) var->x = 5.0;
       return 0;
    }
    in this case you can have several c-files using the struct without the problem

    PS. hope you don't use global vars
    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

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by vart
    this goes into the header
    Code:
    #ifndef STRUCT_EXAMPLE_H
    #define STRUCT_EXAMPLE_H
    
    typedef struct example_tag
    {
       int x;
    }example;
    
    int fillstruct(example* var);
    #endif
    This goes into the c-file
    Code:
    #include "struct_example.h"
    int fillstruct(example* var)
    {
       if(var) var->x = 5.0;
       return 0;
    }
    in this case you can have several c-files using the struct without the problem

    PS. hope you don't use global vars
    so the structure goes in the headerfile, and the rest in the .cpp file?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    This is not a large file by any means. So don't worry much with breaking it up. Unless of course, the file contains unrelated code. A good rule of thumb is to divide your source files so that each file contains code that is somehow related.

    > i have tried before but kept getting "multiple definition" errors and a few others i cant remember,

    Some things can make this happen. If you are trying to include a source file (.cpp, .c,...) into other sources files, for instance. Or if you are coding definitions inside a header file (.hpp, .h,...). Header files should not contain any definitions (with a few exceptions like class and structure definitions, iniline function definitions, const definitions).

    > however i cant seem to manage it with structures.

    Is it possible that you are defining your structures like this?

    Code:
    struct SomeStruct {
        /* ... */
    } foo;
    By doing it you are both defining the struct SomeStruct (which is valid inside an header file), and defining a variable called foo that is of type SomeStruct. That will create you problems when trying to include the header file in more than one source file. You will get a multiple-definitions error of foo.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    could you explain why it has typedef and i take it exmple_tag is the name of the structure?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by Mario F.
    > the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    This is not a large file by any means. So don't worry much with breaking it up. Unless of course, the file contains unrelated code. A good rule of thumb is to divide your source files so that each file contains code that is somehow related.

    > i have tried before but kept getting "multiple definition" errors and a few others i cant remember,

    Some things can make this happen. If you are trying to include a source file (.cpp, .c,...) into other sources files, for instance. Or if you are coding definitions inside a header file (.hpp, .h,...). Header files should not contain any definitions (with a few exceptions like class and structure definitions, iniline function definitions, const definitions).

    > however i cant seem to manage it with structures.

    Is it possible that you are defining your structures like this?

    Code:
    struct SomeStruct {
        /* ... */
    } foo;
    By doing it you are both defining the struct SomeStruct (which is valid inside an header file), and defining a variable called foo that is of type SomeStruct. That will create you problems when trying to include the header file in more than one source file. You will get a multiple-definitions error of foo.
    the reason is that it is tedious to work on as i have to keep scrolling up and down all the time, as im not an experienced programmer. and that its rather difficult to find the correct funtion i want to work on.

    i had my structure declared in the header file as

    Code:
    struct somestruct
    {
    int x;
    };
    
    somestruct mystruct[5];

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    anyway it doesnt matter, ill just keep it in one source file.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dac
    could you explain why it has typedef and i take it exmple_tag is the name of the structure?
    it's because I'm regular with using structs in C...
    there - you cannot use struct name without struct keyword if you don't typedef it previously...

    in C++ classes are mostly used...
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 08:25 AM
  2. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  3. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM