• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why am I always getting the linker error: 'duplicate symbol'...?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why am I always getting the linker error: 'duplicate symbol'...?


  • Subject: Re: Why am I always getting the linker error: 'duplicate symbol'...?
  • From: Jeremy Pereira <email@hidden>
  • Date: Wed, 23 Sep 2009 11:24:11 +0100


On 21 Sep 2009, at 21:19, Frederick C. Lee wrote:

I'm trying to replace #define directives with c datatype directives.Example:

(1) #define x 123

versus...

(2) const unsigned x = 123;

I place this at the top of the .h (or .m) file, outside of any class or
method as stand alone.


However, every time I try to use the (2) declaration (within a .h file) the
linker gives the error: 'duplicate symbol _x in ....'


I tried placing the (2) declaration at the top of a .m or .c file, but I get
the same linker error.


I've declared global constants in another application without a problem.

I've checked the compiler type within the build: gcc 4.2

What am I doing wrong?

You should define the constant in ONE .m file e.g.

foo.m
----
const unsigned int x = 123;
----

and declare it extern in a header file e.g.

foo.h
---
extern const unsigned int x;
---

This is basic C usage but it often confuses people. Back in the 90's I came across a bug where somebody had done exactly the opposite (with a variable, not a constant) i.e. put the extern declaration in every C file and the definition in a header file and the linker was too primitive to complain about the duplicate symbols (or it only emitted a warning that was being ignored). When the code was running, different C functions chose different seemingly random versions of the variable to read/write.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Why am I always getting the linker error: 'duplicate symbol'...? (From: "Frederick C. Lee" <email@hidden>)

  • Prev by Date: Re: Suppressing Crash Reporter dialogs for a task
  • Next by Date: Re: building and running on Snow Leopard
  • Previous by thread: Re: Why am I always getting the linker error: 'duplicate symbol'...?
  • Next by thread: Re: Why am I always getting the linker error: 'duplicate symbol'...?
  • Index(es):
    • Date
    • Thread