Thread: Header files and multiple definitions

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    12

    Header files and multiple definitions

    Hello!

    So I am working on a rather large program using many .cpp files and ,h files and compiling them all together wit a Makefile.

    I'm not the only one working on this project, and my part of it really comes down to just a few files. Here is a somewhat simplified version of the situation.

    My source code files:
    main.cpp
    foo.cpp
    bar.cpp

    Header files:
    foo.h
    bar.h

    Basically, main calls functions from both foo.cpp and bar.cpp, and foo.cpp calls functions from bar.cpp.

    In other word, foo depends on bar, and main depends on both.

    The .h files contain function prototypes and definitions for their respective files.

    I am using the Makefile to first create .o files for each of the .cpp programs, and then I am basically running
    Code:
    g++ main.o foo.o bar.o
    The result is a list of almost all of my functions and errors saying that I have defined them multiple times!

    What can I do to solve this problem?

    By the way, foo.cpp has #includes for both headers and so does main.cpp. bar.cpp only includes bar.h.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do you header files have include guards?
    Code:
    #ifndef FOO_H
    #define FOO_H
    
    // Header code goes here
    
    #endif

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    Unfortunately, I already put those in...
    I'll check to make sure I did it right, but I dont think that's the problem.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you define functions in the header(s) that were not declared inline?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    By inline, do you mean in the source code files? As in, did I put say
    void goodfunc(void);
    in the header but never mentioned it anywhere else?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sjweinberg
    By inline, do you mean in the source code files? As in, did I put say
    void goodfunc(void);
    in the header but never mentioned it anywhere else?
    No, I mean inline as in declared as inline with the inline keyword, or defined within a class definition.

    I suggest that you post the smallest and simplest program that demonstrates the errors. Post the errors as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do you have any global variables in your header files?

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    Well I have things like
    #define ON 1
    is that what you mean by a global variable?

    I also tried to recreate the error with some smaller code but couldn't. I'm really getting confused because I wanted to post code, but dont want to post code if it has no errors!!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sjweinberg
    Well I have things like
    #define ON 1
    is that what you mean by a global variable?
    No, that would be a macro. We are talking about something like this:
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    int my_global_variable = 0;
    
    #endif
    which you might fix as:
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    extern int my_global_variable;
    
    #endif
    then define the global variable in exactly one source file. Of course, this is assuming that you actually need the global variable in the first place.

    Quote Originally Posted by sjweinberg
    I also tried to recreate the error with some smaller code but couldn't. I'm really getting confused because I wanted to post code, but dont want to post code if it has no errors!!
    Well, the problem is that at the moment we are just guessing. In the worst case you may have to post your entire program, but such a large amount of code is hardly inviting for people to casually read and help you, unfortunately. On the other hand, your entire program might actually be rather small even for posting to a message board, if you have not yet progressed too far.
    Last edited by laserlight; 07-17-2009 at 02:23 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    Yeah, I'd like to avoid posting the entire program if possible. It's actually quite long, even if I cut out a lot to simplify. There are no global variables so that isn't the problem.

    Here is an example of the error I am getting:
    Code:
    test_support.o: In function `getip(int, char*)':
    /home/sjweinberg/Documents/VA software/automatedVA/va_test/src/test_support.cpp:13: multiple definition of `getip(int, char*)'
    ./tmp/test_support.o:/home/sjweinberg/Documents/VA software/automatedVA/va_test/src/test_support.cpp:13: first defined here
    I'll look through things. I I can't get anywhere, I'll try posting a simplified version of the code.

    Thanks for the patience!

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can you just post the header that defines the getip() function?

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    Sure! Here is the code with the comments taken out:

    Code:
    #ifndef TEST_SUPPORT_H
    #define TEST_SUPPORT_H
    
    #ifndef ON
    #define ON 0
    #endif
    
    #ifndef OFF
    #define OFF 1
    #endif
    
    #ifndef TRUE
    #define TRUE 1
    #endif
    
    #ifndef FALSE
    #define FALSE 0
    #endif
    
    #ifndef AFG
    #define AFG 10
    #endif
    
    #ifndef SCOPE
    #define SCOPE 20
    #endif
    
    void getip(int device, char *ip);
    
    int deviceCheck(char *device);
    
    void errhandle (int mode);
    
    #endif

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That header, as written, will not cause a problem.

    My guess is that one of your .cpp files is #include'ing another of your .cpp files, and needs to #include the .h file instead.

    Another possibility is that you have cut and pasted the implementations of getip() into two (or more) of your source files. You need to ensure it is only implemented once.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Where is the getip function defined? In a header? In a source file? What is the name of that file?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    12
    getip is defined in a file called test_support.cpp.

    Here is that file:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #include <iostream>
    #include <fstream>
    #include <fcntl.h>
    #include <cstdlib>
    
    #include "../../tek_1.03/library/tek_user.h"
    #include "../include/test_support.h"
    
    void getip(int device, char *ip)
    {
      FILE *inp = fopen("deviceInfo.txt","r");
      char tmp[100];
      char devicestr[10];
      int eofdetect;
      int stringCompare;
      
      switch (device) {
      case SCOPE:
        strcpy(devicestr,"SCOPE");
        break;
      case AFG:
        strcpy(devicestr,"AFG");
        break;
      default:
        errhandle(20);
      }
      
      fscanf(inp,"%s",tmp);
      if (strcmp(tmp,"instruments:") != 0)
        errhandle(1);
      
      eofdetect = fscanf(inp,"%s",tmp);
      stringCompare = strcmp(tmp,devicestr);
      while (stringCompare != 0 && eofdetect != EOF && strcmp(tmp,"END") != 0) {
        eofdetect = fscanf(inp,"%s",tmp);
        stringCompare = strcmp(tmp,devicestr);
      }
      
      if (stringCompare == 0) {
        eofdetect = fscanf(inp,"%s",tmp);
        if (eofdetect != EOF)
          strcpy(ip,tmp);
      }
      else
        errhandle(25);
    
      fclose(inp);
    }
    
    
    int deviceCheck(char *device)
    {
      FILE *inp = fopen("deviceInfo.txt","r");
      char tmp[100];
      int eofdetect;
      int stringCompare;
      int usedevice = FALSE;
    
      
      
      fscanf(inp,"%s",tmp);
      if (strcmp(tmp,"instruments:") != 0)
        errhandle(1);
      
      eofdetect = fscanf(inp,"%s",tmp);
      stringCompare = strcmp(tmp,device);
      while (stringCompare != 0 && eofdetect != EOF && strcmp(tmp,"END") != 0) {
        eofdetect = fscanf(inp,"%s",tmp);
        stringCompare = strcmp(tmp,device);
      }
      
      if (stringCompare == 0)
        usedevice = TRUE;
      else if(strcmp(tmp,"END") == 0)
        usedevice = FALSE;
      else
        errhandle(1);
      
      fclose(inp);
    
      return usedevice;
    }
    
    void errhandle (int mode)
    {
      switch (mode) {
    // A lot of cases that Ive taken out to shorten this.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Multiple header files , cout undelcared probem
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2005, 10:15 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Conflicting Header Files
    By X PaYnE X in forum Windows Programming
    Replies: 17
    Last Post: 01-08-2004, 11:28 AM