顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2015年12月28日 星期一

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