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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/adc%40jeremyp.net

This email sent to [email protected]

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to