Results 1 to 8 of 8

Thread: already defined in *.obj

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question already defined in *.obj

    How can I prevent these link errors, besides keeping all variables and functions in main.cpp?
    Code:
    main.obj : error LNK2005: "struct HINSTANCE__ * hInst" (?hInst@@3PAUHINSTANCE__@@A) already defined in button.obj
    main.obj : error LNK2005: "struct HWND__ * hWndCmd1" (?hWndCmd1@@3PAUHWND__@@A) already defined in button.obj
    main.obj : error LNK2005: "struct HFONT__ * hFont" (?hFont@@3PAUHFONT__@@A) already defined in button.obj
    Release/test.exe : fatal error LNK1169: one or more multiply defined symbols found
    VC++6.0 project attached...
    Attached Files Attached Files
    Last edited by wey97; Aug 19th, 2002 at 08:03 PM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    The reasons I do not want to keep everything in main.cpp:

    1) It is ugly code

    2) I have a large project and I like to build incrementally (I think that's right), i.e. the large main.obj does not have to be rebuilt every time small changes are made, only a smaller .obj file.

    Is it a good idea to have global variables and function prototypes in a header file, have function definitions in other cpp files, and pass the globals by reference to the functions from the window procedure?

    What is the optimal solution?

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Try formatting your header files like this:

    PHP Code:
    //File: myheader.h
    #ifndef MYHEADER_H
    #define MYHEADER_H
    //.....
    //Header file contents...
    //.....
    #endif //End of file 
    Baaaaaaaaah

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Thanks, but the header's like this in the project above:
    Code:
    #ifndef __HEADER_H__
    #define __HEADER_H__
    
    //  header file contents
    
    #endif // __HEADER_H__
    BTW: what is the difference in __HEADER_H__ (the way I've seen it) and HEADER_H ?

    It doesn't make a difference either way.
    Still errors.
    Last edited by wey97; Aug 20th, 2002 at 12:11 AM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Aha

    The variables can be declared static so only one instance of them will be created. This seems to work.
    Will this cause any problems?
    I've never seen a project with all globals declared static.

  6. #6
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    The problem is that you have to define the global variables in at least one of the .cpp files!! Otherwise, they do not exist! Also, you might wanna use the "extern" keyword to let the compiler know that ur not declaring it in the h file!

    I would just copy the variables into the main.cpp and stick extern in front of the header.h ones.

    Hope that helps,
    MoMad the NoMad
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    That seems to work rather well.
    I see your point - declare globals in .h and define in .cpp similar to function declarations and definitions.

    thanks!
    Last edited by wey97; Aug 20th, 2002 at 08:40 AM.

  8. #8
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    A rather better and cleaner sollution is to put all of ur stuff in classes. It will save you alot of confusion.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width