首页 > 解决方案 > cgo - 在 GO 中链接动态库时出现“未定义引用”错误

问题描述

我有一个 C++ 共享库/opt/xx/lib64。标头在 C 中。在我的 go 代码中,我使用 CPPFLAGS/CFLAGS 和 LDFLAGS 包含正确的标头和链接器选项。仍然在构建过程中,我收到“未定义的引用”错误。

import (
    //#cgo LDFLAGS: -L/opt/xxx/lib64/ -lsessionmgmt
    //#cgo CPPFLAGS: -I/opt/xxx/include/
    //#include <stdlib.h>
    //#include <sessionMgmt/sessionMgmt.h>
    "C"
    "unsafe"
)
/tmp/go-build609413262/b163/_x002.o: In function `_cgo_e38947506e9d_Cfunc_um_get_lastlogin_info':
/tmp/go-build/cgo-gcc-prolog:52: undefined reference to `um_get_lastlogin_info'
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:48: vet] Error 2

库存在于正确的路径中,并且确实包含所需的方法。不知道为什么会出现这个错误。

$ grep um_get_lastlogin_info /opt/xxx/lib64/libsessionmgmt.so Binary file /opt/xxx/lib64/libsessionmgmt.so matches

还尝试使用pkg-configwith cgo。仍然出现相同的链接错误。

标签: golinker-errorscgo

解决方案


推荐阅读