extern c 和extern c++

// my_c_function.c
//
#include <stdio.h>
#include "my_c_function.h"

void print_hello_from_c() {
    printf("Hello from C!\n");
}
// my_c_function.h

extern "C"
{
        void print_hello_from_c();
}
// my_cpp_code.cpp
//
#include "my_c_function.h"
extern "C" {
   void print_hello_from_c();
}

int main() {
    print_hello_from_c(); // 调用C函数
    return 0;
}
g++ -o  my_c_function.o -c my_c_function.c -I ./
g++ -o  my_cpp_code.o -c my_cpp_code.cpp
g++ my_c_function.o my_cpp_code.o -o my_program

用g++ 编译时,如果my_c_function.h 里面有extern “C” 是c的编译方式, 调用如果不extern "C" 会编译失败

// my_c_function.h

print_hello_from_c 

hfu@CNSH-BLD1:~/program/cplusplus/extern_test$ objdump -t my_c_function.o

my_c_function.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 my_c_function.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .rodata        0000000000000000 .rodata
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 g     F .text  0000000000000013 print_hello_from_c
0000000000000000         *UND*  0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000         *UND*  0000000000000000 puts

如果用 g++ 编译时,如果my_c_function.h 里面没有extern “C” 便是c++ 编译方式

// my_c_function.h

//extern "C"
//{
        void print_hello_from_c();
//}

_Z18print_hello_from_cv 

hfu@CNSH-BLD1:~/program/cplusplus/extern_test$ objdump -t my_c_function.o

my_c_function.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 my_c_function.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .rodata        0000000000000000 .rodata
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 g     F .text  0000000000000013 _Z18print_hello_from_cv
0000000000000000         *UND*  0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000         *UND*  0000000000000000 puts

相关推荐

  1. extern c extern c++

    2024-03-24 13:36:07       17 阅读
  2. C单片机关键字extern、static const

    2024-03-24 13:36:07       38 阅读
  3. externstatic的使用与区别

    2024-03-24 13:36:07       20 阅读
  4. 深入理解C语言中的 extern` static

    2024-03-24 13:36:07       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 13:36:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 13:36:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 13:36:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 13:36:07       18 阅读

热门阅读

  1. cookie、session和token的区别

    2024-03-24 13:36:07       21 阅读
  2. 读《舞!舞!舞!》有感

    2024-03-24 13:36:07       19 阅读
  3. SpringBoot全局异常处理方法

    2024-03-24 13:36:07       19 阅读
  4. 【Node.js】events

    2024-03-24 13:36:07       21 阅读
  5. 【jvm】young gc full gc

    2024-03-24 13:36:07       16 阅读
  6. Python爬虫之urllib库

    2024-03-24 13:36:07       17 阅读
  7. 游戏中线上已有功能迭代的兼容问题

    2024-03-24 13:36:07       17 阅读
  8. Python爬虫之requests库

    2024-03-24 13:36:07       15 阅读