extern和extern"C"对于变量 [英] extern and extern "C" for variables

查看:144
本文介绍了extern和extern"C"对于变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个供C程序使用的C ++共享库.但是,我对externextern "C"有疑问.

I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C".

考虑以下代码

我的头文件是这样的:

#ifdef __cplusplus      
     extern "C" int global;
     extern "C" int addnumbers(int a, int b); 
 #else
      extern int global;
 #endif

这很好用;我只需要声明

This works perfectly fine; I just have to declare

int global;

在我的.cpp或.c文件中.但是,我不明白的是:

in either my .cpp or my .c file. However, what I don't understand is:

这里extern "C"extern有什么区别?我尝试注释掉extern "C" int global,它起作用了!为什么?

What is the difference between extern "C" and extern here? I tried commenting out extern "C" int global and it works! Why?

我知道extern "C"用于进行C链接.这就是为什么我有extern "C" int addnumbers(int,int)的原因.换句话说,如果我想编写要在C程序中使用的C ++函数,请编写extern "C".现在,全局变量呢-我猜这里的情况有所不同吗?我希望C程序使用名为global的C ++变量,但是我可以使用extern而不是extern "C".这是为什么?这对我来说并不直观.

I know that extern "C" is used for making C linkage. That's why I have extern "C" int addnumbers(int,int). In other words, if I want to write a C++ function that is to be used in a C program, I write extern "C". Now, what about global variables - the situation is different here I guess? I want the C program to use a C++ variable named global, but I can use extern not extern "C". Why is that? This is not intuitive to me.

评论:我不认为这是重复的,因为我要问的是,将变量用于函数还是函数时有什么区别.

Comment: I don't think this is a duplicate, because I'm asking what the difference is when you use it for variables versus functions.

推荐答案

通过将extern "C"附加到C ++声明(对象和函数均如此),可以为它们提供"C链接"-使它们可从C代码访问.如果省略此语言链接"规范,则编译器将不做任何努力来进行适当的链接.就功能而言,这会导致由于操作不当而导致链接失败.对于全局变量,一切都可能正常运行,因为变量不需要整形.

By attaching extern "C" to your C++ declarations (objects and functions alike) you give them "C linkage" - make them accessible from C code. If you omit this "language linkage" specification, the compiler doesn't do any effort to do proper linkage. In the case of functions, this results in failed linkage, because of mangling. In the case of global variables, everything might work fine, because variables don't need mangling.

但是,在我的系统(MS Visual Studio)上,如果我忘记"在C ++头文件中指定extern "C"链接规范,则C和C ++之间的链接将不起作用.错误消息示例:

However, on my system (MS Visual Studio), linkage between C and C++ doesn't work if I "forget" to specify the extern "C" linkage specification in the C++ header file. Example error message:

error LNK2001: unresolved external symbol "int global" (?_global)

同时,当我使用dumpbin实用工具检查包含global定义的已编译C ++源代码时,我看到了

While, when I examine a compiled C++ source code that contains the definition of global with the dumpbin utility, I see

00B 00000014 SECT4  notype       External     | ?global@@3HA (int global)

因此,MS Visual Studio会修改全局变量的名称,除非它们具有C链接-这使C链接规范成为必需.

So MS Visual Studio mangles the names of global variables, unless they have C linkage - this makes C linkage specifications mandatory.

此外,请考虑以下示例:

In addition, consider the following example:

namespace example {
    int global;
}

如果全局变量在名称空间内,则C代码将无法访问它.在这种情况下,所有编译器都需要在C ++声明中使用正确的链接规范:

If the global variable is inside a namespace, C code will not get access to it. In this case, all compilers will require the proper linkage specification on the C++ declaration:

namespace example {
    extern "C" int global;
}


结论:


Conclusion:

当需要C链接时使用extern "C"-不管是函数还是全局变量都没有关系.如果它是全局变量,则无论如何它都可以工作,但不能保证(并且可能很危险).

Use extern "C" when you want C linkage - doesn't matter if it's a function or a global variable. If it's a global variable, it may work regardless, but it's not guaranteed (and may be dangerous).

这篇关于extern和extern"C"对于变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