- 先说一下函数重载, C++ 之所以会进行函数重载, 是因为对函数名进行二次修饰(重新命名)
在C文件中写好的程序, C++引入过来,却没法使用提示 无法连接的外部符号,那是因为C++按照C++的函数命名机制来寻找函数的实现.
第一种情况:
文件为 test.h
void show(); // 进行了函数声明
文件 test.c
#include "test.h"
void show() {printf("hello world\n"); //函数实现
}
文件 main.cpp 如果想使用 test.h 中声明的 show函数,需要使用
extern “C” void show();
#include "test.h"extern "C" void show(); //告诉编译器,用C的方式去链接show函数
void test() {show();
}
第二种情况:
是在C文件中做修改
需要修改文件 test.h
//如果是C++在运行本文件时候, extern C 包含的内容用C语言方式连接
#ifdef __cplusplus
extern "C" {
#endif#include <stdio.h>void show();#ifdef __cplusplus
}
#endif