Go: relocation target main.main not defined

Created on 8 Apr 2018  ·  17Comments  ·  Source: golang/go

Please answer these questions before submitting your issue. Thanks!

What did you do?

I was trying to out put some String "This is less" below is the program

package main

func main() {
    i := 3
    if i < 3 {
        println("This is less")
    }
}

What did you expect to see?

The output should be "This is less"

What did you see instead?

Each time it tells me that my main has not been defined. Yet I have already defined it. Here is a complete error message:

# command-line-arguments
runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"
dennis@dennis-HP-Notebook:~/Desktop/Go Proj

System details

go version go1.10.1 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dennis/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/dennis/go"
GORACE=""
GOROOT="/snap/go/1688"
GOTMPDIR=""
GOTOOLDIR="/snap/go/1688/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build360112992=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.10.1 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.10.1
uname -sr: Linux 4.13.0-37-generic
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.23-0ubuntu10) stable release version 2.23, by Roland McGrath et al.
gdb --version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
FrozenDueToAge NeedsInvestigation WaitingForInfo

Most helpful comment

@jaytaylor There is no main function in that file, so the error is normal and expected.

Whether this is the same problem as @dennismuturia I do not know.

All 17 comments

It seems like your installation is somehow broken - I cannot reproduce this issue. Does it happen with all Go programs and packages?

If you think this is a bug in Go, please provide a self-contained Go package that you are building, and exactly what commands you ran to encounter the failure.

Please run your go build command with the -x option and post the output here.

Something is broken with your installation or with how you are running the go command, but I don't know what.

I'm also running into this running from master built on March 24 2018.

Golang commit hash: 2ba98f1ae9381d78aea68cce63878df484b42812

