Two options:
static const int thisIsGlobal = 123;
or
#define thisIsGlobal 123
Two options:
static const int thisIsGlobal = 123;
or
#define thisIsGlobal 123
What you can do is put in your header (MyConstants.h
):
extern const int MyConstant;
extern NSString * const MyStringConstant;
And in a source file, include the header above but define the constants (MyConstants.m
):
const int MyConstant = 123;
NSString * const MyStringConstant = @"SomeString";
Then, you simply need to include the header in any other source file that uses either of these constants. The header is simply declaring that these constants exist somewhere, so the compiler won't complain, because it's the linker's job to resolve these constant names. The source file that contains your constant definitions gets compiled, and the linker sees that this is where the constants are, and resolves all of the references found in the other source files.
The problem with declaring and defining a constant in a header (that is not declared as static
) is that the compiler treats it as an independent global for each file that includes that header. When the linker tries to link all of your compiled sources together it encounters the global name as many times as you have included MyConstants.h
.
This is because the symbol name in question (thisIsGlobal) is being emitted into every object file created, where the header containing the declaration for thisIsGlobal is included and visible.
The examples provided by another poster: 'extern const int MyConstant;' is the best way, unless you need the value to be visible, in which case you can use an enum:
int thisIsGlobal = 123; // bad
enum { thisIsGlobal = 123 }; // ok
using static will emit a lot of hidden symbols in a large program -- don't use it. Using a define is scary as well (considering there are safer alternatives available, why not use them?).
I usually put my application constants file in the Xcode project's MyApplication_Prefix.pch
file, usually located within 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 would then no longer need to include your MyConstants.h
file from every file in your project — it will be included automatically.