C 中的函数名称范围

发布于 2024-07-14 06:08:16 字数 293 浏览 3 评论 0原文

函数名称作用域如何跨多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2024-07-21 06:08:17

所有未标记为静态的函数都具有全局作用域(它们被转储到单个命名空间中)。 标记为静态的函数仅限于定义它们的翻译单元。

您违反了“单一定义规则”。

您的标头之一可能有变量的定义。 例如:在 common.h 中,您将

int foo = 42;

其更改为:

extern int foo; // a declaration

然后创建一个 common.c 文件,在其中放置定义:

int foo = 42;

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 have

int foo = 42;

change it to:

extern int foo; // a declaration

and then create a common.c file where you put a definition:

int foo = 42;
乜一 2024-07-21 06:08:17

在 C 中,如果我没记错的话,static 函数名称对于定义它们的源文件来说是本地的,但所有其他函数名称都存在于全局命名空间中。 因此,如果您有 file1.c

void fn1() {}
static void fn2() {}

file2.c

void fn1() {}
static void fn2() {}

并且您尝试使用类似的内容编译它们

cc 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 have file1.c with

void fn1() {}
static void fn2() {}

and file2.c with

void fn1() {}
static void fn2() {}

and you tried to compile them with something like

cc file1.c file2.c

then you would get a name conflict between the fn1 in file1.c and the fn1 in file2.c, but not between the two fn2 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.)

硬不硬你别怂 2024-07-21 06:08:17

如果从 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.

帅的被狗咬 2024-07-21 06:08:17

我的猜测是公共标头定义了一个内联函数,该函数会导致重复的符号。 如果是这种情况,请在这些函数前添加 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 as inline with an extern declaration in another .c file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文