Go: plugin: runtime.main_main·f: undefined: "main.main"(buildmode=plugin)

Created on 10 May 2017  ·  17Comments  ·  Source: golang/go

I want to build a plugin for my program, and the main content is ...

package main

import (
        "mylib"
        ......
)

The package "mylib" is a package of my defined, when I import it and then build it, the error will occur ...

runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"

why? Thanks a lot!

FrozenDueToAge WaitingForInfo

Most helpful comment

In my case I had not defined a function called main i.e. func main(){}

All 17 comments

@kenkao, please provide a complete repro (code & commands you ran), and include the output of go bug.

@crawshaw, maybe you recognize this as a dup of something else?

@bradfitz
Thanks for your replying!
I have found the answer, because I import the package "plugin" in "mylib" . (Σ( ° △ °|||)︴)
I want to use the "go build" to build the main app, and use "go build plugin" to build the "main.so", then I can let the main to load the "main.so" for itself's hotloading
Can't I ?

No clue.

@bradfitz
The source code is here:
https://github.com/kenkao/plugintest

go version go1.8 linux/amd64

cd main/
go build -ldflags "-pluginpath=plugin/hot-$(date +%s)" -buildmode=plugin -o gplugin.so gplugin.go

You have 2 main functions defined in your main package. I was getting a similar error when I had none defined in a package main.

In my case I had not defined a function called main i.e. func main(){}

[root@localhost main]#  go build -ldflags "-pluginpath=plugin/hot-$(date +%s)" -buildmode=plugin -o gplugin.so gplugin.go

command-line-arguments

runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"
[root@localhost main]#

@jimmiebtlr
the 'main' func in gplugin.go is an redundant code block for my debuging, i have deleted it. but when you want to build the gplugin.go to a plugin.so, the same error will occur..
i guess that you cannot import any package imported the 'plugin' package (╮( ̄▽ ̄")╭), when you want to build it to a plugin.so (the 'gplugin' import the 'mylib', and the 'mylib' import the 'plugin')
i think it may be beneficial to the security of golang..

my hot-update based on plugin for go has succeeded! ^ ^

It sounds like this was resolved when you added func main() {} to your plugin. For now, the main function is required and documented, so I'm going to close this.

(If I misunderstood this issue, please comment and I'll reopen it.)

@crawshaw
When import plugin in plugin:

runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"

demo

You need to add func main() {} to your plugin.

@crawshaw
Sorry, I have added func main () {} to plugin/plugin.go and models/models.go, but still hold the wrong.

runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"

demo

Hello I've a same this problem.
but... I've 3 folders for 3 applications (app-test, app-update, app-deploy)
1st app-test it will be ok and running as server
but when I create second app-update for check heartbeat of first app that i cant use "package main" but when I need run it that I can't
What/How I can fix it... Thank you

@KubisGroup Please ask questions on a forum; see https://golang.org/wiki/Questions. You will get a better and faster response. When asking your question please explain exactly what you are doing and exactly what happens. Thanks.

I came across the same issue when I executed go run main.go.
Demo Repo

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

Was this page helpful?
0 / 5 - 0 ratings