c/c++ extern “C”

c/c++ extern “C”

常见的样式

extern “C”{ ... }
extern "C" return-type func-name(type , type ){}
extern "C" return-type func-name(type , type );
 
含义:在C++中,以C语言的方式编译源文件。
例子
//t_extern_yes.cpp
  1. extern "C"
  2. {
  3.         int f(void)
  4.         {
  5.                 return 0;
  6.         }
  7. }
//t_extern_non.cpp
  1. int f(void)
  2. {
  3.         return 0;
  4. }
汇编
  1. gcc t_extern_non.cpp t_extern_yes.cpp
  2. 或者
  3. gcc t_extern_non.cpp t_extern_yes.cpp
 得到
  1. .file "t_extern_non.cpp"
  2. .text
  3. .align 2
  4. .globl _Z1fv
  5. .type _Z1fv, @function
  6. _Z1fv:
  7. .LFB2:
  8. pushl %ebp
  9. .LCFI0:
  10. movl %esp, %ebp
  11. .LCFI1:
  12. movl $0, %eax
  13. popl %ebp
  14. ret
  15. .LFE2:
  16. .size _Z1fv, .-_Z1fv
  17. .globl __gxx_personality_v0
  18. .ident "GCC: (GNU) 4.2.1 (SUSE Linux)"
  19. .section .note.GNU-stack,"",@progbits
  1. .file "t_extern_yes.cpp"
  2. .text
  3. .align 2
  4. .globl f
  5. .type f, @function
  6. f:
  7. .LFB2:
  8. pushl %ebp
  9. .LCFI0:
  10. movl %esp, %ebp
  11. .LCFI1:
  12. movl $0, %eax
  13. popl %ebp
  14. ret
  15. .LFE2:
  16. .size f, .-f
  17. .globl __gxx_personality_v0
  18. .ident "GCC: (GNU) 4.2.1 (SUSE Linux)"
  19. .section .note.GNU-stack,"",@progbits
用imdiff看时只有上面红色标志的是不同的,这是函数名字编译后的ID,C++ 丰富了内容。如果用extern “C”,告诉C++编译器,使用C的编译方式编译,这样就可以连接原来的用C编译出来的C文件了。现在模拟一个C++调用C库文件的现象。
 

C++ 调用C库

//t_add.c
  1. int add(int a, int b){
  2.         return (a+b);
  3. }
得到c的静态库文件t_add.o
  1. gcc -c t_add.c
原始的C头文件
  1. #ifndef T_ADD_C__
  2. #define T_ADD_C__
  3. int add(int a, int b);
  4. #endif
修改为可以被C++ 使用的头文件//t_add.h
  1. #ifndef T_ADD_C__
  2. #define T_ADD_C__
  3. extern "C" int add(int a, int b);
  4. #endif
可以被C++文件引用了,如//main.cpp
  1. #include <stdio.h>
  2. #include "t_add.h"
  3. int main(void)
  4. {
  5.         printf("1 + 2 = %d ", add(1,2));
  6.         return 0;
  7. }
可以编译了
  1. g++ t_add.o main.c
输出
  1. 1 + 2 = 3
 
使用方式
 
当然了,extern “C”的方式多种,
  1. 可以在函数前,
  2. 文件开始结尾 {}hold住,
  3. 也可以方便的在 #inldue语句加,如
    1. extern "C" {
    2.         #include "t_add.h"
    3. }
另外,C++变压器会有 __cplusplus 的宏定义,一般都是结合使用
原文地址:https://www.cnblogs.com/timssd/p/4781113.html