Duplicate symbol puzzle

  • Today I encountered a strange error in a Cocoa app with Xcode 3.1.4
    targeting Leopard.

    Most of the source files in this project are .mm but a few are .m since they
    do not reference any STL-type classes.

    The error I got was a load error for duplicate symbol _LOGFILE.  LOGFILE is
    a defined constant in a header, Flags.h.

    const flagType LOGFILE = 0x00000001ULL;

    The "duplication" was between two .m files both of which #imported Flags.h
    which, in fact, is included in most of my source files.

    I "fixed" the error simply by renaming one of the files with the duplicate
    symbol to .mm.

    I cannot understand this error since i) it was my impression that #import
    included headers only once and ii) the fix seems that it should be
    irrelevant.

    FWIW, Flags.h is also protected by a #ifndef construct as well.  Also,
    cleaning and rebuilding the project was ineffective.

    So what am I missing?

    --
    Mike McLaughlin
  • On May 20, 2010, at 8:22 AM, McLaughlin, Michael P. wrote:

    > const flagType LOGFILE = 0x00000001ULL;
    >
    > The "duplication" was between two .m files both of which #imported Flags.h
    > which, in fact, is included in most of my source files.

    C doesn’t implement ‘const’ very well.

    In C++ the above declaration would just define a compile-time constant similar to an enum value. But in C the semantics are just like declaring a regular global variable, except that it’s illegal to change its value. And since you put this in a header, you get the same duplication problem you get if you declare any global in a header without using ‘extern’.

    The best way to fix it is to use an enum:
    enum {
      LOGFILE = 0x00000001ULL;
    }
    Or you can use Objective-C++ by changing your source files’ extensions to “.mm”.

    —Jens _______________________________________________
    Do not post admin requests to the list. They will be ignored.
    Xcode-users mailing list      (<Xcode-users...>)
    Help/Unsubscribe/Update your Subscription:
    http://lists.apple.com/mailman/options/xcode-users/<xcode...>

    This email sent to <xcode...>
  • On Thu, 20 May 2010 09:38:18 -0700, Jens Alfke said:

    > C doesn't implement 'const' very well.
    >
    > In C++ the above declaration would just define a compile-time constant
    > similar to an enum value. But in C the semantics are just like declaring
    > a regular global variable, except that it's illegal to change its value.
    > And since you put this in a header, you get the same duplication problem
    > you get if you declare any global in a header without using 'extern'.
    >
    > The best way to fix it is to use an enum:
    > enum {
    > LOGFILE = 0x00000001ULL;
    > }
    > Or you can use Objective-C++ by changing your source files' extensions
    > to ".mm".

    I would advise against switching to Obj-C++ just for that.  The dev
    tool's support for C++ is not as good as C/Obj-C. You'll loose the
    ability to Refactor and use the static analyzer, amongst other things.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • Am 20.05.2010 um 17:22 schrieb McLaughlin, Michael P.:

    > i) it was my impression that #import included headers only once

    Once per source file.

    > ii) the fix seems that it should be irrelevant.

    Your "fix" switches from using C to using C++ as the base language
    for Objective-XXX. C doesn't allow multiple global variables of the
    same name and neither attempts to merge them at link time.

    Markus

    - - - - - - - - - - - - - - - - - - -
    Dipl. Ing. (FH) Markus Hitter
    http://www.jump-ing.de/
previous month may 2010 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today