Xcode Build Settings中No Common Blocks配置项

这个配置从XCode8开始被默认开启,有时会导致"duplicate symbol for architecture xxx" 错误,那么这个配置项的含义是什么呢?

从Xcode 的快速帮助中可以看到下面内容:

In C, allocate even uninitialized global variables in the data section of the object file, rather than generating them as common blocks. This has the effect that if the same variable is declared (without extern ) in two different compilations, you will get an error when you link them. The only reason this might be useful is if you wish to verify that the program will work on other systems which always work this way。

No Common Blocks的含义是,所有未初始化的全局变量不以COMMON块的机制来处理。
那么什么是COMMON块机制?
参考《程序员的自我修养》中第4.3节的解释,大概理解为,对于不同目标文件中未初始化的全局变量,根据COMMON块机制,原则上讲最终链接后输出文件中,其声明空间大小以目标文件中最大的那个为准。

如果将No Common Blocks设置为YES后,所有未初始化的全局变量则不以COMMON块的形式处理,一旦一个未初始化的全局变量不是以COMMON块的形式存在,其就相当于一个强符号,如果其他目标文件中还有一个同样名称的全局变量,根据链接时不允许强符号被多次定义的规则,链接时就会发生符号重定义的错误。

参考:
What is GCC_NO_COMMON_BLOCKS used for?

你可能感兴趣的:(Xcode Build Settings中No Common Blocks配置项)