英文:
Is there a way to include multiple c-archive packages in a single binary
问题
我正在尝试将多个Go c-archive包包含在一个单独的C二进制文件中,但由于每个c-archive中都包含了完整的运行时,所以出现了多个定义错误。
我尝试将多个包放在同一个c-archive中,但是go build不允许这样做。
我还尝试从除一个之外的所有存档中删除go.o
,但似乎我的Go代码也在那个目标文件中,所以这种方法行不通,而且这也是我出现多个定义的原因,而不是链接器忽略后续存档中的go.o
。
如果使用c-shared
而不是c-archive
可能会起作用,但我不想这样做,因为这样我就必须将共享库放在目标机器上,这比只需将最终程序二进制文件放在那里要复杂。如果可能的话,我希望所有内容都是静态链接的。
有没有办法让这个工作起来?如果只在Linux上使用也可以接受(在这种情况下可能需要一些GNU ld
的技巧)。
将所有内容放在一个单独的Go包中不是一个真正的选择,因为它是一个相当大的代码库,不同的程序可能需要不同的部分。在这种情况下,它必须是一个自动生成的包。
复现问题的完整步骤:
cd $GOPATH/src
mkdir a b
cat > a/a.go <<EOT
package main
import "C"
//export a
func a() {
println("a")
}
func main() {}
EOT
cat > b/b.go <<EOT
package main
import "C"
//export b
func b() {
println("b")
}
func main() {}
EOT
cat > test.c <<EOT
#include "a.h"
#include "b.h"
int
main(int argc, char *argv[]) {
a();
b();
}
EOT
go build -buildmode=c-archive -o a.a a
go build -buildmode=c-archive -o b.a b
gcc test.c a.a b.a
英文:
I'm trying to include multiple Go c-archive packages in a single C binary, but I'm getting multiple definition errors due to the full runtime being included in each c-archive.
I've tried putting multiple packages in the same c-archive but go build does not allow this.
I've also tried removing go.o
from all the archives except one, but it seems my own Go code is also in that object file so that doesn't work, and it's even the reason I get multiple defines instead of the linker ignoring go.o
from subsequent archives.
It would probably work to use c-shared
instead of c-archive
, but I don't wish to do that as I then have to put the shared libraries on my target machine, which is more complicated compared to just putting the final program binary there. I'd like everything to be statically linked if possible.
Is there a way to get this working? I can accept a linux only solution if that matters (some GNU ld
trickery probably in that case).
Putting everything in a single Go package is not really an option, since it's a fairly large code base and there would be different programs wanting different parts. It would have to be an auto-generated package in that case.
Full steps to reproduce the problem:
cd $GOPATH/src
mkdir a b
cat > a/a.go <<EOT
package main
import "C"
//export a
func a() {
println("a")
}
func main() {}
EOT
cat > b/b.go <<EOT
package main
import "C"
//export b
func b() {
println("b")
}
func main() {}
EOT
cat > test.c <<EOT
#include "a.h"
#include "b.h"
int
main(int argc, char *argv[]) {
a();
b();
}
EOT
go build -buildmode=c-archive -o a.a a
go build -buildmode=c-archive -o b.a b
gcc test.c a.a b.a
答案1
得分: 2
我今天在看到你的问题后费了些周折才解决了这个问题。
关键是要定义一个单独的主包,导入你需要的包,并使用单个"go install"命令将它们全部构建在一起。我无法通过"go build"命令来实现这个。
package main //import golib
import (
_ "golib/operations/bar"
_ "golib/foo"
)
func main() {}
go install -buildmode=c-archive golib
这将在 pkg/arch/golib 目录下放置你的 .a 和 .h 文件。你可以像往常一样包含 .h 文件,但你只需要链接到 golib.a 文件。
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls
github.com golang.org golib golib.a
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls golib
foo.a foo.h operations
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls golib/operations
bar.a bar.h
请注意,如果你在导入语句中省略下划线,Go 会抱怨未使用的包。
英文:
I fumbled my way through this today after coming across your question.
The key is to define a single main package that imports the packages that you need and build them all together with a single "go install" command. I was unable to get this to work with "go build".
package main //import golib
import (
_ "golib/operations/bar"
_ "golib/foo"
)
func main() {}
go install -buildmode=c-archive golib
This will place your .a and .h files under a pkg/arch/golib directory. You can include the .h files as usual, but you only need to link against golib.a
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls
github.com golang.org golib golib.a
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls golib
foo.a foo.h operations
aaron@aaron-laptop:~/code/pkg/linux_amd64$ ls golib/operations
bar.a bar.h
Note that go will complain about unused packages if you omit the underscore in the import.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论