无法确定 C.free 的名称类型。

huangapple go评论88阅读模式
英文:

could not determine kind of name for C.free

问题

我正在尝试在我的 Golang 应用程序中使用 C.free。我的代码如下所示:

package main

import (
	"fmt"
	"unsafe"
)

// #include <stdlib.h>

import "C"

//export FreeMemory
func FreeMemory(pointer *int64) {
	C.free(unsafe.Pointer(pointer))
}

我已经进行了一些搜索,我了解到错误是因为我没有包含 stdlib.h,但实际上我已经包含了。

这是我的构建命令:go build --buildmode=c-shared -o main.dll。构建后我得到的错误是:could not determine kind of name for C.free

我的操作系统是 Windows 10。

谢谢。

英文:

I am trying to use C.free in my Golang application. My code is as follows:

package main

import (
	&quot;fmt&quot;
	&quot;unsafe&quot;
)

// #include &lt;stdlib.h&gt;

import (
	&quot;C&quot;
)


//export FreeMemory
func FreeMemory(pointer *int64) {
	C.free(unsafe.Pointer(pointer))
}

I have done some searching and I understand the error was because I don't have stdlib.h included, but I do.

This is my build command: go build --buildmode=c-shared -o main.dll. The error I get after building is: could not determine kind of name for C.free

My OS is Windows 10

Thanks

答案1

得分: 9

如果在导入"C"之前有注释,该注释被称为前导注释,并在编译包的C部分时用作头部。例如:

使用紧接Go行注释的方式:

package main

import "unsafe"

// #include <stdlib.h>
import "C"

//export FreeMemory
func FreeMemory(pointer *int64) {
    C.free(unsafe.Pointer(pointer))
}

func main() {}

使用紧接Go通用注释的方式:

package main

import "unsafe"

/*
#include <stdlib.h>
*/
import "C"

//export FreeMemory
func FreeMemory(pointer *int64) {
    C.free(unsafe.Pointer(pointer))
}

func main() {}
英文:

> If the import of "C" is immediately preceded by a comment, that
> comment, called the preamble, is used as a header when compiling the C
> parts of the package. For example:
>
> // #include <stdio.h>
> // #include <errno.h>
> import "C"

Using immediately preceding Go line comments:

package main

import &quot;unsafe&quot;

// #include &lt;stdlib.h&gt;
import &quot;C&quot;

//export FreeMemory
func FreeMemory(pointer *int64) {
	C.free(unsafe.Pointer(pointer))
}

func main() {}

Using immediately preceding Go general comments:

package main

import &quot;unsafe&quot;

/*
#include &lt;stdlib.h&gt;
*/
import &quot;C&quot;

//export FreeMemory
func FreeMemory(pointer *int64) {
	C.free(unsafe.Pointer(pointer))
}

func main() {}

huangapple
  • 本文由 发表于 2021年8月24日 05:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/68899200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定