2015年2月7日 星期六

Golang use C library

  • cgo can compile C code
  • cgo can not compile C++ code (see Swig for that)
  • cgo supports both dynamic and static linking of compiled C code
  • by default, cgo will dynamically link (more on this later)
  • cgo does not support relative lookups of C files
  • #cgo pkg-config
Example :
$ vi 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 




References :
How do you statically link a c library in go using cgo? - Stack Overflow
A little trick to statically link C to Golang with cgo
Going Go Programming: Using C Dynamic Libraries In Go Programs

沒有留言:

張貼留言