go 1.10: cannot implicitly include runtime/cgo in a shared library
Asked Answered
B

3

8

I'm trying to execute go build -buildmode=shared -linkshared test but I'm getting the error:

go 1.10: cannot implicitly include runtime/cgo in a shared library

I have only one file in that package

lib.go

package test
import "fmt"
func Hello() {
    fmt.Println("Hi")
}

I'm on a linux machine running go1.10.4 linux/amd64

Bohner answered 4/9, 2019 at 18:13 Comment(6)
The same error occurs with this command as well: go build -buildmode=shared. I'm using go 1.13.Novice
Did you see this issue on the golang repo about the same error? github.com/golang/go/issues/17177Semanteme
What are you trying to achieve?Volnak
@Volnak Make a shared library (lib.so)Bohner
What happens if you try CGO_ENABLED=0 go build -buildmode=shared -linkshared test?Semanteme
maybe it's because you don't have any cgo code? Why are you trying to make an .so library in a non c/c++ code?Forgot
B
0

The problem was I didn't import cgo anywhere. So that means no functions were to be exported which means there's nothing to build into a library.

Bohner answered 30/3, 2020 at 4:1 Comment(0)
R
0

It worked for me with this procedure:

$ docker run -it golang /bin/sh
# cd src
# mkdir test
# cat << EOF > test/lib.go
> package test
import "fmt"
func Hello() {
    fmt.Println("Hi")
}> > > > 
> EOF
# cat test/lib.go
package test
import "fmt"
func Hello() {
    fmt.Println("Hi")
}
# go install -buildmode=shared -linkshared std
# ldd /usr/local/go/pkg/linux_amd64_dynlink/libstd.so
    linux-vdso.so.1 (0x00007fff4093d000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3454de5000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3454dc4000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3454c03000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f3457514000)
# go install -buildmode=shared -linkshared test
# ldd /go/pkg/linux_amd64_dynlink/libtest.so
    linux-vdso.so.1 (0x00007ffddbbeb000)
    libstd.so => /usr/local/go/pkg/linux_amd64_dynlink/libstd.so (0x00007f82bc8cf000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f82bc70a000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f82bc705000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f82bc6e4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f82bf0fc000)
# 
Raggedy answered 27/3, 2020 at 1:13 Comment(0)
B
0

The problem was I didn't import cgo anywhere. So that means no functions were to be exported which means there's nothing to build into a library.

Bohner answered 30/3, 2020 at 4:1 Comment(0)
W
0

Need to create go std shared library first. go install -buildmode=shared std

Winery answered 13/9 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.