Go and C + + link examples

Source: Internet
Author: User

Example of a link to go lang and C + +:

Foo.hpp

Foo.hpp#ifndef _foo_hpp_#define _foo_hpp_template<typename t>t Add (const t& lhs,const T& RHS) {return LHS+RHS;} void display (); #endif//_foo_hpp_

Foo.cpp

Foo.cpp#include "FOO.HPP" #include <iostream>void display () {std::cout<< "This message was from Foo.cpp- Display "<<STD::ENDL;}

Foo_wrap.h

Foo_wrap.h#ifndef _foo_wrap_h_#define _foo_wrap_h_#ifdef __cplusplusextern "C" {#endif//add function Familyint add_ Int_wrap (int lhs,int rhs); float add_float_wrap (float lhs,float rhs);//display some messagevoid display_wrap (); #ifdef __ Cplusplus} #endif #endif//_foo_wrap_h_

Foo_wrap.cpp

Foo_wrap.cpp#include "Foo_wrap.h" #include "foo.hpp"//add function familyint add_int_wrap (int lhs,int rhs) {return add <int> (LHS,RHS);} Float Add_float_wrap (float lhs,float rhs) {return add<float> (LHS,RHS);} Displayvoid Display_wrap () {return display ();}

Foo.go

Foo.gopackage main//#cgo CFLAGS:-i.///#cgo ldflags:-l./libfoo.a-lstdc++//#include "foo_wrap.h" import "C" import " Fmt "Func main () {//test add Family//intvar A, b int32 = 1,2rsi: = C.add_int_wrap (C.int (a), C.int (b)) fmt. Printf ("C.add_int_wrap (%d,%d) =%d\n", A,b,rsi)//floatvar c,d float32 = 1.3,2.5rsf: = C.add_float_wrap (C.float (C), C.float (d)) fmt. Printf ("C.add_int_wrap (%f,%f) =%f\n", C,D,RSF)//displayfmt. PRINTLN ("Message from C:") C.display_wrap ()}

Compile script build.sh:

#!/bin/sh
#build C + + source codeg++-C foo.cppg++-C foo_wrap.cppar RCS libfoo.a foo.o foo_wrap.o#build go source codego build foo . Go./foo

Go and C + + link examples

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.