dongzexi5125 2018-07-11 02:57
浏览 104
已采纳

使用CGO动态选择正确的操作系统和处理器架构

I have access only to a linux and darwin distributions of dynamically linked shared libraries.

Following is how I've integrated the library with Golang

// #cgo LDFLAGS: -L${SRCDIR}/build -lprocessing_lib
// #include "Processing-bridge.h"
// #include <stdlib.h>
import "C"
import "unsafe"

type ProcessorWrapper struct {
    ptr unsafe.Pointer
}

func init() {
    pr.ptr = C.NewProcessor()
}

func GetDefault() (id int, name string) {
    var default = C.GetDefault(pr.ptr)
    id = int(default.materialId)
    name = C.GoString(default.name)
    return
}

This works perfectly fine when I copy the matching library under the build/ folder.

I'm trying to make this as a go gettable library , where it could work on both linux and darwing architectures.

Problem I'm having is to tell go compiler to pick the right library for the matching GOOS and GOARCH.

I tried having a folder structure like below

build/darwing/libprocessing_lib.so
build/linux/libprocessing_lib.so

And modified the LDFLAGS as below

// #cgo LDFLAGS: -L${SRCDIR}/build/${GOOS} -lprocessing_lib

However it doesn't recognise ${GOOS} and replace it with the correct value.

Is there a way to achieve this dynamic library pickup feature?

  • 写回答

2条回答 默认 最新

  • doudansui6650 2018-07-11 05:14
    关注

    Found an easy way to achive this without creating multiple files by looking at the go source code it self cgo

    // #cgo darwin LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib
    // #cgo linux LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib
    

    we can also pass the Architecture if needed like below

    // #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?