All Projects → draffensperger → Go Interlang

draffensperger / Go Interlang

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

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects
cgo
20 projects

Labels

Projects that are alternatives of or similar to Go Interlang

cffi
Safe* C foreign function interface for Rust, using proc macros and marshaling types.
Stars: ✭ 15 (-95.66%)
Mutual labels:  ffi
swig-webpack-plugin
Render swig templates with webpack.
Stars: ✭ 12 (-96.53%)
Mutual labels:  swig
ext-psi
PHP System Interface (aka FFI)
Stars: ✭ 24 (-93.06%)
Mutual labels:  ffi
unixstring
An FFI-friendly null-terminated byte string
Stars: ✭ 19 (-94.51%)
Mutual labels:  ffi
FABGen
C++ binding generator for CPython 3.x (x>=2), Lua 5.3 and Go
Stars: ✭ 26 (-92.49%)
Mutual labels:  swig
uapi
Unix API
Stars: ✭ 18 (-94.8%)
Mutual labels:  ffi
swift-bridge
swift-bridge facilitates Rust and Swift interop.
Stars: ✭ 260 (-24.86%)
Mutual labels:  ffi
Cbmc
C Bounded Model Checker
Stars: ✭ 309 (-10.69%)
Mutual labels:  swig
douMiBlog-Sailsjs
豆米博客-Sailsjs版,个人博客系统,使用SailsJs,MongoDb,Bootstrap,marked,SASS,swig等
Stars: ✭ 22 (-93.64%)
Mutual labels:  swig
cld3-ruby
cld3-ruby is an interface of Compact Language Detector v3 (CLD3) for Ruby.
Stars: ✭ 59 (-82.95%)
Mutual labels:  ffi
mozilla-deepspeech-flutter
Mozilla DeepSpeech in flutter using Dart FFI
Stars: ✭ 23 (-93.35%)
Mutual labels:  ffi
rust-lang-interop
Rust language interoperability with other languages - C, C++ etc.
Stars: ✭ 53 (-84.68%)
Mutual labels:  ffi
renderdoc-rs
RenderDoc application bindings for Rust
Stars: ✭ 28 (-91.91%)
Mutual labels:  ffi
PhpWebcam
This is a PHP library to capture webcam frames
Stars: ✭ 33 (-90.46%)
Mutual labels:  ffi
Win32
Build Win32 apps with Dart!
Stars: ✭ 256 (-26.01%)
Mutual labels:  ffi
libserialport.dart
Serial Port for Dart
Stars: ✭ 51 (-85.26%)
Mutual labels:  ffi
ffi-libc
Useful Ruby FFI bindings for libc
Stars: ✭ 32 (-90.75%)
Mutual labels:  ffi
Simpleelastix
Multi-lingual medical image registration library
Stars: ✭ 332 (-4.05%)
Mutual labels:  swig
Shogun
Shōgun
Stars: ✭ 2,859 (+726.3%)
Mutual labels:  swig
gpgme
GPGme bindings for Rust
Stars: ✭ 55 (-84.1%)
Mutual labels:  ffi

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].