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
相关推荐
通信仿真实验室12 分钟前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&15 分钟前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
家有狸花1 小时前
VSCODE驯服日记(三):配置C++环境
c++·ide·vscode
dengqingrui1232 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝2 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
ZZZ_O^O2 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
代码雕刻家3 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
吾爱星辰4 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer4 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良4 小时前
c#增删改查 (数据操作的基础)
开发语言·c#