C 中的函数名称范围
函数名称作用域如何跨多个 C 文件工作?
我正在将标准 gnu 工具链项目移植到 iPhone 操作系统,并使用 Xcode 来完成它。
代码是通过 make 构建的,而不是通过 xcode 构建的。 当通过 xcode 构建时,链接器抱怨两个对象中定义了相同的符号(函数)。 该代码有两个不同的源文件,#include
它们之间有一个公共文件。 虽然……很奇怪(至少对我来说),但它似乎适用于标准工具链。 如果这是通过标准 makefile 以某种方式以不同方式处理的东西,有什么想法吗?
How does function name scoping work across multiple C files?
I'm porting a standard gnu toolchain project to iPhone OS, and using Xcode to do it.
The code builds through make, but not through xcode. when building through xcode, the linker complains that the same symbol (function) is defined in two objects. the code has two distinct source files that #include
a common file between them. While... odd (to me at least), it seems to work for the standard toolchain. any ideas if this is something that's somehow handled differently through a standard makefile?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有未标记为静态的函数都具有全局作用域(它们被转储到单个命名空间中)。 标记为静态的函数仅限于定义它们的翻译单元。
您违反了“单一定义规则”。
您的标头之一可能有变量的定义。 例如:在
common.h
中,您将其更改为:
然后创建一个
common.c
文件,在其中放置定义:All functions not marked static have global scope (they are dumped in a single namespace). Functions marked static are limited to the translation unit they are defined in.
You have a One Definition Rule violation.
One of your headers probably has a definition for a variable. E.g: in
common.h
you havechange it to:
and then create a
common.c
file where you put a definition:在 C 中,如果我没记错的话,
static
函数名称对于定义它们的源文件来说是本地的,但所有其他函数名称都存在于全局命名空间中。 因此,如果您有file1.c
和
file2.c
并且您尝试使用类似的内容编译它们
,那么您会在
fn1< 之间出现名称冲突
file1.c
中的 /code> 和file2.c
中的fn1
,但不在两个fn2
函数之间(因为它们是静态的)。 (当然,您还会遇到一堆其他错误,因为该程序不执行任何操作,但这些错误与范围界定无关。)In C, if I remember correctly,
static
function names are local to the source file in which they are defined, but all other function names exist in a global namespace. So if you havefile1.c
withand
file2.c
withand you tried to compile them with something like
then you would get a name conflict between the
fn1
infile1.c
and thefn1
infile2.c
, but not between the twofn2
functions (because they're static). (Of course, you'd get a bunch of other errors too because this program doesn't do anything, but those aren't relevant to scoping.)如果从 Makefile 编译没有错误,但从 XCode 项目编译没有错误,则很可能是因为编译器选项在两个环境中设置不同。 检查 Makefile 以查看传递给“gcc”的选项。 特别是,重复的定义可能是使用 #ifdef 进行条件编译的,并且您可能需要将一些预处理器定义添加到 XCode 项目的设置中。
If it compiles without errors from the Makefile, but not from your XCode project, it is most likely because the compiler's options are being set differently in the two environments. Check the Makefile to see what options are passed to 'gcc'. In particular, it's possible that the duplicate definitions are conditionally-compiled with #ifdef, and you may need to add some preprocessor definitions to your XCode project's settings.
我的猜测是公共标头定义了一个内联函数,该函数会导致重复的符号。 如果是这种情况,请在这些函数前添加
static inline
前缀,或者在另一个 .c 文件中使用extern
声明将它们定义为inline
。My guess is that the common header defines an inline function that is resulting in a duplicate symbol. If that is the case, prefix those functions with a
static inline
, or define them asinline
with anextern
declaration in another .c file.