精华内容
下载资源
问答
  • CGo

    2020-09-07 20:55:24
    文章目录cgo cgo 知识点 /* */之前的是C代码。 C代码和 import “C” 之间不能游空格,否则会报错。 import “C” 必须单独一行,不能和其他的import写在一起。 代码: package main /* #include <stdint.h>...

    一、cgo

    1.1 go调用c函数

    知识点:

    1. /* */之间的是C代码。
    2. C代码和 import "C"之间不能有空格,否则会报错。
    3. import "C"必须单独一行,不能和其他的import写在一起。

    代码:

    package main
    
    /*
    #include <stdint.h>
    
    static int32_t add(int32_t a, int32_t b) {
            return a + b;
    }
    */
    import "C"
    
    import (
            "fmt"
    )
    
    func main() {
            var a, b int32 = 10, 20
            var c int32 = int32(C.add(C.int32_t(a), C.int32_t(b)))
            fmt.Println(c)
    }
    

    运行结果:

    [root@bogon 09]# go run test.go 
    30
    

    1.2 go调用c静态库

    代码:
    hello.h:

    extern int hello(char *name, int age);
    

    hello.c:

    #include <stdio.h>
    #include "hello.h"
    
    int hello(char *name, int age) {
            printf("hello %s, your age is %d\n", name, age);
            return age;
    }
    

    test.go:

    package main
    
    /*
    #include <stdio.h>
    #include <stdlib.h>
    #include "hello.h"
    
    #cgo CFLAGS: -I.
    
    #cgo LDFLAGS: -L. -lhello
    */
    import "C"
    
    import (
            "fmt"
            "unsafe"
    )
    
    func main() {
            name := C.CString("Tom")
            defer C.free(unsafe.Pointer(name))
    
            age := C.int(18)
    
            result := C.hello(name, age)
            fmt.Println(result)
    }
    

    编译c代码为静态库:

    gcc -Wall -c hello.c
    ar -rv libhello.a hello.o
    

    运行结果:

    [root@bogon 09]# go run test.go 
    hello Tom, your age is 18
    18
    

    参考资料:
    Golang 通过 cgo 调用 C/C++ 静态库(一)

    展开全文
  • cgo

    2019-10-08 17:09:30
    package mainimport ( "unsafe" "fmt") .../*#cgo CFLAGS: -I./#cgo LDFLAGS: -L./#include <test.h>*/import "C" //export printlnfunc println(str *C.char) { fmt.Println(C.GoString(str))}//ex...
    package main

    import (
    "unsafe"
    "fmt"
    )


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

    //export println
    func println(str *C.char) {
    fmt.Println(C.GoString(str))
    }

    //export callback
    func callback(ptr unsafe.Pointer) {
    f := *(*func(str *C.char))(ptr)
    f(C.CString("Hello,This from Golang"))
    }

    func main() {
    var fn = println
    C.test(unsafe.Pointer(&fn))
    }

    /

    to install TDM-GCC-64

    go build

    /test.c///
    #include <test.h>
    int test(void* fn)
    {
    callback(fn);
    println("Hello,This from Clang");
    return 0;
    }

    /test.h

    int test(void* fn);
    void println(char* str);
    void callback(void* fn);

    转载于:https://www.cnblogs.com/feicheninfo/p/7357152.html

    展开全文
  • CGO

    2019-08-16 21:46:39
    参考书:《Go语言高级编程》,总结我踩到的一些坑吧。 demo1 // +build go1.12 package main //void SayHello(_GoString_ s) import "C" import ( "fmt" ) func main() { C.SayHello("Hello World!...func S...

    参考书:《Go语言高级编程》,总结我踩到的一些坑吧。

    demo1

    // +build go1.12
    
    package main
    
    //void SayHello(_GoString_ s)
    import "C"
    import (
    	"fmt"
    )
    
    func main() {
        C.SayHello("Hello World!\n")
    }
    
    //export SayHello
    func SayHello(s string) {
        fmt.Print(s)
    }
    

    // +build go1.12 版本要写自己的go语言版本!!!

    go version查看自己的go语言版本。

    import "C" 和注释部分的C语言之间不能有空行!!!

    //export SayHello 和下面的函数之间不能有空行!!!

    欢迎访问我的主页获取更多教程

    展开全文
  • CGO error

    2020-12-26 20:20:26
    <p>I am using cgo, which I have seen you comment on in another issue. But he never really answered if he did or did not use cgo. My project does use CGO, is this an issue? <pre><code> DEBU[0003] ...
  • CGO TypesC作为一种混合编程语言已经很久了,无论那些广泛使用的包是用何种语言实现的,都导出了和C兼容的API。Go程序调用C程序,可以借助两种工具实现,一种是cgo,另一种是SWIG工具。Cgo是为C函数创建go绑定的工具...

    CGO Types

    C作为一种混合编程语言已经很久了,无论那些广泛使用的包是用何种语言实现的,都导出了和C兼容的API。Go程序调用C程序,可以借助两种工具实现,一种是cgo,另一种是SWIG工具。Cgo是为C函数创建go绑定的工具;SWIG工具可以提供更复杂的特性来调用C++程序。(关于SWIG工具的使用,可以参考以前的《calling c++ from golang with swig》系列随笔)。

    本文主要讲述C语言类型、在CGO中可以引用的C类型及对应的引用类型名称。

    查询C99标准文档(6.2.5 Types),主要类型包括:_Bool、char、signed char、unsigned char、short、unsigned short、int、unsigned int、long、unsigned long、long long、unsigned long long、float、double、long double、float _Complex、double _Complex、long double _Complex、void *、__int128_t、__uint128_t

    在CGO中引用C类型的名称可以参考CGO官方文档(Command cgo)。Go references to C部分给出了可以引用的数值类型名称。

    综合两份参考文献,给出下面的类型列表。

    C Types

    Cgo  Types

    _Bool

    C._Bool

    char

    C.char

    signed char

    C.schar

    unsigned char

    C.uchar

    short

    C.short

    unsigned short

    C.ushort

    int

    C.int

    unsigned int

    C.uint

    long

    C.long

    unsigned long

    C.ulong

    long long

    C.longlong

    unsigned long long

    C.ulonglong

    float

    C.float

    double

    C.double

    long double

    不支持(unexpected: 16-byte float type - long double)

    float _Complex

    C.complexfloat

    double _Complex

    C.complexdouble

    long double _Complex

    不支持( unexpected: 32-byte complex type - complex long double)

    void*

    unsafe.Pointer

    __int128_t

    [16]byte

    __uint128_t

    [16]byte

    C语言中的数值类型long double和long double _Complex在CGO中不能引用,编译报错。

    写一个简单的程序来演示下:

    package main

    /*

    #include

    #include

    _Bool b = 1;

    char c = 'A';

    signed char sc = CHAR_MIN;

    unsigned char usc = UCHAR_MAX;

    short s = SHRT_MIN;

    unsigned short us = USHRT_MAX;

    int i = INT_MIN;

    unsigned int ui = UINT_MAX;

    long l = LONG_MIN;

    unsigned long ul = ULONG_MAX;

    long long ll = LONG_LONG_MIN;

    unsigned long long ull = ULONG_LONG_MAX;

    float f = FLT_MIN;

    double d = DBL_MIN;

    long double ld = LDBL_MIN;

    float _Complex fc = 5+5i;

    double _Complex dc = 5+5i;

    long double _Complex ldc = 5+5i;

    __int128_t i128 = 0;

    __uint128_t ui128 = 3.4028236692093846346337460743177e+38;

    void *ptr = 0;

    */

    import "C"

    import "fmt"

    import "unsafe"

    func main() {

    fmt.Println(C.b)

    fmt.Println(C.c)

    fmt.Println(C.sc)

    fmt.Println(C.usc)

    fmt.Println(C.s)

    fmt.Println(C.us)

    fmt.Println(C.i)

    fmt.Println(C.ui)

    fmt.Println(C.l)

    fmt.Println(C.ul)

    fmt.Println(C.ll)

    fmt.Println(C.ull)

    fmt.Println(C.f)

    fmt.Println(C.d)

    //fmt.Println(C.ld)

    fmt.Println(C.fc)

    fmt.Println(C.dc)

    //fmt.Println(C.ldc)

    fmt.Println(C.i128)

    fmt.Println(C.ui128)

    fmt.Println(C.ptr)

    fmt.Println("-------------------------------------------------")

    fmt.Println(C._Bool(true))

    fmt.Println(C.char('A'))

    fmt.Println(C.schar(-128))

    fmt.Println(C.uchar(255))

    fmt.Println(C.short(C.SHRT_MIN))

    fmt.Println(C.ushort(C.USHRT_MAX))

    fmt.Println(C.int(C.INT_MIN))

    fmt.Println(C.uint(C.UINT_MAX))

    fmt.Println(C.long(C.LONG_MIN))

    fmt.Println(C.ulong(C.ULONG_MAX))

    fmt.Println(C.longlong(C.LONG_LONG_MIN))

    fmt.Println(C.ulonglong(18446744073709551615))

    fmt.Println(C.float(-1))

    fmt.Println(C.double(-1))

    //fmt.Println(C.longdouble(1))

    fmt.Println(C.complexfloat(5 + 5i))

    fmt.Println(C.complexdouble(5 + 5i))

    C.i128 = [16]byte{15: 127}

    fmt.Println(C.i128)

    C.ui128 = [16]byte{15: 255}

    fmt.Println(C.ui128)

    C.ptr = unsafe.Pointer(nil)

    fmt.Println(C.ptr)

    }

    运行后,输出结果如下:(运行环境:Windows 7 64位操作系统,go version go1.8.3 windows/amd64)

    true

    65

    -128

    255

    -32768

    65535

    -2147483648

    4294967295

    -2147483648

    4294967295

    -9223372036854775808

    18446744073709551615

    1.1754944e-38

    2.2250738585072014e-308(5+5i)

    (5+5i)

    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

    [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255]

    -------------------------------------------------

    true

    65

    -128

    255

    -32768

    65535

    -2147483648

    4294967295

    -2147483648

    4294967295

    -9223372036854775808

    18446744073709551615

    -1

    -1(5+5i)

    (5+5i)

    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127]

    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255]

    展开全文
  • CGO dependency

    2020-11-27 23:07:30
    t depend on CGO, but now I can't build it with CGO_ENABLED=0 <h3>from head <pre><code>sh jackie:~/go-mssqldb/examples/tsql$ CGO_ENABLED=0 go build -ldflags '-linkmode external -...
  • cgo support

    2020-12-02 06:19:17
    <div><p>Adds a new macro <code>cgo_library, which wraps build steps with cgo. See the following thread in bazel-dev for the overall design. - ...
  • CGO使用

    2020-12-04 16:16:12
    文章目录CGO使用1、什么是CGO2、CGO环境基础3、开始使用4、类型转换4.1 数值类型4.2 字符串和切片,结构体、联合、枚举5、静态库的使用5.1. 静态库制作5.2 在golang程序中引入静态库6、C回调Go函数7、更多介绍 1、...
  • cgo enabled

    2021-01-09 00:50:15
    <div><p><strong>What is the problem ...<p>cgo enabled, because final stage contain c standard library. Also changed source path on stage builder</p><p>该提问来源于开源项目:gomods/athens</p></div>
  • cgo翻译

    2020-06-07 16:40:45
    cgo翻译command cgo在go command中使用cgo command cgo Cgo支持创建可以调用C代码的Go包。 在go command中使用cgo 为了在常规go代码中使用cgo,需要引入pseudo-package “C”。然后,Go代码可以引用诸如类型C.size_t...
  • cgo c数组_CGO编程

    2020-12-24 02:53:26
    1)什么是CGO编程?2)CGO语法3)相关资料一、什么是CGO编程?简单说就是GO语言代码中可以编写C代码和调用C代码,在一些偏底层业务中,用C来编写会比较简单,然后GO中来调用二、CGO语法1)简单案例2)GO与C语言类型转换3)...
  • cgo编程

    2020-11-13 09:18:31
    go语言通过自带的一个叫cgo的工具来支持c语言函数调用,同事我们可以用Go语言导出c动态库给其他语言使用。 2.1 快速入门 1、没有释放使用C.CString创建的C语言字符串会导致内存泄漏。 2、需要通过extern "C"语句...
  • Enable CGO and CGO_DEBUG

    2020-11-22 21:55:58
    There is a problem for Pods running Go applications compiled with the CGO_DEBUG flag disabled. So, I just enable it. See bug for details: https://bugzilla.redhat.com/show_bug.cgi?id=1786217 <p>...
  • Disable CGO

    2020-12-26 13:56:52
    s disable CGO entirely. <p>This means the system's networking configuration may not be entirely honoured. On *nix systems Go will read /etc/resolv.conf and use those dns servers, but otherwise ...
  • <div><p><code>go mobile</code> compilation for IOS overwrites <code>CGO_CFLAGS, <code>CGO_CXXFLAGS, and <code>CGO_LDFLAGS</code> on iOS Builds, which makes it impossible to link go code against C ...
  • cgo panic

    2021-01-09 18:51:09
    ve just upgraded to Go 1.9 and it looks like that reading from torrent throws a panic most likely caused by the cgo calls to go-libutp. It doesn't happen all the time(i.e. only the torrent is open...
  • Golang CGO

    2019-03-05 09:16:27
    CGO用于在GO代码中使用C语言编程,或者说是调用C代码封装的链接库文件中编写的函数,有些想用的东西GOLANG没有官方源码,但是C语言有时,就可以用CGO调用它,比如喜闻乐见的OpenCV。 一、错误之坑: 1、CGO报错 。...
  • Cannot use cgo

    2021-01-11 05:57:03
    t use cgo in a test package with Ginkgo. The failure is as follows: <pre><code> $ ginkgo . Failed to compile cgo_ginkgo: can't load package: package github....
  • add cgo support

    2020-12-02 19:51:12
    <div><p>This adds cgo support to gazelle. <p>A cgo_library rule called cgo_default_library is created if there are any CFiles, HFiles, or CgoFiles in the package. If there are no GoFiles, a go_library...
  • CGO编程

    2018-10-10 23:44:00
    1)什么是CGO编程? 2)CGO语法 3)相关资料 一、什么是CGO编程? 简单说就是GO语言代码中可以编写C代码和调用C代码,在一些偏底层业务中,用C来编写会比较简单,然后GO中来调用 二、CGO语法 1)简单案例 2)GO与...
  • cgo c数组_深入CGO编程

    2021-01-09 08:57:58
    作者简介柴 树 杉青云QingCloud应用平台...2010年开始参与和组织 Go 语言早期文档翻译,2013年正式转向Go语言开发,CGO资深用户。目录1. CGO的价值2. 快速入门3. 类型转换4. 函数调用5. CGO内部机制6. 实践:包装 ...
  • 作者简介柴 树 杉青云QingCloud应用平台...2010年开始参与和组织 Go 语言早期文档翻译,2013年正式转向Go语言开发,CGO资深用户。目录1. CGO的价值2. 快速入门3. 类型转换4. 函数调用5. CGO内部机制6. 实践:包装 ...
  • <div><p>When building the CGO_CFLAGS and CGO_LDFLAGS start with the systems current env values. Without this it's impossible to link with shared libs.</p><p>该提问来源于开源项目:go-flutter-...
  • cgo build fail

    2020-12-01 22:54:52
    cgo_default_library_filter_cgo_srcs') File "/home/travis/.cache/bazel/_bazel_travis/ef1dc36cffa27d24eeff815ad32c70b0/external/io_bazel_rules_go/go/def.bzl", line 850, in _cgo_filter_srcs_...
  • Reduce CGO dependency

    2020-12-09 04:26:35
    <div><p>CGO tends to make Go project hard to reuse in interesting ways. Static compilation and cross compilation become hard. There is a PoC here https://github.com/tomberek/desync/tree/tom/no_cgo ...
  • breaking CGO compabitility

    2020-12-04 12:55:55
    #cgo darwin LDFLAGS: -framework Foundation -framework MultipeerConnectivity #include <stdlib.h> #include "mc-driver.h" */ import "C" import "unsafe" [...] </stdlib.h&...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,586
精华内容 1,834
关键字:

cgo