1. C 調用 C++封裝好后的函數:
-> 在C++中有一個函數 int main_cpp():
-> 首先構建頭文件, #ifndef CPP_FILE_H #define CPP_FILE_H extern "C" int main_cpp(); 將C++函數按C規范編譯; #endif
-> 然后在C++中, #include "cppf.h" 實現頭文件中定義的函數, int main_cpp(){...}
-> 最后在C中, #include "cppf.h" 導入頭文件, 直接使用main_cpp(); 即可
C++調C 看這篇文章: https://blog.csdn.net/ygsyyl/article/details/8153886
C調C++並重寫其成員函數 看這篇文章: https://blog.csdn.net/nizqsut/article/details/52148973
2. 在編寫C++調C的頭文件中, extern "C" int main_z(); 中出現未輸入表示符錯誤,
#ifdef __cplusplus
extern "C" {
int main_z();
}
endif
改為將C++重定義為C即可;
3. 如果是C++調C庫:
例如 我們有了一個C庫文件,它的頭文件是f.h
extern "C"
{
#include "f.h"
}
extern "C"
{
extern void f1();
}
