C代码

C代码在willUseGo.c文件中,分两部分展示,第一部分如下:

#include <stdio.h>
#include "usedByC.h"
int main(int argc, char **argv) {
    GoInt x = 12;
    GoInt y = 23;
    printf("About to call a Go function!\n");
    PrintMessage();

导入usedByC.h之后,就可以使用其中实现的函数。

第二部分代码:

    GoInt p = Multiply(x,y);
    printf("Product: %d\n",(int)p);
    printf("It worked!\n");
    return 0;
}

变量GoInt p是从Go函数中获取一个整数值,使用(int)p可以把它转换成C语言的整数。

在Mac机器上编译和执行willUseGo.c,输出如下:

$ gcc -o willUseGo willUseGo.c ./usedByC.o
$ ./willUseGo
About to call a Go function!
A Go function!
Product: 276
It worked!

results matching ""

    No results matching ""