Ir a las llamadas de idioma c biblioteca dinámica

test.h es el siguiente:

#include<stdio.h>

int add(int a,int b,char *name,int *c);

test.c es el siguiente:

#include "test.h"

int add(int a,int b,char *name,int *c)
{
        printf("-----name[%s]\n",name);
        memcpy(name,"hello",5);
        *c = a+b;
        return a+b;
}

Compilar biblioteca dinámica

       gcc -w test.c -fPIC -shared -o libtest.so

 

test.go es el siguiente:

package main


/*
#cgo CFLAGS: -I./
#cgo LDFLAGS: -L./ -ltest
#include "test.h"
#include <stdlib.h>
*/
import "C"

import (
    "fmt"
)

func main(){

	name := "nxy"
	cname := C.CString(name)
	
	a := C.int(3)
	b := C.int(6)
	c := C.int(0)
	m := C.add(a,b,cname,&c)
	fmt.Println(m)
	fmt.Println(C.GoString(cname))
	fmt.Println(c)
	
    C.free(unsafe.Pointer(cname))        /*C.CString存在内存泄露需要释放*/
	
}

llevado a cabo

      ve a ejecutar test.go 

resultado:

----- nombre [nxy]
9
hola
9

 

Supongo que te gusta

Origin blog.csdn.net/woailp___2005/article/details/105998128
Recomendado
Clasificación