Header /file module issues

This is a discussion on Header /file module issues within the C++ Programming forums, part of the General Programming Boards category; i am presently breaking a program down into seperate source files and writing some small header files also, everything has ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    Header /file module issues

    i am presently breaking a program down into seperate source files and writing some small header files also, everything has gone swimmingly but now i am hitting problems with the below..i am sure other problems of this sort will come up as i continue so id appreciate any advice...

    this opening entry of my Graphics.h file works fine included in main and everything in there seems to use it fine, but when i include it in other source files i get a list of errors saying each element is being redefined..first defined here..etc.. even when the source file does not so far use anything in the header. on the other hand other headers dont give me any problem being put in more than one source, ...but then they only contain class definitions

    Code:
    #ifndef GRAPHICS_H_INCLUDED
    #define GRAPHICS_H_INCLUDED
    
    
     SDL_Surface *screen;
     SDL_Surface *temp;
     SDL_Surface *PegArea;
     SDL_Surface *Buttonarea;
     SDL_Surface *Gameboard;
    
    
    #endif // GRAPHICS_H_INCLUDED

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    if you are having redefinition problem then you must use extern

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    unfortunately that doubled the number of errors:-( i dunno whats going on, will have to read up more as this is new to me.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    can you post some errors dude here

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    cheers... heres the build log...Graphics.h header to a working source file 'Buttonfunctions.cpp' to generate the errors

    In function `_ZN10MenuButton11InitButtonsEv':|
    C:\Program Files\CodeBlocks\My Programs\ButtonFunctions.cpp|10|multiple definition of `_screen'|
    C:\Program Files\CodeBlocks\My Programs\New SDL Project\.objs\SDLmain.o:C:\Program Files\CodeBlocks\My Programs\SDLmain.cpp|22|first defined here|

    objs\ButtonFunctions.o||In function `_ZN10MenuButton11InitButtonsEv':|
    C:\Program Files\CodeBlocks\My Programs\ButtonFunctions.cpp|10|multiple definition of `_temp'|
    C:\Program Files\CodeBlocks\My Programs\New SDL Project\.objs\SDLmain.o:C:\Program Files\CodeBlocks\My Programs\SDLmain.cpp|22|first defined here|

    .objs\ButtonFunctions.o||In function `_ZN10MenuButton11InitButtonsEv':|
    C:\Program Files\CodeBlocks\My Programs\ButtonFunctions.cpp|11|multiple definition of `_PegArea'|
    C:\Program Files\CodeBlocks\My Programs\New SDL Project\.objs\SDLmain.o:C:\Program Files\CodeBlocks\My Programs\SDLmain.cpp|23|first defined here|

    \.objs\ButtonFunctions.o||In function `_ZN10MenuButton11InitButtonsEv':|
    C:\Program Files\CodeBlocks\My Programs\ButtonFunctions.cpp|11|multiple definition of `_Buttonarea'|
    C:\Program Files\CodeBlocks\My Programs\New SDL Project\.objs\SDLmain.o:C:\Program Files\CodeBlocks\My Programs\SDLmain.cpp|23|first defined here|

    \.objs\ButtonFunctions.o||In function `_ZN10MenuButton11InitButtonsEv':|
    C:\Program Files\CodeBlocks\My Programs\ButtonFunctions.cpp|12|multiple definition of `_Gameboard'|
    C:\Program Files\CodeBlocks\My Programs\New SDL Project\.objs\SDLmain.o:C:\Program Files\CodeBlocks\My Programs\SDLmain.cpp|24|first defined here|

    ||=== Build finished: 10 errors, 0 warnings ===|
    and i dont get the '`_ZN10MenuButton11InitButtonsEv' bit, whats the _ZN10 all about, and 'Ev' at end? i the 'MenuButton' is a class
    and 'InitButtons is a member funtion, this member function is the only thing in the source file 'Buttonfunctions.cpp' and does not call anything from Graphics.h
    Last edited by rogster001; 10-21-2009 at 05:31 AM.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    i just saw that my obj files are being created in the folder 'New SDL Project' i dont know why that is, i think that needs sorting, they should be in my actual project folder somewhere right? i havent told the IDE to put them in NEW SDL_Project, but then i havent told it not to either...sigh

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    i tried to explain this problem what i am getting by a simple example

    test.h
    Code:
    #ifndef INC_TEST_H_
    #define INC_TEST_H_
    
    #include <stdio.h>
    
    int global_variable;
    
    void show_test();
    
    #endif // INC_TEST_H_

    test.c
    Code:
    #include "test.h"
    
    global_variable = 10;
    
    void show_test() {
      printf("global_variable == %d\n", global_variable);
    }

    test1.h
    Code:
    #ifndef INC_TEST1_H_
    #define INC_TEST1_H_
    
    #include "test.h"
    
    void show_test2();
    
    #endif // INC_TEST1_H_

    test1.c
    Code:
    #include "test1.h"
    
    global_variable = 20;
    
    void show_test2() {
      printf("global_variable == %d\n", global_variable);
    }

    main.c
    Code:
    #include "test.h"
    #include "test1.h"
    
    int main() {
      show_test();
      show_test2();
      return 0;
    }

    while building

    [rocky@localhost test]$ gcc test.c test1.c main.c
    test.c:3: warning: data definition has no type or storage class
    test1.c:3: warning: data definition has no type or storage class
    test1.c: In function ‘show_test2’:
    test1.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
    /tmp/ccisu68m.o.data+0x0): multiple definition of `global_variable'
    /tmp/ccCehBee.o.data+0x0): first defined here
    collect2: ld returned 1 exit status

    what i am saying is that


    test.h
    Code:
    #ifndef INC_TEST_H_
    #define INC_TEST_H_
    
    #include <stdio.h>
    
    extern int global_variable;
    
    void show_test();
    
    #endif // INC_TEST_H_

    test1.c
    Code:
    #include "test1.h"
    
    //global_variable = 20;
    
    void show_test2() {
      printf("global_variable == %d\n", global_variable);
    }

    By this you can have one defination of your variables which is declared in your graphics.h
    I hope you understand what i try to say

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    well i not sure if i understand entirely what you are showing...i did however delete the include graphics.h from the test file and name the variables as 'extern' in the test file and the functions therein now recognise them,, thanks for your advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. u_short header?
    By NuNn in forum C Programming
    Replies: 4
    Last Post: 02-19-2009, 11:07 AM
  2. Precompiled header question
    By donglee in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2009, 12:23 AM
  3. IP header "total length" vs. packet size
    By MK27 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-04-2009, 06:45 AM
  4. Many Header Files
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2005, 01:45 PM
  5. Diff between Header file and Module
    By john212 in forum C Programming
    Replies: 4
    Last Post: 12-09-2002, 02:13 AM

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