为什么外部“C”是仅在要包含的文件内工作,而不是在 #include 指令周围工作
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的
AniUtils.h
文件被任何其他 Objective-C 类包含(例如,.m 文件;不是 Objective-C++、.mm 文件),那么第一种方法您实现的extern C
语句将导致错误。例如,下面是一个普通的 Objective C 类,它将导入有问题的
AniUtils.h
Objective-C++ 类。请注意,因为这些文件是WowClass.h
&WowClass.m
,这些文件将作为普通 Objective-C 进行处理:WowClass.h:
WowClass.m:
现在,如果您的
AniUtils.h
是以第一种方式定义的,当您尝试像普通 Objective-C 一样处理包含extern C
语句的WowClass.m
文件时,您会收到错误。相反,您需要对AniUtils.h
中的extern C
语句进行条件化:当然,另一种方法是将
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 theextern 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 areWowClass.h
&WowClass.m
, these files will be processed as plain Objective-C:WowClass.h:
WowClass.m:
Now, if your
AniUtils.h
is defined in the first manner, you'll get errors when trying to process theWowClass.m
file, which includes theextern C
statement, as plain Objective-C. Instead, you need to conditionalize theextern C
statement inAniUtils.h
:Of course, an alternative would be to simply rename
WowClass.m
toWowClass.mm
so that theWowClass
interface and implementation files are always processed as Objective-C++, but that kind of misses the point.嗯,应该可以。您确定
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
您是否也将 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.
您可能也将 AniUtils.h 包含在另一个文件中。
You might have AniUtils.h included in another file as well.