如何使用gccgo导入非标准库的包

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

How to import non-standard-library packages use gccgo

问题

首先,所有这些代码都可以使用go工具(例如go build,go install)成功构建。

比如说,我有一个a.go文件,试图从github导入一个非标准库的包:

package a

import (
    "fmt"
    "github.com/usr/pkg"
)

func init() {
    fmt.Println("Import pkg", pkg.somevar)
}

当我尝试使用gccgo编译它时:

$ gccgo -c a.go
a.go:5:20: 错误:找不到导入文件‘github.com/usr/pkg’
...

然后我阅读了设置和使用gccgo,它说:

> 当你使用gccgo导入包FILE时,它会在以下文件中查找导入数据,并使用找到的第一个文件。
>
> FILE.gox
> FILE.o
> libFILE.so
> libFILE.a
>
> gccgo编译器会在当前目录中查找导入文件。

所以我将$GOPATH/pkg/github.com/usr/pkg.a复制到当前目录,并将其重命名为libpkg.a。

似乎又失败了:

$ gccgo -c a.go
a.go:9:4: 错误:libpkg.a:在第8个字符处的归档头名称格式错误
a.go:9:4: 错误:libpkg.a存在,但不包含任何Go导出数据

是的,我使用的是gccgo 4.7.2

我没有使用gcc的经验,所以在这里寻求一些帮助。

英文:

First of all, all these code can be built successfully using go tool(e.g. go build, go install)

For say I got an a.go which tries to import a non-standard-library pkg from github:

package a

import (
    "fmt"
    "github.com/usr/pkg"
)

func init() {
    fmt.Println("Import pkg", pkg.somevar)
}

when I try to compile it with gccgo:

$ gccgo -c a.go
a.go:5:20: error: import file ‘github.com/usr/pkg’ not found
...

And then I read the Setting up and using gccgo , it says

> When you import the package FILE with gccgo, it will look for the
> import data in the following files, and use the first one that it
> finds.
>
> FILE.gox
> FILE.o
> libFILE.so
> libFILE.a
>
> The gccgo compiler will look in the current directory for import files

So I cp the $GOPATH/pkg/github.com/usr/pkg.a to the current directory and rename it as libpkg.a.

It seems failed again:

$ gccgo -c a.go
a.go:9:4: error: libpkg.a: malformed archive header name at 8
a.go:9:4: error: libpkg.a exists but does not contain any Go export data

And yes, I use gccgo 4.7.2

I've got no experience woking with gcc, so I search for some help here.

答案1

得分: 8

最简单的方法是使用gc发行版中的go命令,并运行go build -compiler gccgo .

复制pkg.a的想法行不通,因为pkg.a不是使用gccgo构建的。

英文:

The simplest way is to use the go command from the gc distribution, and run go build -compiler gccgo .

Your idea of copying pkg.a does not work because pkg.a was not built with gccgo.

huangapple
  • 本文由 发表于 2012年11月22日 16:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/13508332.html
匿名

发表评论

匿名网友

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

确定