Thread: Linking problem

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Linking problem

    First I'd like to point out that the structure is kinda bad, so if you wanna flame me for it please don't .

    Anyway, consider 3 files:

    a.h:
    Code:
    #ifndef A_H
    #define A_H
    
    void Func()
    {
       ...
    }
    
    #endif
    (yes, that's a function definition in the header!)

    b.cpp:
    Code:
    #include "a.h"
    c.cpp:
    Code:
    #include "a.h"
    b.cpp and c.cpp are compiled as separate modules then linked together. Now, even with inclusion guards in a.h the function will be defined in both files thus causing conflicts when linking b and c together.

    I know I have a definition in the header file which I in all other cases would NOT do, but in this particular scenario it is required.

    Is this solveable, to only get it defined once in one module? Or solve it in any other way? Since the modules are separately compiled the inclusion guards doesn't work well.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ...but in this particular scenario it is required.
    Why are you "required" to do something you're not supposed to do?

    Make it "inline void Func()".

    gg

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm making a header for a DLL I'm working on, and it needs a setup and a shutdown function. I wanted the user to simply include the header to get access to those functions without having to include some lib or cpp file. Simplicity.

    Inline seemed to work, however I forgot to tell I had a few globals too which are still defined multiple times.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Get rid of the globals or put em in a cpp file.

    gg

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I solved this by using a class:
    a.h:
    Code:
    #ifndef A_H
    #define A_H
    
    class AA
    {
       void Func()
       {
          ...
       }
    
       int Data;
    };
    
    #endif
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linking files
    By slippy in forum C Programming
    Replies: 2
    Last Post: 11-23-2007, 11:35 PM
  2. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 03:03 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. Linking problem...
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 04:13 PM