二級(jí)C技巧(C程序如何使用C++寫好的模塊)

字號(hào):

一個(gè)使用C++寫好的程序,c程序如何使用?
    1. 采用 function (args)函數(shù)。 Function中利用C++類等模塊實(shí)現(xiàn)相應(yīng)的功能。但是args中不包含與C++模塊(類)相關(guān)的類型,均為C能接受的類型。如果需要返回本來需要vector保存的結(jié)果,考試,大提示現(xiàn)在用char buff[LEN]保存。
    2. 定義cFunction(args)接口,與function完全相同,但是需要用C編譯器編譯
    Extern “C” {
    Int cFunction ( args ){ return function ( args ); }
    }
    3. 將上面所有的.cpp文件做成lib庫。Libtest.so
    4. 在c文件里面這些寫:
    Extern int cFunction (args) 聲明后,即可使用。
    注意使用libtest.so庫。Gcc xx.c -LXXXX –ltest