[email protected]:~/go/src/github.com/jaytaylor/html2text$ go build -x -o ~/go/bin/html2text cmd/html2text.go
WORK=/var/folders/nq/5wj9b1zs1s13k5sfkmkr1kb80000gn/T/go-build246724669
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/jay/Library/Caches/go-build/ba/ba25579e9e615ffd7fb3d71b37746314cbfa7d444e02d2191f68d5c653379f3a-d
packagefile fmt=/usr/local/go/pkg/darwin_amd64/fmt.a
packagefile github.com/Sirupsen/logrus=/Users/jay/Library/Caches/go-build/a2/a228ca8873a5289d78196edc69fe8d0e3c4858be420e80e5abc819a1829ab42d-d
packagefile github.com/jaytaylor/html2text=/Users/jay/Library/Caches/go-build/7f/7f13fffcc6c8192a116b49e9881b0ce97d4b3dfbf3c4e9a2711849b06dee1ad6-d
packagefile github.com/spf13/cobra=/Users/jay/Library/Caches/go-build/f7/f71c52c383a8da89e6e38cfe3a8e82e3d2df31fb214cca9d58768eb1a3ca3710-d
packagefile os=/usr/local/go/pkg/darwin_amd64/os.a
packagefile runtime=/usr/local/go/pkg/darwin_amd64/runtime.a
packagefile errors=/usr/local/go/pkg/darwin_amd64/errors.a
packagefile io=/usr/local/go/pkg/darwin_amd64/io.a
packagefile math=/usr/local/go/pkg/darwin_amd64/math.a
packagefile reflect=/usr/local/go/pkg/darwin_amd64/reflect.a
packagefile strconv=/usr/local/go/pkg/darwin_amd64/strconv.a
packagefile sync=/usr/local/go/pkg/darwin_amd64/sync.a
packagefile unicode/utf8=/usr/local/go/pkg/darwin_amd64/unicode/utf8.a
packagefile bufio=/usr/local/go/pkg/darwin_amd64/bufio.a
packagefile bytes=/usr/local/go/pkg/darwin_amd64/bytes.a
packagefile encoding/json=/usr/local/go/pkg/darwin_amd64/encoding/json.a
packagefile golang.org/x/crypto/ssh/terminal=/Users/jay/Library/Caches/go-build/b5/b53af99379376720cef3cb53edf6741e6a18d4c60b8419f2447dcc6fe45a36f6-d
packagefile golang.org/x/sys/unix=/Users/jay/Library/Caches/go-build/58/58f047870b1d083ab668949862a1f821f7d6b9731ed7ed2312cc8eaa030d4888-d
packagefile log=/usr/local/go/pkg/darwin_amd64/log.a
packagefile sort=/usr/local/go/pkg/darwin_amd64/sort.a
packagefile strings=/usr/local/go/pkg/darwin_amd64/strings.a
packagefile sync/atomic=/usr/local/go/pkg/darwin_amd64/sync/atomic.a
packagefile time=/usr/local/go/pkg/darwin_amd64/time.a
packagefile github.com/olekukonko/tablewriter=/Users/jay/go/pkg/darwin_amd64/github.com/olekukonko/tablewriter.a
packagefile github.com/ssor/bom=/Users/jay/go/pkg/darwin_amd64/github.com/ssor/bom.a
packagefile golang.org/x/net/html=/Users/jay/Library/Caches/go-build/cd/cd85eefaec244b9a36898971ed32e1d34a13adb63bcf2e8da44124c8cae5def6-d
packagefile golang.org/x/net/html/atom=/Users/jay/Library/Caches/go-build/8e/8e0d24d4bfc2335a6bc55a1b98a4a1dd8fbcf39bd1182c02c2185a9e3e157543-d
packagefile regexp=/usr/local/go/pkg/darwin_amd64/regexp.a
packagefile unicode=/usr/local/go/pkg/darwin_amd64/unicode.a
packagefile github.com/spf13/pflag=/Users/jay/Library/Caches/go-build/29/2940bb43bed5dfcb449621b315f7e0f321436dfe42bacc999025b59abf8dc010-d
packagefile path/filepath=/usr/local/go/pkg/darwin_amd64/path/filepath.a
packagefile text/template=/usr/local/go/pkg/darwin_amd64/text/template.a
packagefile internal/poll=/usr/local/go/pkg/darwin_amd64/internal/poll.a
packagefile internal/testlog=/usr/local/go/pkg/darwin_amd64/internal/testlog.a
packagefile syscall=/usr/local/go/pkg/darwin_amd64/syscall.a
packagefile internal/bytealg=/usr/local/go/pkg/darwin_amd64/internal/bytealg.a
packagefile runtime/internal/atomic=/usr/local/go/pkg/darwin_amd64/runtime/internal/atomic.a
packagefile runtime/internal/sys=/usr/local/go/pkg/darwin_amd64/runtime/internal/sys.a
packagefile internal/cpu=/usr/local/go/pkg/darwin_amd64/internal/cpu.a
packagefile internal/race=/usr/local/go/pkg/darwin_amd64/internal/race.a
packagefile encoding=/usr/local/go/pkg/darwin_amd64/encoding.a
packagefile encoding/base64=/usr/local/go/pkg/darwin_amd64/encoding/base64.a
packagefile unicode/utf16=/usr/local/go/pkg/darwin_amd64/unicode/utf16.a
packagefile encoding/csv=/usr/local/go/pkg/darwin_amd64/encoding/csv.a
packagefile github.com/mattn/go-runewidth=/Users/jay/Library/Caches/go-build/d0/d096316f4d22bc11fa3ea6c64473a32c6b258ae4b27e3c92b0d9d37234d23584-d
packagefile io/ioutil=/usr/local/go/pkg/darwin_amd64/io/ioutil.a
packagefile regexp/syntax=/usr/local/go/pkg/darwin_amd64/regexp/syntax.a
packagefile encoding/hex=/usr/local/go/pkg/darwin_amd64/encoding/hex.a
packagefile flag=/usr/local/go/pkg/darwin_amd64/flag.a
packagefile net=/usr/local/go/pkg/darwin_amd64/net.a
packagefile net/url=/usr/local/go/pkg/darwin_amd64/net/url.a
packagefile text/template/parse=/usr/local/go/pkg/darwin_amd64/text/template/parse.a
packagefile encoding/binary=/usr/local/go/pkg/darwin_amd64/encoding/binary.a
packagefile context=/usr/local/go/pkg/darwin_amd64/context.a
packagefile vendor/golang_org/x/net/dns/dnsmessage=/usr/local/go/pkg/darwin_amd64/vendor/golang_org/x/net/dns/dnsmessage.a
packagefile vendor/golang_org/x/net/route=/usr/local/go/pkg/darwin_amd64/vendor/golang_org/x/net/route.a
packagefile internal/nettrace=/usr/local/go/pkg/darwin_amd64/internal/nettrace.a
packagefile internal/singleflight=/usr/local/go/pkg/darwin_amd64/internal/singleflight.a
packagefile math/rand=/usr/local/go/pkg/darwin_amd64/math/rand.a
packagefile runtime/cgo=/usr/local/go/pkg/darwin_amd64/runtime/cgo.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=bUE8BpzubBTAwFA6J13k/ZZTWtlBjwWxUq18tUMeB/rGuBd9gD9dTHh0j7Dl6R/bUE8BpzubBTAwFA6J13k -extld=clang /Users/jay/Library/Caches/go-build/ba/ba25579e9e615ffd7fb3d71b37746314cbfa7d444e02d2191f68d5c653379f3a-d
# command-line-arguments
runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"

