假設我有一個使用類和模板的C++庫lib.h。再假設我有以下的自定義C++頭myLink.h:c和C++與庫的鏈接
#include "lib.h"
//call methods from lib.h that use templates and classes
// and return an integer based off of the information gained from calling functions lib.h
extern "C" int foo(int param1, const int param2);
現在假設我在一個名爲C test.c.如下調用函數foo()是否合法?
//in test.c
int first = foo(5, 6);
此外,編譯的目標代碼/鏈接器階段發生了什麼?
謝謝!
您可能希望讓您的「myLink.h」能夠從C和C++編譯,例如通過使用'#ifdef __cplusplus'在單獨的行上包裝'extern「C」{'和'}'。這樣'int foo(int param1,const int param2);'部分對於純C代碼是可見的,其餘部分對C++是可見的。如果沒有這個,你的「test.c」將需要一個單獨的方法來聲明函數。 – 2012-07-10 03:19:29