仅在为模拟器构建时才复制符号 [英] Duplicate Symbols only when building for simlator

查看:71
本文介绍了仅在为模拟器构建时才复制符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为设备(ipad 3)进行构建时,我的构建工作没有警告或错误,但是在为iPad或iPhone模拟器进行构建时,我收到如下链接错误:

When building for a device (ipad 3) my build works find with no warnings or errors, but when building for the iPad or iPhone simulator I receive linker errors like the following:

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

常量的定义类似于头文件中的

The constants are defined like so in header files

const int CONSTANT_NAME = 123;

我已经尝试将常量的内容包装在#define标签中,如下所示:

I have tried wrapping the constant's in #define tag's like so:

#ifndef CONSTANTS_H
#define CONSTANTS_H

const int CONSTANT_NAME = 123;

#endif

为什么在为设备构建时可以正常工作,但在为模拟器构建时会导致问题?

why does this work fine when building for device but cause issues when building for simulator?

推荐答案

编译器会告诉您完全正确的信息.您很幸运,直接构建到iPad时不会发生这种情况.

The compiler is telling you exactly the correct thing. You are lucky that it's not happening when building to your iPad directly.

在每个包含此标头的.m文件中,您将创建一个具有相同名称的新变量.将所有这些文件链接到单个.a中时,编译器可以解决此问题,但是当构建多个.a文件并将这些多个.a文件链接在一起时,编译器将对重复副本进行编译.

In every .m file where you include this header, you create a new and distinct variable with the same name. The compiler can resolve this when linking all these files into a single .a, but when multiple .a files are built and those multiple .a files are linked together, the compiler compiles about duplicate copies.

我会做以下三件事之一:

I would do one of three things:

  1. const int转换为#define. #define CONSTANT_NAME 123
  2. const int之前添加静态. static const int CONSTANT_NAME = 123;
  3. const int之前添加extern并将实数const int添加到单个.m.在.h中,extern const int CONSTANT_NAME;.在单个.m中,const int CONSTANT_NAME = 123;.
  1. Turn the const int into a #define. #define CONSTANT_NAME 123
  2. Add static before const int. static const int CONSTANT_NAME = 123;
  3. Add extern before const int and add the real const int to a single .m. In .h, extern const int CONSTANT_NAME;. In the single .m, const int CONSTANT_NAME = 123;.

对于最后一个,我将创建一个constants.m文件作为保存const int CONSTANT_NAME = 123;定义的单独位置.

For the last one, I would create a constants.m file as a separate place to hold the const int CONSTANT_NAME = 123; definition.

希望有帮助.

这篇关于仅在为模拟器构建时才复制符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