- 先說一下函數重載, 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