当运行“go build”或“go run main.go”时,“pkcs11.h”出现问题

yftpprvb  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(181)

我正在做一个使用PKCS11的CGO代码,在启用CGO和一些错误,测试和更正之后,我发现了一个我从未见过的新错误。无论我使用“go build”还是“go run main.go”,错误仍然出现。有人知道如何修复它吗?

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/miekg/pkcs11"
  5. )
  6. func main() {
  7. p := pkcs11.New("aetpkss1.dll")
  8. err := p.Initialize()
  9. if err != nil {
  10. panic(err)
  11. }
  12. defer p.Destroy()
  13. defer p.Finalize()
  14. slots, err := p.GetSlotList(true)
  15. if err != nil {
  16. panic(err)
  17. }
  18. session, err := p.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
  19. if err != nil {
  20. panic(err)
  21. }
  22. defer p.CloseSession(session)
  23. err = p.Login(session, pkcs11.CKU_USER, "1234")
  24. if err != nil {
  25. panic(err)
  26. }
  27. defer p.Logout(session)
  28. p.DigestInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA_1, nil)})
  29. hash, err := p.Digest(session, []byte("this is a string"))
  30. if err != nil {
  31. panic(err)
  32. }
  33. for _, d := range hash {
  34. fmt.Printf("%x", d)
  35. }
  36. fmt.Println()
  37. }
  1. C:\Users\user\Desktop\Teste2> go run main.go
  2. # runtime/cgo
  3. cc1.exe: error: C:\src\github.com\miekg\pkcs11\pkcs11.h: not a directory [-Werror]
  4. cc1.exe: all warnings being treated as errors
xuo3flqw

xuo3flqw1#

你需要安装包pkcs11,你可以这样命令

  1. go get -u github.com/miekg/pkcs11

字符串
你需要在你的系统上有aetpkss1.dll PKCS#11库。它在正确的位置并正确安装。
检查环境变量CGO_ENABLED和CC是否正确设置。您可能需要设置这些变量以指定要使用和启用CGO的C编译器。例如:

  1. export CGO_ENABLED=1
  2. export CC=gcc

相关问题