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

前端 未结 4 1664
感情败类
感情败类 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:56

    A straightforward Makefile to link Go code with a dynamic or static library:

    static:
        gcc -c gb.c
        ar -rcs libgb.a gb.o
        go build -ldflags "-linkmode external -extldflags -static" bridge.go
    
    dynamic:
        gcc -shared -o libgb.so gb.c
        go build bridge.go
    

    Directives in bridge.go:

    /*
    #cgo CFLAGS: -I.
    #cgo LDFLAGS: -L. -lgb
    #include "gb.h"
    */
    import "C"
    ...
    

提交回复
热议问题