我們想在C++文件中定義C語言的函數,不想讓其名字被mangle,可以用以下的方式來聲明或者定義:
extern "C" { int testMethod(int a, int b); }
另外,其實也類似,C++文件最好不要include C文件,因爲如果直接include C文件的話,沒有extern "C",C++會將其名字mangle.
C文件也最好不要include C++的頭文件,因爲C++的頭文件往往會包含class的定義,造成C文件編譯失敗。
我們想在C++文件中定義C語言的函數,不想讓其名字被mangle,可以用以下的方式來聲明或者定義:
extern "C" { int testMethod(int a, int b); }
另外,其實也類似,C++文件最好不要include C文件,因爲如果直接include C文件的話,沒有extern "C",C++會將其名字mangle.
C文件也最好不要include C++的頭文件,因爲C++的頭文件往往會包含class的定義,造成C文件編譯失敗。