【问题标题】:could not determine kind of name for C.free无法确定 C.free 的名称类型
【发布时间】:2021-08-24 06:10:10
【问题描述】:

我正在尝试在我的 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

谢谢

【问题讨论】:

    标签: go


    【解决方案1】:

    如果 "C" 的导入紧跟在注释之前,则 注释,称为序言,在编译 C 时用作标头 包的部分。例如:

    // #include <stdio.h> 
    // #include <errno.h> 
    import "C"
    
    package main
    
    import "unsafe"
    
    // #include <stdlib.h>
    import "C"
    
    //export FreeMemory
    func FreeMemory(pointer *int64) {
        C.free(unsafe.Pointer(pointer))
    }
    
    func main() {}
    

    【讨论】:

    • 谢谢,就这么简单:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多