doushun1904 2019-08-20 09:03
浏览 335

在go项目中使用cgo包含libsodium失败

I am trying to include the libsodium into my Go project. For that, I've copied the repo inside my project

// #cgo CFLAGS: -I/mypath/libsodium/src/libsodium/include/sodium
// #include <stdlib.h>
// #include "crypto_sign_ed25519.h"
import "C"

When trying to build the project I get the following error:

/tmp/go-build/cgo-gcc-prolog:53: undefined reference to `crypto_sign_ed25519_pk_to_curve25519'
collect2: error: ld returned 1 exit status

The file can be found but the error is there. I've also tried to reference the '.c' file as well as to copy the crypto_sign_ed25519.h into the src folder but it does not work. My question is do I have to add LDFLAGS and therefore generate a .so file from the library or that is not needed and there is another possible way of doing it?

UPDATE: I've achieved to make it running by installing the library on my local ubuntu:

$ ./configure
$ make && make check
$ sudo make install

and adding // #cgo LDFLAGS: -L/usr/local/lib -lsodium But how can I do it without adding the local path?

  • 写回答

2条回答 默认 最新

  • doujiushi9007 2019-08-20 15:36
    关注

    You indeed need to link the library, the headers themselves are only the interface to the library and don't link the actual libsodium code to your binary.

    Assuming libsodium ships a pkg-config file (it seems to be the case), you can use something like

    // #cgo pkg-config: libsodium
    // #include "crypto_sign_ed25519.h"
    

    See https://golang.org/cmd/cgo/ for more information about pkg-config support.

    To see what cflags/libs you'd be getting (so what cgo will use), run:

    pkg-config --cflags --libs libsodium
    
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法