Go language-use Swig to convert C + + code to go

Source: Internet
Author: User

Server-side project, to take into account performance issues, before using the company-based framework of PHP, performance bottlenecks, after investigation, decided to use go. Because a key module was already implemented using C + +, you plan to use go as an HTTP container to combine go and C + + modules.
In the online research and integration program, go comes with CGO only support C language, swig tools can support multiple languages combined with C + +, so decided to use Swig, here briefly describe the use of swig and some of the points of discovery.
1. After extracting the Swig, we first run configure and make on the root directory, which generates makefile for each subdirectory and generates preinst-swig.in and swig executables in the current directory, which are later owned.

2. We enter example's go inside, here are some examples worth reference

3. To study the class example, there are several documents:

Class.cxx and Example.h are the C + + code we want to convert.
EXAMPLE.I is a bridge interface file for C + + and go, mainly configuration.
Makefile will do build work, the core is actually called the example root directory under that makefile.
Runme.go is the go code, which calls the C + + code.
4. Through reading makefile and analysis, we understand the basic process of swig:
Compile the C + + code to generate. O;
The C + + code is converted according to the interface configuration file, generating. Go and go packaging. a files;
Combine runme.go and the generated C + +. Go file to generate an executable file.
5. If you want to predefine some map and vector, you can configure it in the configuration file, these types and C + + structure, classes, etc. are converted to the type in go and stored in C + +. Go and. A. An example of an interface configuration file:

/ * FILE:EXAMPLE.I * /%module tipcpp%{#include "example.h"%}%include"STD_STRING.I"%include"Std_vector.i"%include"STD_MAP.I"namespace STD{   %Template(Appinfovector) vector<sAppInfo>; %Template(jobdict) Map<string, string>; %Template(Retmap) Map<string, bool>;}/* Let's just grab the original header file here * /%include"Example.h"

6. We usually want to have C + + code presented in Go pkg, so it's good to put the generated. Go and. A in the corresponding place in the Gopath. However, if you want to invoke this library to generate the executable file, you cannot simply use go build, but instead use the following compilation method, which is extracted from makefile:

#! /bin/bash6${GOPATH}${1}6${GOPATH}"g++""-I/usr/include  "$1${1}.6

Above if not set the path of Gopath, will go to the installation directory of Go to find the installation package lib/pkg, this is not what we want, we want all the package control in their own gopath inside.

Go language-use Swig to convert C + + code to go

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.