c++ - 名称或类型具有某种语言链接意味着什么?

标签 c++ c standards linkage extern-c

根据 (c) ANSI ISO/IEC 14882:2003,第 127 页:

Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur only in namespace scope (3.3). In a linkage-specification, the specified language linkage applies to the function types of all function declarators, function names, and variable names introduced by the declaration(s).

extern "C" void f1(void(*pf)(int));
// the name f1 and its function type have C language
// linkage; pf is a pointer to a C function

extern "C" typedef void FUNC();
FUNC f2;
// the name f2 has C++ language linkage and the
// function's type has C language linkage

extern "C" FUNC f3;
// the name of function f3 and the function's type
// have C language linkage

void (*pf2)(FUNC*);
// the name of the variable pf2 has C++ linkage and
// the type of pf2 is pointer to C++ function that
// takes one parameter of type pointer to C function

这一切意味着什么?比如f2()函数有什么链接,C还是C++语言链接?

正如@Johannes Schaub 所指出的,标准中没有真正解释这意味着什么,因此在不同的编译器中可能会有不同的解释。

请解释目标文件中的差异:

  • 具有 C 语言链接和 C++ 语言链接的函数名称。
  • 具有 C 语言链接和 C++ 语言链接的函数类型。

最佳答案

语言链接是用于C++non-C++ 代码片段之间链接的术语。通常,在 C++ 程序中,所有函数名、函数类型甚至变量名都有默认的 C++ 语言链接。

可以使用预定义的链接说明符将 C++ 目标代码链接到使用其他源语言(如 C)生成的另一个目标代码。

你必须知道name mangling的概念,它对函数名、函数类型和变量名进行编码,以便为它们生成一个唯一的名称。这允许链接器区分常用名称(如在函数重载的情况下)。将 C 模块与使用 C++ 编译器编译的库或目标文件链接时,不希望进行名称修改。为了防止这种情况下的名称修改,使用了链接说明符。在这种情况下,extern "C" 是链接说明符。举个例子(c++代码提到here):

typedef int (*pfun)(int);  // line 1
extern "C" void foo(pfun); // line 2
extern "C" int g(int)      // line 3
...
foo( g ); // Error!        // line 5

第 1 行声明 pfun 指向 C++ 函数,因为它缺少链接说明符。

因此,第 2 行将 foo 声明为一个 C 函数,它接受一个指向 C++ 函数的指针。

第 5 行尝试使用指向 g、C 函数、类型不匹配的指针调用 foo。

函数名链接中的差异:

让我们拿两个不同的文件:

一个带有extern "c"链接(file1.cpp):

#include <iostream>
using namespace std;

extern "C"
{
void foo (int a, int b)
{
    cout << "here";
}
}

int main ()
{
    foo (10,20);
    return 0;
}

一个没有extern "c"链接(file2.cpp):

#include <iostream>
using namespace std;

void foo (int a, int b)
{
    cout << "here";
}

int main ()
{
    foo (10,20);
    return 0;
}

现在编译这两个并检查 objdump。

# g++ file1.cpp -o file1
# objdump -Dx file1

# g++ file2.cpp -o file2
# objdump -Dx file2

使用外部“C”链接,函数 foo 没有名称修改。因此,任何使用它的程序(假设我们用它制作了一个共享库)都可以直接调用 foo(使用 dlsymdlopen 等辅助函数)而无需考虑任何名称修饰效果。

0000000000400774 <foo>:
  400774:   55                      push   %rbp
  400775:   48 89 e5                mov    %rsp,%rbp
....
....
  400791:   c9                      leaveq 
  400792:   c3                      retq   

0000000000400793 <main>:
  400793:   55                      push   %rbp
  400794:   48 89 e5                mov    %rsp,%rbp
  400797:   be 14 00 00 00          mov    $0x14,%esi
  40079c:   bf 0a 00 00 00          mov    $0xa,%edi
  4007a1:   e8 ce ff ff ff          callq  400774 <foo>
  4007a6:   b8 00 00 00 00          mov    $0x0,%eax
  4007ab:   c9                      leaveq 

另一方面,当没有使用 extern "C" 时,func: foo 会被一些预定义的规则(编译器/链接器知道)因此应用程序不能直接从它指定名称为 foo 调用它。但是,如果您愿意,您可以使用重命名的名称(在本例中为 _Z3fooii)来调用它,但出于显而易见的原因,没有人使用它。

0000000000400774 <_Z3fooii>:
  400774:   55                      push   %rbp
  400775:   48 89 e5                mov    %rsp,%rbp
 ...
...
  400791:   c9                      leaveq 
  400792:   c3                      retq   

0000000000400793 <main>:
  400793:   55                      push   %rbp
  400794:   48 89 e5                mov    %rsp,%rbp
  400797:   be 14 00 00 00          mov    $0x14,%esi
  40079c:   bf 0a 00 00 00          mov    $0xa,%edi
  4007a1:   e8 ce ff ff ff          callq  400774 <_Z3fooii>
  4007a6:   b8 00 00 00 00          mov    $0x0,%eax
  4007ab:   c9                      leaveq 
  4007ac:   c3                      retq   

This page也是这个特定主题的好读物。

一篇关于调用约定的很好且解释清楚的文章:http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx

关于c++ - 名称或类型具有某种语言链接意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5763919/

相关文章:

c++ - 可以覆盖(隐藏)一个非虚方法但仍然从子类显式调用它吗?

javascript - 有 JSDoc 标准吗?

c++ - MSVC 的 C4138 警告 ("' */' found outside of comment") 是否应该被禁用?

c++ - 关于C++结构和数组的一些问题

c++ - 关于strftime的一个问题

c - 为什么 child 的 getppid() 返回 1

c++ - 什么类型会使 "std::has_unique_object_representations"返回 false?

c - C标准是否对INT_MIN和INT_MAX之间的关系做出保证?

c++ - get_driver_instance() MySQL C++ VS2010 的链接器错误

c - fscanf() - 如何让它在读取 n 个字符后跳到下一个单词?