英文:
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 (
"fmt"
"unsafe"
)
// #include <stdlib.h>
import (
"C"
)
//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 "unsafe"
// #include <stdlib.h>
import "C"
//export FreeMemory
func FreeMemory(pointer *int64) {
C.free(unsafe.Pointer(pointer))
}
func main() {}
Using immediately preceding Go general comments:
package main
import "unsafe"
/*
#include <stdlib.h>
*/
import "C"
//export FreeMemory
func FreeMemory(pointer *int64) {
C.free(unsafe.Pointer(pointer))
}
func main() {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论