GoLang: main找不到模块使用的共享库

huangapple go评论68阅读模式
英文:

GoLang: main not finding shared library used by module

问题

我正在构建一个Go模块,大致如下所示:

package eos

/*
#cgo LDFLAGS: -L${SRCDIR}/vendor/MyLibrary/1.14.2/Bin/Linux -lMY_SDK-Linux-Shipping
#cgo CFLAGS: -I ${SRCDIR}/vendor/MyLibrary/1.14.2/Include
#include "my_sdk.h"*/
import "C"

func useMyCFunc() void {
	C.myCFunc()
}

然后我有一个名为hello.go的主文件:

package main

import (
	"helloworld/eos"
)

func main() {
	eos.useMyCFunc()
}

目录结构如下:

-helloworld/
|
- hello/
    |
    - hello.go
|
-eos/
    |
    - eos.go
    |
    - vendor/
        |
        - (所有所需的库文件)

这些是我用来构建程序的命令:

cd eos
go mod init helloworld/eos
cd ../hello
go mod init helloworld/hello
go mod edit -replace helloworld/eos=../eos
go mod tidy
go build .

这些命令都没有返回任何错误。然而,当我从/hello目录运行go run .时,我得到以下错误:

/tmp/go-build2142984290/b001/exe/hello: error while loading shared libraries: libMY_SDK-Linux-Shipping.so: cannot open shared object file: No such file or directory

这个文件确实存在,并且位于helloworld/eos/vendor/MyLibrary/1.14.2/Bin/Linux/libMY_SDK-Linux-Shipping.so,正如eos.go中的LDFLAGS所指定的那样。

为什么hello.go找不到这个文件?

英文:

I'm building a module in go, it looks something like this:

package eos

/*
#cgo LDFLAGS: -L${SRCDIR}/vendor/MyLibrary/1.14.2/Bin/Linux -lMY_SDK-Linux-Shipping
#cgo CFLAGS: -I ${SRCDIR}/vendor/MyLibrary/1.14.2/Include
#include "my_sdk.h"*/
import "C"

func useMyCFunc() void {
	C.myCFunc()
}

Then I have hello.go which is my main:

package main

import (
	"helloworld/eos"
)

func main() {
	eos.useMyCFunc()
}

The directory structure is as follows:

-helloworld/
|
- hello/
    |
    - hello.go
|
-eos/
    |
    - eos.go
    |
    - vendor/
        |
        - (all the required lib files)

These are the commands I run to build the program:

cd eos
go mod init helloworld/eos
cd ../hello
go mod init helloworld/hello
go mod edit -replace helloworld/eos=../eos
go mod tidy
go build .

None of these return any kind of error.
However, when I run go run . from /hello, I get this error:

/tmp/go-build2142984290/b001/exe/hello: error while loading shared libraries: libMY_SDK-Linux-Shipping.so: cannot open shared object file: No such file or directory

This file does exist, and is located at helloworld/eos/vendor/MyLibrary/1.14.2/Bin/Linux/libMY_SDK-Linux-Shipping.so, as specified in the LDFLAGS of eos.go.

Why can hello.go not find this file?

答案1

得分: 1

关于@rocka2q提到的CGo,除此之外,useMyCFunc()需要大写才能在eos包之外访问。

英文:

On top of what @rocka2q mentioned about CGo, useMyCFunc() needs to be capitalized for it to be accessible to outside of the eos package.

huangapple
  • 本文由 发表于 2022年4月25日 19:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71998889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定