Statically compile and link a go program with C dependencies

1,339 views
Skip to first unread message

snmed

unread,
Jan 21, 2019, 10:50:27 AM1/21/19
to golang-nuts
Hi all 

I try to compile and statically link the following example: https://github.com/rthornton128/goncurses/tree/master/examples/curs_menu
I use this command:
go build -ldflags '-w -extldflags "-static"' -a

Unfortunately i get only this:
/usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1

/usr/bin/ld: cannot find -lformw

/usr/bin/ld: cannot find -lmenuw

/usr/bin/ld: cannot find -lncursesw

/usr/bin/ld: cannot find -lpanelw

collect2
: error: ld returned 1 exit status



I have verified that all necessary libs are under /lib64, but the linker still complains. I'd appreciate any clues on this topic.

Cheers,

Kurtis Rader

unread,
Jan 21, 2019, 11:40:54 AM1/21/19
to snmed, golang-nuts
You say you "verified that all necessary libs are under /lib64." Do you actually have static versions of those libraries or just dynamic versions? For example, on macOS I only have dynamic (ending in .dylib) versions so it isn't possible to statically link a binary that requires those libraries. The "file" command should tell you whether the library is dynamic or static. But in general a static lib will have a ".a" extension and dynamic libs will have a ".so" or ".dylib" extension. "In general" because this is platform dependent and you could be working on a platform with different conventions.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

snmed

unread,
Jan 21, 2019, 2:27:17 PM1/21/19
to golang-nuts
Hi thanks,

Ah yes, it seems that i forgot that with the static library. Now i've the static library under ./libs folder and i use this command:
go build -ldflags '-L./libs -w -extldflags "-static"' -a

but now i get this message:
flag provided but not defined: -L./libs

usage
: link [options] main.o

 
-B note

        add an ELF NT_GNU_BUILD_ID note
when using ELF

 
-D address

       
set data segment address (default -1)

 
-E entry

       
set entry symbol name

 
-H type

       
set header type

 
-I linker

...rest omitted for brevity

am i using the command wrong or is there a flag i'm missing?

Justin Israel

unread,
Jan 21, 2019, 2:38:37 PM1/21/19
to snmed, golang-nuts


On Tue, Jan 22, 2019, 8:27 AM snmed <sandro....@gmail.com> wrote:
Hi thanks,

Ah yes, it seems that i forgot that with the static library. Now i've the static library under ./libs folder and i use this command:
go build -ldflags '-L./libs -w -extldflags "-static"' -a

but now i get this message:
flag provided but not defined: -L./libs

usage
: link [options] main.o

 
-B note

        add an ELF NT_GNU_BUILD_ID note
when using ELF

 
-D address

       
set data segment address (default -1)

 
-E entry

       
set entry symbol name

 
-H type

       
set header type

 
-I linker

...rest omitted for brevity

am i using the command wrong or is there a flag i'm missing?

That is the flag for the Go tool linker. Check out this page:

You can either define external linking options within your source or with CGO_LDFLAGS env var
Reply all
Reply to author
Forward
0 new messages