Also just pulled latest go tip code and rebuilt, and still getting the same error.

Golang git commit hash: 49ed4cbe853e910b6f8d83012bc8b9afedb4b6b6

Here is the source code being referenced: https://github.com/jaytaylor/html2text/tree/jay/markdown

OS Info:

OS X 10.9.5

[email protected]:~/go/src/github.com/jaytaylor/html2text$ uname -a
Darwin jays-mba 13.4.0 Darwin Kernel Version 13.4.0: Mon Jan 11 18:17:34 PST 2016; root:xnu-2422.115.15~1/RELEASE_X86_64 x86_64

@jaytaylor Please show us the complete contents of cmd/html2text.go. Thanks.

@jaytaylor There is no main function in that file, so the error is normal and expected.

Whether this is the same problem as @dennismuturia I do not know.

Doh.. okay. Sorry for the noise then.

@dennismuturia Is this still a problem for you?

Yeah @ianlancetaylor.

@dennismuturia Above I asked for go build -x output showing the problem. Would it be possible to provide that? Thanks.

ETIMEOUT.

I hit this error when I accidentally defined func Main() instead of func main().

I encountered this issue after upgrading my operating system to macOS 10.13.

$ go build -v -i -o kltest ./cli
github.com/linkerd/linkerd2/vendor/k8s.io/client-go/discovery/fake.init: call to external function
github.com/linkerd/linkerd2/vendor/k8s.io/client-go/kubernetes/fake.init: relocation target github.com/linkerd/linkerd2/vendor/k8s.io/client-go/discovery/fake.init not defined
github.com/linkerd/linkerd2/vendor/k8s.io/client-go/kubernetes/fake.init: undefined: "github.com/linkerd/linkerd2/vendor/k8s.io/client-go/discovery/fake.init"

Interestingly, the build succeeds if I add the -race flag. For posterity, the full -x output for the failing build is available here:

https://gist.github.com/klingerf/57e0f6dd46f3951fb6ddcea2f4674a42

After removing my go pkg dir (rm -r $GOPATH/pkg), the rebuild succeeds, and the issue is fixed. There's probably a better way to go about this than blowing away the whole pkg dir, however.

No lance. Had reinstall the package.

[image: photo]
Dennis Muturia
Software Developer

+254711667171 | [email protected]

[email protected]
Nairobi, 00100
http://www.linkedin.com/in/dennis-muturia-a53a50130/
http://github.com/dennismuturia
Create your own email signature
https://www.wisestamp.com/signature-in-email?utm_source=promotion&utm_medium=signature&utm_campaign=create_your_own

On Wed, Apr 18, 2018 at 3:17 AM, Ian Lance Taylor notifications@github.com
wrote:

@dennismuturia https://github.com/dennismuturia Is this still a problem
for you?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/golang/go/issues/24764#issuecomment-382200005, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AV-2Y34acjOrV5DmhH9lK616vXwmQwmCks5tpoY3gaJpZM4TLejG
.

There's probably a better way to go about this than blowing away the whole pkg dir, however.

@klingerf Have you tried go clean -i -r -cache ?

@agnivade Ah, thanks! I hadn't seen the clean subcommand before.

Was this page helpful?
0 / 5 - 0 ratings