Use the Swig tool to call C, C + + code examples for Go language generation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Swig Open Source project (http://www.swig.org) for multiple programming languagesprovides amutually accessible interface generation tool, which also contains the build for the Go languagethe ability to invoke C, C + + code. The CGO tool that comes with the go language already supports calls to C code, but support for C + + code does not work, so Go1 has added the Swig C + + functionality as a companion feature (see Go source code \ Go\misc\swig). In this way, the resources of C + + can also be used by the go language.


1, for Go language C language interface:

according to the relevant documentation guidance of Swig ( Http://www.swig.org/Doc2.0/Go.html#Go ), test it with an example from swig. The following examples are all tested under ubuntu12.04. Example source \swigwin-2.0.7\examples\go\simple

Swig-go 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 8g Example.go

Go Tool 8c EXAMPLE_GC.C
There will be an error.

example_gc.c:14 8c:no such file or directory:runtime.h

After reading Golang-nuts thread:
The original is not set 3 environment variables: goroot, goarch and GOOS. Here temporarily in console settings:

Export Goroot=/usr/lib/go
Export goarch=386

Export Goos=linux

Go tool 8c-i ${goroot}/pkg/${goos}_${goarch} EXAMPLE_GC.C

Go Tool Pack GRC Example.a example.8 example_gc.8

Go Tool 8g Runme.go

Go tool 8l-o Runme runme.8
In order to run Runme, export Ld_library_path is also required

Export Ld_library_path=${pwd}:${ld_library_path}

./runme

This should see the results of the operation.

2. for the Go language generation, call the C + + Language interface:

Example Sources \swigwin-2.0.7\examples\go\class

Swig-c++-go example.i

g++-g-c-fpic example.cxx example_wrap.cxx

g++-shared EXAMPLE.O Example_wrap.o-o example.so

Go tool 8g example.go

Export Goroot=/usr/lib/go
Export goarch=386
Export Goos=linux

Go tool 8c-i ${goroot}/pkg/${goos}_${goarch} EXAMPLE_GC.C

Go Tool Pack GRC example.a example.8 example_gc.8

Go Tool 8g Runme.go

Go tool 8l-o Runme runme.8

//In order to run Runme, export is also required Ld_library_path

Export Ld_library_path=${pwd}:${ld_library_path}

./runme


Finally, pour some cold water. Calling C or C + + native code does meet the demand for higher performance requirements. But remember, it's a double-edged sword. It makes your solution more complex and takes more time and effort to develop and maintain. If you want to use it, you can only measure it on your own.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.