Duplicate characters only when created for simlator

When I create for the device (ipad 3), my build work is found without warning or error, but when I create it for the iPad or iPhone simulator, I get linker errors, such as:

duplicate symbol _CONSTANT_NAME in: /Users/me/libLibrary.a(FileName.o) /Users/me/libOtherLibrary.a(OtherFileName.o) 

Constants are also defined in header files.

 const int CONSTANT_NAME = 123; 

I tried to wrap the constant in the #define tag like this:

 #ifndef CONSTANTS_H #define CONSTANTS_H const int CONSTANT_NAME = 123; #endif 

Why does this work great when creating a device, but causing problems when creating a simulator?

+7
source share
1 answer

The compiler tells you the exact thing. You are fortunate that this does not happen with the direct creation of the iPad.

In each .m file where you include this header, you create a new and separate variable with the same name. The compiler can solve this problem by linking all of these files into one .a, but when multiple .a files are created and these several .a files are linked together, the compiler compiles duplicates.

I would do one of three things:

  • Turn const int to #define . #define CONSTANT_NAME 123
  • Add static to const int . static const int CONSTANT_NAME = 123;
  • Add extern to const int and add the real const int to one .m. In .h, extern const int CONSTANT_NAME; . In single .m, const int CONSTANT_NAME = 123; .

For the latter, I will create the file constants.m as a separate place for storing the definition of const int CONSTANT_NAME = 123; .

Hope this helps.

+13
source

All Articles