Thread: Multiple definition errors for seemingly no reason at all

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Multiple definition errors for seemingly no reason at all

    I don't know what to think, here. Basically, what I have are three constants stored in a header file, that my compiler (Dev C ver. 4.9.9.2) is telling me have multiple definitions. I have done everything that I know to do (such as using ifndef/endif) to try and get these errors to go away, but it doesn't work.

    Here is some of the relevant code:
    Code:
    #ifndef _CONSTANTS_H
    #define _CONSTANTS_H
    
    const int MAX_ID_SIZE = 16;
    const int FILE_BUFFER_SIZE = 80;
    const int SYMBOL_TABLE_SIZE = 32;
    
    #endif
    Code:
    #ifndef _INITIALIZATION_H
    #define _INITIALIZATION_H
    #include "ScannerParams.h"
    #include "Constants.h"
    
    void InitializeSymbolTable(symbol * symbolTable);
    
    #endif
    Code:
    //Initialization.c
    #include "Initialization.h"
    
    void InitializeSymbolTable(symbol * symbolTable){
       int x;
       for(x = 0; x < SYMBOL_TABLE_SIZE; x++){
          symbolTable[x].info = NULL;
          symbolTable[x].token_Code = -1;      
       }
    }
    Code:
    //main.c
    #include "Initialization.h"
    
    int HashKey(char * key){
       int keyIndex = 0;
       int x;
       for(x = 0; x < MAX_ID_SIZE; x++){
    ....
    ^This code in main.c is the only place where MAX_ID_SIZE is currently being used right now; for whatever reason, C has decided that by using this constant, I am therefore "redefining" it. I don't know how or why.

    Do you guys have any ideas on what I'm doing wrong?
    Last edited by jmc4518; 03-16-2013 at 08:44 PM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The preprocessor only work on a simple cut and paste principle. So since initialization.h is included in both initialization.c and main.c, your definitions appear twice (since they are simply pasted into initialization.h.

    I had this same issue the other day, it can be resolved by moving the definitions to a .c file and only keep declarations in the .h file. Since these are const declared, in order to do that, add an extern qualifier to the declarations.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by Subsonics View Post
    I had this same issue the other day, it can be resolved by moving the definitions to a .c file and only keep declarations in the .h file. Since these are const declared, in order to do that, add an extern qualifier to the declarations.
    Oh, I see. I've never used the extern qualifier before, so I was totally unfamiliar with that. Reading up on it now.

    It's strange that I would have to define these global constants in each of the necessary *.c files. Is this how it is in C++ as well?

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    It appears to not be working. I changed the relevant files to say:

    Code:
    #ifndef _CONSTANTS_H
    #define _CONSTANTS_H
    
    extern const int MAX_ID_SIZE;
    extern const int FILE_BUFFER_SIZE;
    extern const int SYMBOL_TABLE_SIZE;
    
    #endif
    Code:
    //Initialization.c
    #include "Initialization.h"
    
    const int MAX_ID_SIZE = 16;
    const int FILE_BUFFER_SIZE = 80;
    const int SYMBOL_TABLE_SIZE = 32;
    
    void InitializeSymbolTable(symbol * symbolTable){
       int x;
       for(x = 0; x < SYMBOL_TABLE_SIZE; x++){
          symbolTable[x].info = NULL;
          symbolTable[x].token_Code = -1;      
       }
    }
    Code:
    //main.c
    #include "Initialization.h"
    
    const int MAX_ID_SIZE = 16;
    
    int HashKey(char * key){
       int keyIndex = 0;
       int x;
       for(x = 0; x < MAX_ID_SIZE; x++){
    ....
    But it still has a "multiple definition" error for MAX_ID_SIZE.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by jmc4518 View Post
    It's strange that I would have to define these global constants in each of the necessary *.c files.
    I probably should have been more clear. You add a complementary constants.c file to you constants.h where you define the constants. Alternatively you could use #define instead I guess. Pasting them into initialization.c and main.c is what the preprocessor did earlier.
    Last edited by Subsonics; 03-16-2013 at 09:44 PM. Reason: initialization.c not .h

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Oh, hah! Yeah, that was poor reading on my part.

    Okay, the program is working now. Thank you very much for your help.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI:

    It is considered wrong to define something starting with an underline followed by a Capital letter; this is what Compiler writers do. To avoid conflict with you compiler it is best not to do this.

    Code:
    #define _INITIALIZATION_H
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When dealing with global variables in multiple source files there are several ways of using these variables in other files. You need to declare the variable once and only once without the extern keyword in a source file. Then you can either put the extern qualified declaration in a header file that you include when you want to use those variables or you declare the variable with the extern keyword in your other source files.


    main.h header file.
    Code:
     // Don't forget the include guards.
    extern const int global;
    Source file 1:
    Code:
    const int global = 10;
    
    int main()
    {
    
       return(0);
    }
    Source file2:
    Code:
    external const int global;
    
    void someFunctionImplementations()
    {
       double check = global;
    }
    Source file3:
    Code:
    #include "main.h"
    
    void someOtherFunction()
    {
       double check = global;
    }
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  2. multiple definition??
    By coni in forum C Programming
    Replies: 4
    Last Post: 09-11-2009, 07:55 PM
  3. Multiple Definition Problem
    By EASports in forum Linux Programming
    Replies: 2
    Last Post: 07-14-2009, 11:42 AM
  4. Multiple Definition
    By Finfarfin in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2006, 04:18 PM
  5. Seemingly correct printf statements bringing up errors
    By hpteenagewizkid in forum C Programming
    Replies: 9
    Last Post: 11-08-2006, 12:13 PM