Giter Site home page Giter Site logo

draffensperger / go-interlang Goto Github PK

View Code? Open in Web Editor NEW
417.0 16.0 62.0 2.38 MB

Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)

C 24.53% Go 34.18% C++ 6.51% Java 2.92% JavaScript 0.80% Python 1.25% Ruby 6.02% Makefile 9.12% Shell 14.68%
golang cgo swig ffi

go-interlang's Introduction

Go interlanguage call examples

Examples of calls between Go and C/C++ and calling Go from dynamic languages.

The examples are designed to work on Mac and Linux. If you're using Windows, feel free to reach out to me to request updates for examples that don't work correctly.

Calls from Go to C (go_to_c folder)

These examples use cgo to enable calls to C.

  • Calling a C snippet in the cgo comment: c_in_comment
  • Calling a C statically-linked library (.a file): static_c_lib
  • Calling a C dynamically-linked library (.so or .dylib): dynamic_c_lib

Calls from Go to C++ (go_to_cxx folder)

C++ has more complex calling conventions (e.g. function overloading, inheritance, templates) and so it uses name mangling which adds a step when calling it from Go. Below are ways to do it.

  • Calling C++ via a C wrapper function: c_wrapper
  • Calling C++ via an auto-generated SWIG wrapper: swig

Calls from C/C++ to Go (c_to_go folder)

Calls from Python/Node.js/Ruby/Java to Go (dyn_langs_to_go folder)

Go now allows building a C-compatible dynamically-linked library with buildmode=c-shared. That allows any language that can call C dynamic libraries to call Go.

Cross-language call benchmarks

There is a cost to calling between languages a Go to C call is about 50x slower than a pure Go call and a Ruby FFI call is about 30x slower than a pure Ruby call. For more details, see the benchmarks section.

Helpful Links

Cgo documentation: golang.og/cmd/cgo/

Documentation for go command (see especially go build section and "Calling between Go and C"): golang.org/cmd/go/

SWIG Documentation: swig.org

SWIG Go examples: github.com/swig/swig/tree/master/Examples/go

Gccgo documentation: golang.org/doc/install/gccgo

License

The code in this repo is MIT Licensed.

go-interlang's People

Contributors

draffensperger avatar stroxler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-interlang's Issues

Compile error: I can't call exported go function in C++

I followed your example in cxx_to_go_dyn_lib. I want to export go functions in infiniband go module so that they are callable from C++. I manage to generate the .h header files but when I compile my C++ code it bumps out with the following error:

./src/github.com/jsgilmore/ib/libadder.so(_all.o): In function Initialize': /tmp/go-build/github.com/jsgilmore/ib/_obj/_cgo_export.c:33: undefined reference to _cgo_wait_runtime_init_done'
/tmp/go-build/github.com/jsgilmore/ib/_obj/_cgo_export.c:38: undefined reference to `_cgoexp_1f5f6ba4e969_Initialize'

Didn't get much help from the internet on this error. I will appreciate your input.
P.S. The file that contains the the exported go functions has an "import C" declaration with commented includes. Could this be an issue?

external c library

Hi,
do you know how I can call and link an external c/c++ library in callback or adder?
I can call from the main, but not in the .c/.h files of the callback project.
Thanks a lot
Angelo

GO binding struct for a self referencing struct

Hi,

Any ideas on how to write a golang equivalent struct for self referencing struct something like this -

typedef struct WRITER {
int version;
int (WriteBlock)(struct WRITER thisPtr,
const void* dataBufferPtr,
unsigned long bufferSize);
} WRITER;

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.