herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
452 stars 152 forks source link

Want to use static library in a golang project. #54

Closed xcshuan closed 5 years ago

xcshuan commented 5 years ago

I want to use the library in a golang project use cgo, first, I use "sudo make install" and the libraries install in "/usr/local", but "/usr/local/lib" has only "libmcl.a" and "libmcl.so", and I move "libmclbn384.a" and "libmclbn384.so" from "mcl/lib" to "/usr/local/lib" and the go code works well.

1752504875

but if I want to use pure static library(copy the .a and .h to golang project), How should I do? I think I should change all the header file about path, and specify the link path where the static library locate. I try to do that, but don't work. Is there some idea to do that? I'm not familiar with C++.

/*
#cgo  CFLAGS:-DMCLBN_FP_UNIT_SIZE=6
#cgo LDFLAGS:-L ./lib -lmclbn384 -lmcl
#include "./include/bn.h"
*/
import "C"
import "fmt"
import "unsafe"
herumi commented 5 years ago

If there are a shared library and a static library having the same name in the same directory, then the shared library is linked. So one way to link static library is as the followings:

  1. copy only libmcl.a and libmclbn384_256.a in /usr/local/lib (don't copy shared library)
  2. env CGO_LDFLAGS="-lmclbn384_256 -lmcl -lstdc++ -lgmp -lgmpxx -lcrypto" go build -tags bn384_256 aaa.go
cat aaa.go
package main
import (
    "fmt"
    "github.com/herumi/mcl/ffi/go/mcl"
)

func main() {
    mcl.Init(mcl.BLS12_381)
    var a mcl.Fr
    a.SetString("256", 10)
    fmt.Printf("a=%s\n", a.GetString(16))
}
xcshuan commented 5 years ago

@herumi Thanks.... I have tested your library in our project, it works well.