Go v 1.5.
1) Compile the package worker:
go build -buildmode=shared -linkshared
2) Install this package
3) Try to compile another package, which imports worker:
go build -linkshared
go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
gcc: error: missing argument to ‘-l’
The same command with '-x --compiler=gccgo' option produces the following:
/usr/bin/gccgo -o $WORK/godev/testgo/_obj/exe/a.out $WORK/godev/testgo/_obj/_go_.o -Wl,-( -m64 -Wl,-) -L/home/user/dev/godev/pkg/gccgo_linux_amd64_fPIC/shlibs -Wl,-rpath=/home/user/dev/godev/pkg/gccgo_linux_amd64_fPIC/shlibs -l -Wl,-E -fPIC
Here is a bug on github. It turned out that go tool generated wrong names.
Related
I built my .so file:
go build -buildmode=plugin -o test.so
and run debug with Goland, then I get the error:
Error running agent: could not initialize input inputs.plugin_input: plugin.Open("./plugins_lib/test1"): plugin was built with a different version of package runtime/internal/sys
But I can build my main program in my terminal and it will work well.
The plugin should be compiled with the same flags as the main application.
If the application is compiled using the IDE already, then add the -gcflags="all=-N -l" to the above go build ... command.
go build -buildmode=plugin -gcflags="all=-N -l" -o test.so
In addition,if the IDE is Goland, the build command of the main application can be found at the debug console of Goland.
Is it possible?
I have the following Go function I'd like to call from a C program:
// package name: test
package main
import "C"
//export Start
func Start() {
println("Hello world")
}
func main() {
}
I'm using the following command to build the archive
go build -buildmode=c-archive -o test.a main.go
Using gcc I can get this C program working:
#include <stdio.h>
#include "test.h"
int main() {
Start();
return 0;
}
I'm using the following command to build the executable:
gcc main.c sdlgotest.a -o main -lpthread
This all works fine for amd64, but I'd like to use this archive in a aarch64 targed development environment (using libtransistor)
libtransistor build system uses LLVM but it has it's own set of standard includes (libc, etc.) and it doesn't use glibc.
So when I'm trying to get libtransistor to link my archive I get the following errors:
/usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
/usr/lib/llvm-5.0/bin/ld.lld: error: undefined symbol: stderr
>>> referenced by gcc_libinit.c:29
>>> 000006.o:(x_cgo_sys_thread_create) in archive ./sdlgotest.a
libtransistor by the way is compiling the code with flags like this:
-nostdlib -nostdlibinc -isystem /opt/libtransistor/include/
So I guess the problem is that the linker can't resolve those glibc symbols.
Is there a way to compile the Go runtime without those glibc symbols so I can use the archive like I intend to? (without relying on GCC toolchain or glibc)
From the docs:
The default C and C++ compilers may be changed by the CC and CXX environment variables, respectively; those environment variables may include command line options.
So I'd try something like
$ CC=clang go build -buildmode=c-archive -o test.a main.go
and see what happens.
Run go env and see the list of env parameters set to go. You can change certain parameters based on what you need.
Following are some of the samples that I have used to build a shared dll from go program.
GOARCH=386 GOOS=windows CGO_ENABLED=1 GOPATH=`pwd` CC=i686-w64-mingw32-gcc go build -o go-shared-lib.dll -buildmode=c-shared go-shared-libs
GOARCH=amd64 GOOS=windows CGO_ENABLED=1 GOPATH=`pwd` CC=x86_64-w64-mingw32-gcc go build -o go-shared-lib.dll -buildmode=c-shared go-shared-libs
Is there a way to build hyperledger fabric using gccgo? I want to do this in order to use -finstrument-functions option of gcc to trace function calls. But I encountered two problems. My steps are as follows.
find the build command
make -n release
echo "Building release/linux-amd64/bin/configtxgen for
linux-amd64"
mkdir -p release/linux-amd64/bin CGO_CFLAGS=" "
GOOS=linux GOARCH=amd64 go build -o
/home/yiifburj/go/src/github.com/hyperledger/fabric/release/linux-amd64/bin/configtxgen
-tags "nopkcs11" -ldflags "-X github.com/hyperledger/fabric/common/tools/configtxgen/metadata.Version=1.1.0"
github.com/hyperledger/fabric/common/tools/configtxgen
modify the build command to use gccgo
CGO_CFLAGS=" " GOOS=linux GOARCH=amd64 go build -compiler gccgo -o /home/yiifburj/go/src/github.com/hyperledger/fabric/release/linux-amd64/bin/configtxgen
-tags "nopkcs11" -gccgoflags "-X github.com/hyperledger/fabric/common/tools/configtxgen/metadata.Version=1.1.0"
github.com/hyperledger/fabric/common/tools/configtxgen
#github.com/hyperledger/fabric/bccsp/factory
bccsp/factory/pluginfactory.go:12:8: error: import file ‘plugin’ not
found
"plugin"
bccsp/factory/pluginfactory.go:56:15: error: reference to undefined name
‘plugin’ plug, err :=
plugin.Open(config.PluginOpts.Library)
First, as above, "plugin" couldn't be found when gccgo is invoked by go build.
Another one is how to pass ldflags -X when use gccgo? It seems that -X is a parameter only invalid in gc tools not gccgo.
Anyone can help me? Thanks.
You'll need to use the correct version of GCC in order to get support for the versions of Go supported by the various Fabric releases.
Fabric 1.1 requires Go 1.9.x
Fabric 1.2.x requires Go 1.10.x
The upcoming Fabric 1.3 also requires Go 1.10
gccgo did not support Go 1.9 (GCC 7 had support for Go 1.8). GCC 8 adds support for Go 1.10.
So you should use Fabric v1.2 and GCC 8.
I'm trying to do a go install and rename the output with the -o flag.
go install -o bar.exe src/foo.go
But this fails with the error:
flag provided but not defined: -o
usage: install [build flags] [packages]
go help build shows -o as the correct build flag to rename the output binary. There is no mention that this flag is not defined for go install.
go run -o bar.exe src/foo.go fails with the same error.
go build -o bar.exe src/foo.go works. I get bar.exe.
So is this just an error of documentation, or have I missed something?
My version: go1.5 windows/386.
Thanks.
go build accepts the -o flag but go install does not.
go install will always output to $GOPATH/bin
If you want to install a custom binary name to your gopath you can do go build -o $GOPATH/bin/whatever and that will be roughly equivalent to go install
You can fake the -o flag, if all you care about is the location, and not the name of the binary. Define GOBIN for the install command:
GOBIN=`readlink -f my/location` go install some/persons/go-package
Caveat: This doesn't work for cross-compiled binaries.
I want to compile example (make_torrent) from libtorrent official website:
g++ create_torrent_file.cpp -o run -lboost_filesystem-mt
But I get this error:
create_torrent_file.cpp:(.text+0x158): undefined reference to
`libtorrent::file_storage::file_storage()'
I have libtorrent-rasterbar installed
ldconfig -v | grep libtorrent:
libtorrent-rasterbar.so.6 -> libtorrent-rasterbar.so.6.0.0
So how should I compile this source code?
You need to add libtorrent-rasterbar to the linker. Try the following command:
g++ create_torrent_file.cpp -o run -ltorrent-rasterbar -lboost_filesystem-mt