C中如何呼叫C 函式

2021-06-28 08:32:30 字數 1935 閱讀 7015

1,在c中如何呼叫c++函式將函式用extern "c"宣告;

將 c++ 函式宣告為``extern "c"''(在你的 c++ **裡做這個宣告),然後呼叫它(在你的 c 或者 c++ **裡呼叫)。例如:

// c++ code:

extern "c" void f(int);

void f(int i)

然後,你可以這樣使用 f():

/* c code: */

void f(int);

void cc(int i)

2, 那麼如何將類內成員函式宣告?

// c++ code:

class c

; 然後,你就可以這樣呼叫 c::f():

/* c code: */

double call_c_f(struct c* p, int i); 

void ccc(struct c* p, int i)

例子:tvqecpayloadfetcher * vqecpayloadfetcher = null;

ivqecstatistics::tvqecstatisticsinfo vqecstatisticsinfo ;

bool ttest::

myfun

(const char* addr, uint32_t port) throw ()

/** callback  function supplied during a tuner bind operation.

*/extern "c" int32_t getdcrstats (int32_t context_id,

vqec_ifclient_dcr_stats_t *stats)

else } }

fprintf(stderr, "%s:%d:%s,  vqec stats info  else if(temp !=null)  \n",__file__, __line__,__function__ );

//stats->dec_picture_cnt = 15;

}  else

}3, 如果你想在 c 裡呼叫過載函式,則必須提供不同名字的包裝,這樣才能被 c **呼叫。例如:

// c++ code:

void f(int);

void f(double);

extern "c" void f_i(int i)

extern "c" void f_d(double d)

然後,你可以這樣使用每個過載的 f():

/* c code: */

void f_i(int);

void f_d(double); 

void cccc(int i,double d)

注意,這些技巧也適用於在 c 裡呼叫 c++ 類庫,即使你不能(或者不想)修改 c++ 標頭檔案。

4, 如何在c程式中呼叫c++的庫

假如我們手上有乙個c++的類

class test

;#include "test1.h"

void test::test();

我們需要用乙個c程式來呼叫這個c++庫裡面的函式。

a. 將c++庫做c的封裝。

#include "test1.h"

extern "c"

}編譯:g++ -shared -o test.so test2.c test1.cc

b. 編寫用c**呼叫c++的實現

#include

#include

int main(int argc , char ** argv)

void (* function)();

function = dlsym(lib , "test");

if (function == 0)

function();

dlclose(lib);

return 0;

}

C中如何呼叫C 函式

前陣子被問及乙個在c中如何呼叫c 函式的問題,當時簡單回答是將函式用extern c 宣告,當被問及如何將類內成員函式宣告時,一時語塞,後來網上查了下,網上有一翻譯c 之父的文章可以作為解答,遂拿來mark一下。將 c 函式宣告為 extern c 在你的 c 裡做這個宣告 然後呼叫它 在你的 c ...

如何在C 中呼叫C函式

這是程式設計師面試寶典中的一道題,查資料得到解決方法 注意這裡的c呼叫c 或者c 呼叫c意思是.c檔案中呼叫.cpp檔案中 或者相反。整合開發環境如vc 6.0或者vs都是以檔案字尾來區別當前要編譯的是c 還是 然後採用響應的編譯 呼叫協議等。使用extern c 主要是因為c編譯器編譯函式時不帶引...

如何在C 中呼叫C函式?

假如在乙個專案中同時包含了c和c 當c 呼叫c函式時,以傳統c程式設計 include h 後,宣告函式。由於main.cpp 是個c 以c方式的呼叫,g 編譯器無法通過編譯。解決方案一 重寫乙個專門被c 用的標頭檔案 可能存在是別人已經寫好的標頭檔案,我們無法修改等問題 e.g.新增乙個標頭檔案 ...