2008年10月21日 星期二

[C++] C++ 呼叫 C 的 function

我知道  我知道  這是老問題了, 可是就是有人不會或者是忘記. 沒關係很簡單.

在這裡寫下方法.

注意了一切都沒變, 只是在 c 的 header file 上加入 下面標示的紅色指令就可以了. 要求 c++ 編譯程式不要把 symbol 加入 mangle 後綴.

----------------- hello.h ------------

#ifdef __cplusplus
    extern "C" {
#endif

void hello();

#ifdef __cplusplus
    }
#endif

----------------- hello.c -------------

void hello(){

            int i=0;

            i++;

}

 

-------------------- 你的 c++ 程式 --------------

#include "hello.h"

void main(){

        hello();

}

 

by Jing

1 則留言:

  1. Minor correction...

    >> ...要求 c++ 編譯程式不要把 symbol 加入 mangle 後綴

    Name mangling does not ask compilers to pad things at the end of symbols. Currently, it's up to implementations to do what works for them.
    http://en.wikipedia.org/wiki/Name_mangling#How_different_compilers_mangle_the_same_functions

    回覆刪除