Sharing Golang packages to C and Go
Sharing Golang packages to C and GoSun,Aug 23,2015 programming languagestutorial The latestGo 1.5version is out. As part of thenew features, It accepts
By default,listed main packages are built into executables and listed non-main packages are built into .a files. In this article we will explore two major ways to share libraries between Go and C: Using shared library in GoAssume that . ├── calc │ └── calc.go └── cashier └── main.go The // filename: calc.go package calc func Sum(x,y int) int { return x + y } Before compile any shared library,the standard builtin packages should be installed as shared library. This will allow any other shared library to link with them. $ go install -buildmode=shared -linkshared std Then the $ go install -buildmode=shared -linkshared calc Due to aissue,building and installing shared library should be from Lets use the shared library // package: cashier // filename: main.go package main import "calc" import "fmt" func main() { fmt.Println("Cashier Application") fmt.Printf("Result: %dn",calc.Sum(5,10)) } The application should be compiled and linked with $ go build -linkshared -o app cashier The output of executing the application is: $ ./app Cashier Application Result: 15 Note that this feature is available on Using shared Go library in CGo functions can be executed from C applications. They should be exported by using the following comment line: //export <your_function_name> In the code snippet below,the function // package name: nautilus package main import "C" import "fmt" //export SayHello func SayHello(name string) { fmt.Printf("Nautilus says: Hello,%s!n",name) } //export SayBye func SayBye() { fmt.Println("Nautilus says: Bye!") } func main() { // We need the main function to make possible // CGO compiler to compile the package as C shared library } The packaged should be compiled with // as c-shared library $ go build -buildmode=c-shared -o nautilus.a nautilus.go // as c-archive $ go build -buildmode=c-archive -o nautilus.a nautilus.go As result the typedef signed char GoInt8; typedef unsigned char GoUint8; typedef short GoInt16; typedef unsigned short GoUint16; typedef int GoInt32; typedef unsigned int GoUint32; typedef long long GoInt64; typedef unsigned long long GoUint64; typedef GoInt64 GoInt; typedef GoUint64 GoUint; typedef __SIZE_TYPE__ GoUintptr; typedef float GoFloat32; typedef double GoFloat64; typedef __complex float GoComplex64; typedef __complex double GoComplex128; typedef struct { char *p; GoInt n; } GoString; typedef void *GoMap; typedef void *GoChan; typedef struct { void *t; void *v; } GoInterface; typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; #endif /* End of boilerplate cgo prologue. */ #ifdef __cplusplus extern "C" { #endif extern void SayHello(GoString p0); extern void SayBye(); #ifdef __cplusplus } #endif The header file In the example below,the // filename: _wale.c #include "nautilus.h" #include <stdio.h> int main() { printf("This is a C Application.n"); GoString name = {"Jack",4}; SayHello(name); SayBye(); return 0; } The $ gcc -o _wale _wale.c nautilus.a Execution produce the following output: $ ./wale This is a C Application. Nautilus says: Hello,Jack! Nautilus says: Bye! ConclusionSharing libraries between (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |