diff --git a/Doc/Manual/Go.html b/Doc/Manual/Go.html index ca12410ad..7531a218c 100644 --- a/Doc/Manual/Go.html +++ b/Doc/Manual/Go.html @@ -13,8 +13,8 @@
-Go is a compiled language, not a scripting language. However, it does -not support direct calling of functions written in C/C++. The cgo -program may be used to generate wrappers to call C code from Go, but -there is no convenient way to call C++ code. SWIG fills this gap. +Go does not support direct calling of functions written in C/C++. The +cgo program may be used to generate +wrappers to call C code from Go, but there is no convenient way to call C++ +code. SWIG fills this gap.
-There are (at least) two different Go compilers. One is the gc -compiler, normally invoked via the go tool. The other -is the gccgo compiler, which is a frontend to the gcc compiler suite. -The interface to C/C++ code is completely different for the two Go -compilers. SWIG supports both, selected by a command line option. +There are (at least) two different Go compilers. The first is the Go compiler +of the Go distribution. +Since Go 1.5 the Go compiler is part of the +go tool. Go 1.4 and earlier use the gc tool which is called by the go tool. +The second Go compiler is the +gccgo compiler, which is a frontend to the GCC compiler suite. +The interface to C/C++ code is completely different for the two Go compilers. +SWIG supports both Go compilers, selected by the -gccgo command line +option.
-Because Go is a type-safe compiled language, SWIG's runtime type -checking and runtime library are not used with Go. This should be -borne in mind when reading the rest of the SWIG documentation. +Go is a type-safe compiled language and the wrapper code generated by SWIG is +type-safe as well. In case of type issues the build will fail and hence SWIG's +runtime library and +runtime type checking +are not used.
-Working examples can be found here: +Working examples can be found in the +SWIG source tree +.
- +-The examples in the 2nd link are shipped with the SWIG distribution under the Examples/go directory. +Please note that the examples in the SWIG source tree use makefiles with the .i +SWIG interface file extension for backwards compatibility with Go 1.
@@ -99,12 +105,83 @@ The examples in the 2nd link are shipped with the SWIG distribution under the Ex-To generate Go code, use the -go option with SWIG. By -default SWIG will generate code for the gc compilers. To generate -code for gccgo, you should also use the -gccgo option. +Most Go programs are built using the go +tool. Since Go 1.1 the go tool has support for SWIG. To use it, give your +SWIG interface file the extension .swig (for C code) or .swigcxx (for C++ code). +Put that file in a GOPATH/src directory as usual for Go sources. Put other +C/C++ code in the same directory with extensions of .c and .cxx. The +go build and go install commands will automatically run SWIG +for you and compile the generated wrapper code. To check the SWIG command line +options the go tool uses run go build -x. To access the automatically +generated files run go build -work. You'll find the files under the +temporary WORK directory.
-+To manually generate and compile C/C++ wrapper code for Go, use the -go +option with SWIG. By default SWIG will generate code for the Go compiler of the +Go distribution. To generate code for gccgo, you should also use the -gccgo + option. +
+ ++When using the -cgo option, SWIG will generate files that can be used +directly by go build. Starting with the Go 1.5 distribution the +-cgo option has to be given. Put your SWIG interface file in a +directory under GOPATH/src, and give it a name that does not end in the +.swig or .swigcxx extension. Typically the SWIG interface file extension is .i +in this case. +
+ ++% swig -go -cgo example.i +% go install +
+You will now have a Go package that you can import from other Go packages as +usual. +
+ ++To use SWIG without the -cgo option, more steps are required. Recall +that this only works with Go versions before 1.5. When using Go version 1.2 or +later, or when using gccgo, the code generated by SWIG can be linked directly +into the Go program. A typical command sequence when using the Go compiler of +the Go distribution would look like this: +
+ ++% swig -go example.i +% gcc -c code.c # The C library being wrapped. +% gcc -c example_wrap.c +% go tool 6g example.go +% go tool 6c example_gc.c +% go tool pack grc example.a example.6 example_gc.6 code.o example_wrap.o +% go tool 6g main.go +% go tool 6l main.6 +
+You can also put the wrapped code into a shared library, and when using the Go +versions before 1.2 this is the only supported option. A typical command +sequence for this approach would look like this: +
+ ++% swig -go -use-shlib example.i +% gcc -c -fpic example.c +% gcc -c -fpic example_wrap.c +% gcc -shared example.o example_wrap.o -o example.so +% go tool 6g example.go +% go tool 6c example_gc.c +% go tool pack grc example.a example.6 example_gc.6 +% go tool 6g main.go # your code, not generated by SWIG +% go tool 6l main.6 +
@@ -116,9 +193,9 @@ also be seen by using: swig -go -help -