#import still gets duplicate character error

When I compile my iPhone application, xCode gives a "duplicate symbol" error for my variables in MyConstants.h

I thought that if I used:

#import "MyConstants.h" 

would he avoid this?

But I still have a problem.

Information added:

An error occurs during "binding." (I just use the "Build and Go" xCode button.)

I also tried (optionally C # import) # ifndef / def method too.

Maybe I should just ask about this:

If you need to access a constant in EVERY part of ALL source code files ... what would you put in your .h file? What would you use to include this constant in other parts of your code.

I thought (but I think it’s not) was simple:

MyConstants.h> int thisIsGlobal = 123;

(No, where I redefine thisIsGlobal anywhere in any code.)

And then just β€œ#import MyConstants.h” at the top of each of my other source files.

+10
c import iphone xcode compiler-errors
May 02 '10 at 17:16
source share
5 answers

What you can do is put in your header ( MyConstants.h ):

 extern const int MyConstant; extern NSString * const MyStringConstant; 

And in the source file, include the header above, but define the constants ( MyConstants.m ):

 const int MyConstant = 123; NSString * const MyStringConstant = @"SomeString"; 

Then you just need to include the header in any other source file that uses any of these constants. The header simply declares that these constants exist somewhere, so the compiler will not complain, because it is the job of the linker to resolve these constant names. The source file containing your constant definitions is compiled, and the linker sees that these are constants, and resolves all the links found in other source files.

The problem with declaring and defining a constant in the header (which is not declared as static ) is that the compiler treats it as an independent global for every file that includes this header. When the linker tries to link all compiled sources together, it encounters the global name as many times as you included MyConstants.h .

+19
May 03, '10 at 0:19
source share

Two options:

 static const int thisIsGlobal = 123; 

or

 #define thisIsGlobal 123 
+8
May 2 '10 at 23:58
source share

I use this and it works: (in .h outside of @interface)

 static NSString * const mkLocaleIdentifierUS = @"en_US"; static NSString * const mkLocaleUserSystemSettings = nil; 
+5
Jul 19 2018-11-17T00:
source share

This is because the specified symbol name (thisIsGlobal) is emitted into each created object file, where the header containing the declaration for this IsGlobal is included and visible.

Examples provided by another poster: "extern const int MyConstant;" this is the best way if you don't want the value to be visible, in which case you can use an enumeration:

int thisIsGlobal = 123; // bad

enum {thisIsGlobal = 123}; // ok

using a static system, it produces a lot of hidden characters in a large program - do not use it. Using a definition is also scary (given the availability of safer alternatives, why not use them?).

+3
May 04 '10 at 7:01
source share

I usually put my application constant file in the MyApplication_Prefix.pch Xcode project file, usually located in the Other Sources group. Any header file included in this pch file will be included from all files in your project.

After adding this include statement, you will no longer need to include your MyConstants.h file from each file in your project - it will be included automatically.

+2
May 04 '10 at 7:06 a.m.
source share



All Articles