Thread: DevCpp error: multiple definition of `img'

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    DevCpp error: multiple definition of `img'

    When I am trying to compile my project, DevCpp says that variable img is defined multiple times. But I have only defined it at one place; in one of my header files. That header file is included by two different source files, but that shouldn't be any problem, should it? And the header file is protected against double including.

    DevCpp does make .o-files, but it doesn't produce any .exe-file. Is it possible that img is in two of the .o-files and that it colides when it tries two put the different .o-files together? How do I solve it?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's a bit hard to spot the error without code, but I'd say you might have declared a variable in the header file which is wrong. Well, partially wrong. You need to use the 'extern' keyword before declaring a variable in a header file and then you have to redeclare it in its proper source file.

    Ex:

    file.h

    Code:
    #ifndef FILE_H_INCLUDED
    #define FILE_H_INCLUDED
    
    extern MyFooClass my_class;
    
    #endif // FILE_H_INCLUDED
    file.cpp

    Code:
    #include "file.h"
    
    MyFooClass my_class;
    Edit: No executable file is produced when there is a compile-time error.

  3. #3
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Disclaimer: The following advice is most likely useless.

    Are you using guards in your headers?

    Code:
    #ifndef __HEADERNAME_HPP__
    #define __HEADERNAME_HPP__
    
    // Code
    
    #endif
    If you don't then your program is going to try to compile the header each time it is included.

    http://www.fredosaurus.com/notes-cpp...sor/ifdef.html
    Last edited by rynoon; 01-01-2007 at 07:22 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    He/she specified that the header is "protected against double including". It suggests that he/she is either using the #pragma once instruction, or the #ifndef.. as I did in my code and as you did as well.

  5. #5
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by Desolation
    He/she specified that the header is "protected against double including". It suggests that he/she is either using the #pragma once instruction, or the #ifndef.. as I did in my code and as you did as well.
    Excuse me. Apparently I can no longer read.

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    The header image.h where the variabel is declared:

    Code:
    #ifndef IMAGE_H
    #define IMAGE_H
    
    /****************************************************************
     *  SHARED VARIABLES
     ****************************************************************/
    
    extern SDL_Surface *img; // <--------------------------------- Variable here
    
    /****************************************************************
     *  EXPORTED FUNCTIONS
     ****************************************************************/
     
    void UpdateImage();
    
    #endif //IMAGE_H
    One of the source files inluding image.h (image.cpp):

    Code:
    /****************************************************************
     *  INCLUDES
     ****************************************************************/
    
    #include <cstdio>
    #include <cstdlib>
    #include "SDL/SDL.h"
    #include <string>
    #include <cmath>
    using namespace std;
    
    #include "wavetest.h"
    #include "image.h"
    
    /****************************************************************
     *  MACROS
     ****************************************************************/
    
    #define  PIXEL(s, x, y)  ((Uint32 *)(s)->pixels)[(x) + (y)*(s)->w]
    
    /****************************************************************
     *  VARIABLE DECLARATION
     ****************************************************************/
    
    SDL_Surface *img;  // <--------------------------------- Variable here
    
    /****************************************************************
     *  NOT EXPORTED FUNCTIONS
     ****************************************************************/
    
    /****************************************************************
     *  EXPORTED FUNCTIONS
     ****************************************************************/
     
    void UpdateImage() {
        int x, y;
        for (y = 0; y < img->w; y++) {
            for (x = 0; x < img->h; x++) {
                PIXEL(img, x, y) = rand()&(1<<(24-1));
            }
        }
    }
    Another one of them (main.cpp):

    Code:
    /****************************************************************
     *  INCLUDES
     ****************************************************************/
    
    #include <cstdio>
    #include <cstdlib>
    #include "SDL/SDL.h"
    #include <string>
    using namespace std;
    
    #include "wavetest.h"
    #include "main.h"
    #include "SDL_functions.h"
    #include "image.h"
    
    /****************************************************************
     *  VARIABLE DECLARATION
     ****************************************************************/
     
    const int IMG_WIDTH = 640;
    const int IMG_HEIGHT = 480;
    const int IMG_BPP = 32;
    SDL_Surface *img = 0;  // <--------------------------------- Variable here
    
    /****************************************************************
     *  NOT EXPORTED FUNCTIONS
     ****************************************************************/
     
    int main(int argc, char* args[]) {
        
        InitSDL(img, IMG_WIDTH, IMG_HEIGHT, IMG_BPP, "Wavetest");
        do {
        } while(1);
        QuitSDL();
    }
    
    /****************************************************************
     *  EXPORTED FUNCTIONS
     ****************************************************************/
    I tried to use the extern keyword but it didn't matter.
    And if there is something bad or uggly in the code, it's no big deal, I'm only doing this as a hobby anyway.
    Last edited by TriKri; 01-01-2007 at 07:42 PM.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    from what i gather i think your making some kind of media player, and ye, them comments are wayyyyyy big lol, makes the code a little hard to read tbh.

    what exactly does the code do? just display a picture and the likes.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    You really define the variable twice. Remove the definition inside of main.cpp and the error should go away.

  9. #9
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thank you, it worked! What I understand, I got to have the extern-declaration in the header file, and then exactly one time in a source file? Ok, good...

    What I am doing? I am not doing a media player, Hahaha!. No, that is to hard for me. Actually I am doing a program that is going to simulate real waves in a plane. The plane consists of many squares and each square has a density (which creates a pressure) and a velocity. By adding some density to any of the squares, you can create a blast wave. Pretty cool, I have done a similar before, but at that time I was programming in VB and unsing the REALLY slow built-in Line function, so I had to make each squares 5 x 5 pixels and yet it ran really slow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM