extern c 和extern c++

cpp 复制代码
// my_c_function.c
//
#include <stdio.h>
#include "my_c_function.h"

void print_hello_from_c() {
    printf("Hello from C!\n");
}
cpp 复制代码
// my_c_function.h

extern "C"
{
        void print_hello_from_c();
}
cpp 复制代码
// 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;
}
bash 复制代码
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

bash 复制代码
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++ 编译方式

cpp 复制代码
// my_c_function.h

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

_Z18print_hello_from_cv

bash 复制代码
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
相关推荐
Dr.勿忘2 分钟前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
dal118网工任子仪8 分钟前
92,[8] 攻防世界 web Web_php_wrong_nginx_config
开发语言·php
Terasic友晶科技9 分钟前
第26篇 基于ARM A9处理器用C语言实现中断<二>
c语言·fpga开发·中断·de1-soc开发板
wjs202416 分钟前
SQLite Update 语句详解
开发语言
加油,旭杏21 分钟前
【go语言】接口
开发语言·后端·golang
xianwu5431 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
爱编程的小新☆1 小时前
Java篇之继承
java·开发语言
jk_1011 小时前
MATLAB中lineBoundary函数用法
开发语言·matlab
行十万里人生2 小时前
Qt事件处理:理解处理器、过滤器与事件系统
开发语言·git·qt·华为od·华为·华为云·harmonyos
大模型铲屎官2 小时前
HTML从入门到精通:链接与图像标签全解析
开发语言·前端·javascript·html·编程·链接标签·图像标签