为什么外部“C”是仅在要包含的文件内工作,而不是在 #include 指令周围工作

发布于 2024-10-19 10:08:27 字数 441 浏览 6 评论 0原文

我在 Objective C++ 类中包含了一个名为“AniUtils.h”的 C 文件。据我所知,如果我要在 C++ 文件中包含 C 文件,则需要使用“extern "C"”。让我感到困惑的是,将 include 指令包装在 extern C 中似乎不起作用,而将 extern C 放入文件正文中却可以。

因此,包装 include 指令不起作用。

extern "C" {
    #include "AniUtils.h"
}

然而,这样做效果很好:

// AniUtils.h
#ifdef __cplusplus
extern "C" {
#endif

// body of included file

#ifdef __cplusplus
}
#endif

为什么第二种方法有效,而第一种方法不行?

I'm including a C file called "AniUtils.h" in an Objective C++ class. I understand that I need to use 'extern "C"' if I'm going to include a C file in a C++ file. What's confusing me is that wrapping the include directive in an extern C doesn't seem to work, while putting extern C in the body of the file does.

So, wrapping the include directive doesn't work.

extern "C" {
    #include "AniUtils.h"
}

However, doing it this way works fine:

// AniUtils.h
#ifdef __cplusplus
extern "C" {
#endif

// body of included file

#ifdef __cplusplus
}
#endif

Why does the second approach work but not the first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我家小可爱 2024-10-26 10:08:27

如果您的 AniUtils.h 文件被任何其他 Objective-C 类包含(例如,.m 文件;不是 Objective-C++、.mm 文件),那么第一种方法您实现的 extern C 语句将导致错误。

例如,下面是一个普通的 Objective C 类,它将导入有问题的 AniUtils.h Objective-C++ 类。请注意,因为这些文件是 WowClass.h & WowClass.m,这些文件将作为普通 Objective-C 进行处理:

WowClass.h:

#import <Foundation/Foundation.h>
#import "AniUtils.h"

@interface WowClass : NSObject {

}

@end

WowClass.m:

#import "WowClass.h"

@implementation
 .....
@end

现在,如果您的 AniUtils.h 是以第一种方式定义的,当您尝试像普通 Objective-C 一样处理包含 extern C 语句的 WowClass.m 文件时,您会收到错误。相反,您需要对 AniUtils.h 中的 extern C 语句进行条件化:

// AniUtils.h
#ifdef __cplusplus
extern "C" {
#endif

// body of included file

#ifdef __cplusplus
}
#endif

当然,另一种方法是将 WowClass.m 重命名为WowClass.mm 以便 WowClass 接口和实现文件始终作为 Objective-C++ 进行处理,但这种做法没有抓住重点。

If your AniUtils.h file is included by any other Objective-C classes (e.g., .m files; not Objective-C++, .mm files), then the first way you've implemented the extern C statement will cause errors.

For example, here is a plain Objective C class that will be importing the problematic AniUtils.h Objective-C++ class. Note that because these files are WowClass.h & WowClass.m, these files will be processed as plain Objective-C:

WowClass.h:

#import <Foundation/Foundation.h>
#import "AniUtils.h"

@interface WowClass : NSObject {

}

@end

WowClass.m:

#import "WowClass.h"

@implementation
 .....
@end

Now, if your AniUtils.h is defined in the first manner, you'll get errors when trying to process the WowClass.m file, which includes the extern C statement, as plain Objective-C. Instead, you need to conditionalize the extern C statement in AniUtils.h:

// AniUtils.h
#ifdef __cplusplus
extern "C" {
#endif

// body of included file

#ifdef __cplusplus
}
#endif

Of course, an alternative would be to simply rename WowClass.m to WowClass.mm so that the WowClass interface and implementation files are always processed as Objective-C++, but that kind of misses the point.

甜柠檬 2024-10-26 10:08:27

嗯,应该可以。您确定 AniUtils.h 是用 C 编译器而不是 C++ 编译器编译的吗?
这里有一个非常详细的整个问题的描述,看看这里,也许会对你有所帮助。

http://de.w3support.net/index.php?db=so& ;id=67894

Hm, it should work. Are you sure that AniUtils.h is compiled wit a C compiler and not with a C++ one?
There is a really detailed descrption of the whole problematic, take a look here, perhaps it will help you.

http://de.w3support.net/index.php?db=so&id=67894

謸气贵蔟 2024-10-26 10:08:27

您是否也将 AniUtils.h 包含在 .cpp 文件中?在第二种情况下,它也会看到外部“C”,但在第一种情况下不会。

Are you including AniUtils.h in the .cpp file as well? In the second case it will see the extern "C" as well, but not in the first case.

[浮城] 2024-10-26 10:08:27

您可能也将 AniUtils.h 包含在另一个文件中。

You might have AniUtils.h included in another file as well.

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