How do you statically link a c library in go using cgo?

前端 未结 4 1669
感情败类
感情败类 2020-11-28 23:22

So there\'s a bunch of stuff on the group that suggests you can do this in go (although not on the cgo documentation):

package bridge

import \"fmt\"

// #cg         


        
4条回答
  •  遥遥无期
    2020-11-28 23:45

    You just have to link with -Ldirectory -lgb.

    $ cat >toto.c
    int x( int y ) { return y+1; }
    $ cat >toto.h
    int x(int);
    $ gcc -O2 -c toto.c
    $ ar q libgb.a toto.o
    $ cat >test.go
    package main
    
    import "fmt"
    
    // #cgo CFLAGS: -I.
    // #cgo LDFLAGS: -L. -lgb 
    // #include 
    import "C"
    
    func main() {
      fmt.Printf("Invoking c library...\n")
      fmt.Println("Done ", C.x(10) )
    }
    $ go build test.go
    $ ./test
    Invoking c library...
    Done  11
    

提交回复
热议问题