CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2006
    Posts
    42

    multiple definition

    Hi,
    I have the following problem: I'm using an external (as in: not under my control) library C, that is implemented completely inside of header-files. I need to include those headers both in a library B and in an application A. B is also linked into A, so the functions of C are linked into A twice: once by its own includes and once with B, resulting in a "multiple definition" error.
    Is there an elegant solution to this problem without the need to modify C?

    Thanks in advance

  2. #2
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: multiple definition

    put this both places where you include your c-library. this is by no means elegant, but it'll work.
    Code:
    #if !defined(_include_once_)
    #    include "c-library.h"
    #    define _include_once_
    #endif

  3. #3
    Join Date
    Jun 2006
    Posts
    42

    Re: multiple definition

    Thanks, but that doesn't help, because it only prevents including multiple times inside one "translation unit", it doesn't prevent two "translation units", in this case A and B, from containing the same functions.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: multiple definition

    I don't think there is an easy way out on this since whatever you do the functions from C end up in B lib & A obj.

    The best solution I can come up with is that you extract prototype information from C into a new header that you use in A (omit including the original C headers). Hopefully this is C++ so if C is updated and the new signatures differ the linker will catch it.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: multiple definition

    If the implementation is "inlined" that should be no problem.

    If for reasons best known to themselves they have put implementation into the header files treating them as source, then so should you.

    Copy just the prototypes into your header and use inclusion guards for it.

    Have a compilation unit that includes their headers and does nothing else. Everything else includes yours not theirs.

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: multiple definition

    I am not sure but wasn't the extern C++ keyword provided exactly for this type of situations.
    If there is no love sun won't shine

  7. #7
    Join Date
    Jun 2006
    Posts
    42

    Re: multiple definition

    Thanks to you two, this seems like a good suggestion. I'll try that.

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: multiple definition

    extern is on by default for function prototypes and off by definition for global variables.

    The inline keyword can be used for free-functions that are implemented in header files to ensure they are linked only once. It does not actually guarantee that the compiler will inline them.

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

Featured