Skip to content

Go绑定动态连接失败 #15

@AkimioJR

Description

@AkimioJR

运行go test测试时发生问题:

CoralReefPlayer/build/install on  v1 via C v17.0.0-clang via 🐹 v1.25.3 
❯ go test                                   
# oureda.cn/gcrp.test
/opt/homebrew/Cellar/go/1.25.3/libexec/pkg/tool/darwin_arm64/link: running cc failed: exit status 1
/usr/bin/cc -arch arm64 -Wl,-S -Wl,-x -o $WORK/b001/gcrp.test -Qunused-arguments /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/go.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000000.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000001.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000002.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000003.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000004.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000005.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000006.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000007.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000008.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000009.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000010.o /var/folders/x3/p_mvf5p973d0_nvxxkfmvgzr0000gn/T/go-link-677538093/000011.o -O2 -g -L/Users/akimio/Documents/DeepLab/2026/crp/CoralReefPlayer/build/install -lCoralReefPlayer -lresolv -O2 -g -framework CoreFoundation
ld: library 'CoralReefPlayer' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决方法:

Ran terminal command: go test

Ran terminal command: otool -L libCoralReefPlayer.dylib

Ran terminal command: find . -name "*CoralReefPlayer*" -type d

Ran terminal command: cd CoralReefPlayer/build/install && ls -la | grep -i coral

Ran terminal command: install_name_tool -id @rpath/libCoralReefPlayer.dylib libCoralReefPlayer.dylib

Ran terminal command: otool -L libCoralReefPlayer.dylib | head -5

注意事项

  • 平台差异

    • Linux:直接使用 -L. -lCoralReefPlayer
    • macOS:需要rpath设置和库文件重命名
    • Windows:使用 .dll 文件
  • 依赖管理

    • Go包包含所有必要的C库和头文件
    • 无需额外安装CoralReefPlayer
  • 版本兼容性

    • 当前版本:v1.4
    • 确保Go版本 >= 1.18
  • 常见问题

    • 链接错误:检查cgo LDFLAGS配置
    • 运行时错误:确保库文件在运行时路径中

通过以上步骤,您可以成功编译和使用CoralReefPlayer的Golang绑定,实现低延迟的流媒体播放功能。

install_name_tool -id @rpath/libCoralReefPlayer.dylib libCoralReefPlayer.dylib命令解析

  • install_name_tool:macOS提供的工具,用于修改Mach-O二进制文件的动态库引用信息
  • -id 选项:设置动态库本身的标识符(install name),即这个库的"身份"
  • @rpath/libCoralReefPlayer.dylib:新的install name
    • @rpath 是macOS的运行时路径占位符,表示在运行时搜索路径中查找
    • libCoralReefPlayer.dylib 是库文件名
  • libCoralReefPlayer.dylib:要修改的库文件

为什么需要这个命令

在macOS上,当一个可执行文件或库需要加载动态库时,它会根据库的install name来查找。原始的CoralReefPlayer库可能被设置为引用framework路径(如@rpath/CoralReefPlayer.framework/Versions/A/CoralReefPlayer),但我们需要它作为独立的dylib文件工作。

通过这个命令,我们:

  1. 将库的install name改为@rpath/libCoralReefPlayer.dylib
  2. 这样Go程序在运行时可以通过设置的rpath(-Wl,-rpath,.)找到这个库

工作原理

  • 修改前:库的ID可能指向framework路径
  • 修改后:库的ID指向@rpath/libCoralReefPlayer.dylib
  • 运行时:当Go程序加载这个库时,系统会在rpath指定的目录中查找libCoralReefPlayer.dylib

这解决了macOS上动态库链接的问题,确保Go绑定能够正确加载CoralReefPlayer库。

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions