diff --git a/.gitignore b/.gitignore index 6e5cd1c..7c5fb4d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,8 @@ _obj # macOS .DS_Store +*.a +*.lib *.so *.dll *.obj diff --git a/examples/ch2-06/incorrect-dll-api/Makefile b/examples/ch2-06/incorrect-dll-api/Makefile new file mode 100644 index 0000000..4315468 --- /dev/null +++ b/examples/ch2-06/incorrect-dll-api/Makefile @@ -0,0 +1,20 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + cd mystring && make + go build -o a.out + LD_LIBRARY_PATH=$(shell pwd)/mystring ./a.out + +macos: + cd mystring && make + go build -o a.out + DYLD_LIBRARY_PATH=$(shell pwd)/mystring ./a.out + +windows: + # set path + +clean: + -cd mystring && make clean + -rm *.a + -rm a.out diff --git a/examples/ch2-06/incorrect-dll-api/main.go b/examples/ch2-06/incorrect-dll-api/main.go new file mode 100644 index 0000000..bd9176a --- /dev/null +++ b/examples/ch2-06/incorrect-dll-api/main.go @@ -0,0 +1,22 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//#cgo CFLAGS: -I./mystring +//#cgo LDFLAGS: -L${SRCDIR}/mystring -lmystring +// +//#include "mystring.h" +//#include +import "C" +import ( + "fmt" + "unsafe" +) + +func main() { + cs := C.make_string(C.CString("hello")) + defer C.free(unsafe.Pointer(cs)) + + fmt.Println(C.GoString(cs)) +} diff --git a/examples/ch2-06/incorrect-dll-api/mystring/Makefile b/examples/ch2-06/incorrect-dll-api/mystring/Makefile new file mode 100644 index 0000000..8b9d0fa --- /dev/null +++ b/examples/ch2-06/incorrect-dll-api/mystring/Makefile @@ -0,0 +1,8 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + gcc -shared -o libmystring.so mystring.c + +clean: + -rm *.so diff --git a/examples/ch2-06/incorrect-dll-api/mystring/mystring.c b/examples/ch2-06/incorrect-dll-api/mystring/mystring.c new file mode 100644 index 0000000..4599397 --- /dev/null +++ b/examples/ch2-06/incorrect-dll-api/mystring/mystring.c @@ -0,0 +1,26 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "mystring.h" + +#include + +static char buffer[1024]; + +static char* malloc(int size) { + return &buffer[0]; +} + +static void free(void* p) { + // +} + +char* make_string(const char* s) { + char* p = malloc(strlen(s)+1); + strcpy(p, s); + return p; +} + +void free_string(char* s) { + free(s); +} diff --git a/examples/ch2-06/incorrect-dll-api/mystring/mystring.h b/examples/ch2-06/incorrect-dll-api/mystring/mystring.h new file mode 100644 index 0000000..cb79617 --- /dev/null +++ b/examples/ch2-06/incorrect-dll-api/mystring/mystring.h @@ -0,0 +1,6 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +char* make_string(const char* s); + +void free_string(char* s); diff --git a/examples/ch2-06/make-clib-dll/Makefile b/examples/ch2-06/make-clib-dll/Makefile new file mode 100644 index 0000000..31cadf8 --- /dev/null +++ b/examples/ch2-06/make-clib-dll/Makefile @@ -0,0 +1,20 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -buildmode=c-archive -o number.a + gcc -o a.out _test_main.c number.a + ./a.out + +build_on_win64: + go build -buildmode=c-archive -o number.a + gcc -m64 -shared -o number-win64.dll number-win64.def number.a -Wl,--allow-multiple-definition -static -lstdc++ -lwinmm -lntdll -lWs2_32 + lib /def:number-win64.def /machine:x64 + +build_with_vc: + cl -o a.out.exe _test_main.c number-win64.lib + +clean: + -rm *.a *.lib + -rm a.out + -rm a.out.exe diff --git a/examples/ch2-06/make-clib-dll/_test_main.c b/examples/ch2-06/make-clib-dll/_test_main.c new file mode 100644 index 0000000..bcdc73c --- /dev/null +++ b/examples/ch2-06/make-clib-dll/_test_main.c @@ -0,0 +1,17 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "number.h" + +#include + +int main() { + int a = 10; + int b = 5; + int c = 12; + + int x = number_add_mod(a, b, c); + printf("(%d+%d)%%%d = %d\n", a, b, c, x); + + return 0; +} diff --git a/examples/ch2-06/make-clib-dll/main.go b/examples/ch2-06/make-clib-dll/main.go new file mode 100644 index 0000000..324d07b --- /dev/null +++ b/examples/ch2-06/make-clib-dll/main.go @@ -0,0 +1,13 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import "C" + +func main() {} + +//export number_add_mod +func number_add_mod(a, b, mod C.int) C.int { + return (a + b) % mod +} diff --git a/examples/ch2-06/make-clib-dll/number-win64.def b/examples/ch2-06/make-clib-dll/number-win64.def new file mode 100644 index 0000000..9077d3e --- /dev/null +++ b/examples/ch2-06/make-clib-dll/number-win64.def @@ -0,0 +1,4 @@ +LIBRARY number-win64.dll + +EXPORTS +number_add_mod diff --git a/examples/ch2-06/make-clib-dll/number.h b/examples/ch2-06/make-clib-dll/number.h new file mode 100644 index 0000000..1720059 --- /dev/null +++ b/examples/ch2-06/make-clib-dll/number.h @@ -0,0 +1,60 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/chai2010/advanced-go-programming-book/examples/ch2-06/make-clib-dll */ + +/* Start of preamble from import "C" comments. */ + + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const 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 int number_add_mod(int p0, int p1, int p2); + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-06/make-clib-from-multi-pkg/Makefile b/examples/ch2-06/make-clib-from-multi-pkg/Makefile new file mode 100644 index 0000000..bb25fb2 --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/Makefile @@ -0,0 +1,12 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -buildmode=c-archive -o main.a + gcc -o a.out _test_main.c main.a + ./a.out + +clean: + -rm *.a *.lib + -rm a.out + -rm a.out.exe diff --git a/examples/ch2-06/make-clib-from-multi-pkg/_test_main.c b/examples/ch2-06/make-clib-from-multi-pkg/_test_main.c new file mode 100644 index 0000000..fdf7d40 --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/_test_main.c @@ -0,0 +1,19 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "main.h" +#include "./number/number.h" + +#include + +int main() { + int a = 10; + int b = 5; + int c = 12; + + int x = number_add_mod(a, b, c); + printf("(%d+%d)%%%d = %d\n", a, b, c, x); + + goPrintln("done"); + return 0; +} diff --git a/examples/ch2-06/make-clib-from-multi-pkg/main.go b/examples/ch2-06/make-clib-from-multi-pkg/main.go new file mode 100644 index 0000000..a6e1de7 --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/main.go @@ -0,0 +1,21 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import "C" + +import ( + "fmt" + + _ "github.com/chai2010/advanced-go-programming-book/examples/ch2-06/make-clib-from-multi-pkg/number" +) + +func main() { + println("Done") +} + +//export goPrintln +func goPrintln(s *C.char) { + fmt.Println("goPrintln:", C.GoString(s)) +} diff --git a/examples/ch2-06/make-clib-from-multi-pkg/main.h b/examples/ch2-06/make-clib-from-multi-pkg/main.h new file mode 100644 index 0000000..d24573b --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/main.h @@ -0,0 +1,60 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/chai2010/advanced-go-programming-book/examples/ch2-06/make-clib-from-multi-pkg */ + +/* Start of preamble from import "C" comments. */ + + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const 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 goPrintln(char* p0); + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-06/make-clib-from-multi-pkg/number/number.go b/examples/ch2-06/make-clib-from-multi-pkg/number/number.go new file mode 100644 index 0000000..c9305cf --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/number/number.go @@ -0,0 +1,11 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package number + +import "C" + +//export number_add_mod +func number_add_mod(a, b, mod C.int) C.int { + return (a + b) % mod +} diff --git a/examples/ch2-06/make-clib-from-multi-pkg/number/number.h b/examples/ch2-06/make-clib-from-multi-pkg/number/number.h new file mode 100644 index 0000000..48cae10 --- /dev/null +++ b/examples/ch2-06/make-clib-from-multi-pkg/number/number.h @@ -0,0 +1 @@ +int number_add_mod(int a, int b, int mod); diff --git a/examples/ch2-06/make-clib-shared/Makefile b/examples/ch2-06/make-clib-shared/Makefile new file mode 100644 index 0000000..9125039 --- /dev/null +++ b/examples/ch2-06/make-clib-shared/Makefile @@ -0,0 +1,11 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -buildmode=c-shared -o number.so + gcc -o a.out _test_main.c number.so + ./a.out + +clean: + -rm *.a + -rm a.out diff --git a/examples/ch2-06/make-clib-shared/_test_main.c b/examples/ch2-06/make-clib-shared/_test_main.c new file mode 100644 index 0000000..bcdc73c --- /dev/null +++ b/examples/ch2-06/make-clib-shared/_test_main.c @@ -0,0 +1,17 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "number.h" + +#include + +int main() { + int a = 10; + int b = 5; + int c = 12; + + int x = number_add_mod(a, b, c); + printf("(%d+%d)%%%d = %d\n", a, b, c, x); + + return 0; +} diff --git a/examples/ch2-06/make-clib-shared/main.go b/examples/ch2-06/make-clib-shared/main.go new file mode 100644 index 0000000..324d07b --- /dev/null +++ b/examples/ch2-06/make-clib-shared/main.go @@ -0,0 +1,13 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import "C" + +func main() {} + +//export number_add_mod +func number_add_mod(a, b, mod C.int) C.int { + return (a + b) % mod +} diff --git a/examples/ch2-06/make-clib-shared/number.h b/examples/ch2-06/make-clib-shared/number.h new file mode 100644 index 0000000..ad81ae4 --- /dev/null +++ b/examples/ch2-06/make-clib-shared/number.h @@ -0,0 +1,60 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/chai2010/advanced-go-programming-book/examples/ch2-06/make-clib-shared */ + +/* Start of preamble from import "C" comments. */ + + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const 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 int number_add_mod(int p0, int p1, int p2); + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-06/make-clib-static/Makefile b/examples/ch2-06/make-clib-static/Makefile new file mode 100644 index 0000000..d8435eb --- /dev/null +++ b/examples/ch2-06/make-clib-static/Makefile @@ -0,0 +1,11 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -buildmode=c-archive -o number.a + gcc -o a.out _test_main.c number.a + ./a.out + +clean: + -rm *.a + -rm a.out diff --git a/examples/ch2-06/make-clib-static/_test_main.c b/examples/ch2-06/make-clib-static/_test_main.c new file mode 100644 index 0000000..bcdc73c --- /dev/null +++ b/examples/ch2-06/make-clib-static/_test_main.c @@ -0,0 +1,17 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "number.h" + +#include + +int main() { + int a = 10; + int b = 5; + int c = 12; + + int x = number_add_mod(a, b, c); + printf("(%d+%d)%%%d = %d\n", a, b, c, x); + + return 0; +} diff --git a/examples/ch2-06/make-clib-static/main.go b/examples/ch2-06/make-clib-static/main.go new file mode 100644 index 0000000..324d07b --- /dev/null +++ b/examples/ch2-06/make-clib-static/main.go @@ -0,0 +1,13 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import "C" + +func main() {} + +//export number_add_mod +func number_add_mod(a, b, mod C.int) C.int { + return (a + b) % mod +} diff --git a/examples/ch2-06/make-clib-static/number.h b/examples/ch2-06/make-clib-static/number.h new file mode 100644 index 0000000..d2df1a8 --- /dev/null +++ b/examples/ch2-06/make-clib-static/number.h @@ -0,0 +1,60 @@ +/* Created by "go tool cgo" - DO NOT EDIT. */ + +/* package github.com/chai2010/advanced-go-programming-book/examples/ch2-06/use-clib-static */ + +/* Start of preamble from import "C" comments. */ + + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +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 float _Complex GoComplex64; +typedef double _Complex GoComplex128; + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +typedef struct { const 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 int number_add_mod(int p0, int p1, int p2); + +#ifdef __cplusplus +} +#endif diff --git a/examples/ch2-06/plugin/Makefile b/examples/ch2-06/plugin/Makefile new file mode 100644 index 0000000..053a915 --- /dev/null +++ b/examples/ch2-06/plugin/Makefile @@ -0,0 +1,8 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -buildmode=plugin plugin.go -o plugin.so + +clean: + -rm *.so diff --git a/examples/ch2-06/plugin/main.go b/examples/ch2-06/plugin/main.go new file mode 100644 index 0000000..99bc935 --- /dev/null +++ b/examples/ch2-06/plugin/main.go @@ -0,0 +1,23 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +import "plugin" + +func main() { + p, err := plugin.Open("plugin.so") + if err != nil { + panic(err) + } + v, err := p.Lookup("V") + if err != nil { + panic(err) + } + f, err := p.Lookup("F") + if err != nil { + panic(err) + } + *v.(*int) = 7 + f.(func())() // prints "Hello, number 7" +} diff --git a/examples/ch2-06/plugin/plugin.go b/examples/ch2-06/plugin/plugin.go new file mode 100644 index 0000000..c296a4e --- /dev/null +++ b/examples/ch2-06/plugin/plugin.go @@ -0,0 +1,15 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// +build ignore +// +build go1.10 + +package main + +import "fmt" + +func main() + +var V int + +func F() { fmt.Printf("Hello, number %d\n", V) } diff --git a/examples/ch2-06/use-clib-shared/Makefile b/examples/ch2-06/use-clib-shared/Makefile new file mode 100644 index 0000000..d93871f --- /dev/null +++ b/examples/ch2-06/use-clib-shared/Makefile @@ -0,0 +1,20 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + cd number && make + go build -o a.out + LD_LIBRARY_PATH=$(shell pwd)/number ./a.out + +macos: + cd number && make + go build -o a.out + DYLD_LIBRARY_PATH=$(shell pwd)/number ./a.out + +windows: + # set path + +clean: + -cd number && make clean + -rm *.a + -rm a.out diff --git a/examples/ch2-06/use-clib-shared/main.go b/examples/ch2-06/use-clib-shared/main.go new file mode 100644 index 0000000..0967bf8 --- /dev/null +++ b/examples/ch2-06/use-clib-shared/main.go @@ -0,0 +1,15 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//#cgo CFLAGS: -I./number +//#cgo LDFLAGS: -L${SRCDIR}/number -lnumber +// +//#include "number.h" +import "C" +import "fmt" + +func main() { + fmt.Println(C.number_add_mod(10, 5, 12)) +} diff --git a/examples/ch2-06/use-clib-shared/number/Makefile b/examples/ch2-06/use-clib-shared/number/Makefile new file mode 100644 index 0000000..93d6b0b --- /dev/null +++ b/examples/ch2-06/use-clib-shared/number/Makefile @@ -0,0 +1,8 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + gcc -shared -o libnumber.so number.c + +clean: + -rm *.so diff --git a/examples/ch2-06/use-clib-shared/number/number.c b/examples/ch2-06/use-clib-shared/number/number.c new file mode 100644 index 0000000..42a8304 --- /dev/null +++ b/examples/ch2-06/use-clib-shared/number/number.c @@ -0,0 +1,5 @@ +#include "number.h" + +int number_add_mod(int a, int b, int mod) { + return (a+b)%mod; +} diff --git a/examples/ch2-06/use-clib-shared/number/number.h b/examples/ch2-06/use-clib-shared/number/number.h new file mode 100644 index 0000000..48cae10 --- /dev/null +++ b/examples/ch2-06/use-clib-shared/number/number.h @@ -0,0 +1 @@ +int number_add_mod(int a, int b, int mod); diff --git a/examples/ch2-06/use-clib-static-v1/Makefile b/examples/ch2-06/use-clib-static-v1/Makefile new file mode 100644 index 0000000..8fa7b73 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v1/Makefile @@ -0,0 +1,11 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + cd number && make + go run main.go + +clean: + -cd number && make clean + -rm *.a + -rm a.out diff --git a/examples/ch2-06/use-clib-static-v1/main.go b/examples/ch2-06/use-clib-static-v1/main.go new file mode 100644 index 0000000..0967bf8 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v1/main.go @@ -0,0 +1,15 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//#cgo CFLAGS: -I./number +//#cgo LDFLAGS: -L${SRCDIR}/number -lnumber +// +//#include "number.h" +import "C" +import "fmt" + +func main() { + fmt.Println(C.number_add_mod(10, 5, 12)) +} diff --git a/examples/ch2-06/use-clib-static-v1/number/Makefile b/examples/ch2-06/use-clib-static-v1/number/Makefile new file mode 100644 index 0000000..3dd283f --- /dev/null +++ b/examples/ch2-06/use-clib-static-v1/number/Makefile @@ -0,0 +1,10 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + gcc -c -o number.o number.c + ar rcs libnumber.a number.o + -rm *.o + +clean: + -rm *.a *.o diff --git a/examples/ch2-06/use-clib-static-v1/number/number.c b/examples/ch2-06/use-clib-static-v1/number/number.c new file mode 100644 index 0000000..42a8304 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v1/number/number.c @@ -0,0 +1,5 @@ +#include "number.h" + +int number_add_mod(int a, int b, int mod) { + return (a+b)%mod; +} diff --git a/examples/ch2-06/use-clib-static-v1/number/number.h b/examples/ch2-06/use-clib-static-v1/number/number.h new file mode 100644 index 0000000..48cae10 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v1/number/number.h @@ -0,0 +1 @@ +int number_add_mod(int a, int b, int mod); diff --git a/examples/ch2-06/use-clib-static-v2/Makefile b/examples/ch2-06/use-clib-static-v2/Makefile new file mode 100644 index 0000000..9e40cf0 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/Makefile @@ -0,0 +1,9 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go build -o a.out + ./a.out + +clean: + -rm a.out diff --git a/examples/ch2-06/use-clib-static-v2/main.go b/examples/ch2-06/use-clib-static-v2/main.go new file mode 100644 index 0000000..6aeb2e3 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/main.go @@ -0,0 +1,13 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package main + +//#cgo CFLAGS: -I./number +//#include "number.h" +import "C" +import "fmt" + +func main() { + fmt.Println(C.number_add_mod(10, 5, 12)) +} diff --git a/examples/ch2-06/use-clib-static-v2/number/Makefile b/examples/ch2-06/use-clib-static-v2/number/Makefile new file mode 100644 index 0000000..3dd283f --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/number/Makefile @@ -0,0 +1,10 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + gcc -c -o number.o number.c + ar rcs libnumber.a number.o + -rm *.o + +clean: + -rm *.a *.o diff --git a/examples/ch2-06/use-clib-static-v2/number/number.c b/examples/ch2-06/use-clib-static-v2/number/number.c new file mode 100644 index 0000000..42a8304 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/number/number.c @@ -0,0 +1,5 @@ +#include "number.h" + +int number_add_mod(int a, int b, int mod) { + return (a+b)%mod; +} diff --git a/examples/ch2-06/use-clib-static-v2/number/number.h b/examples/ch2-06/use-clib-static-v2/number/number.h new file mode 100644 index 0000000..48cae10 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/number/number.h @@ -0,0 +1 @@ +int number_add_mod(int a, int b, int mod); diff --git a/examples/ch2-06/use-clib-static-v2/z_link_number_c.c b/examples/ch2-06/use-clib-static-v2/z_link_number_c.c new file mode 100644 index 0000000..ce91a51 --- /dev/null +++ b/examples/ch2-06/use-clib-static-v2/z_link_number_c.c @@ -0,0 +1,4 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include "./number/number.c" diff --git a/examples/ch2-08/hello-swig-v1/Makefile b/examples/ch2-08/hello-swig-v1/Makefile new file mode 100644 index 0000000..20a2494 --- /dev/null +++ b/examples/ch2-08/hello-swig-v1/Makefile @@ -0,0 +1,8 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +default: + go run runme.go + +clean: + -rm a.out diff --git a/examples/ch2-08/hello-swig-v1/hello.cc b/examples/ch2-08/hello-swig-v1/hello.cc new file mode 100644 index 0000000..c1246bc --- /dev/null +++ b/examples/ch2-08/hello-swig-v1/hello.cc @@ -0,0 +1,8 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include + +void SayHello() { + std::cout << "Hello, World!" << std::endl; +} diff --git a/examples/ch2-08/hello-swig-v1/hello.swigcxx b/examples/ch2-08/hello-swig-v1/hello.swigcxx new file mode 100644 index 0000000..b532652 --- /dev/null +++ b/examples/ch2-08/hello-swig-v1/hello.swigcxx @@ -0,0 +1,8 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +%module hello + +%inline %{ +extern void SayHello(); +%} diff --git a/examples/ch2-08/hello-swig-v1/hello_test.go b/examples/ch2-08/hello-swig-v1/hello_test.go new file mode 100644 index 0000000..3e331dc --- /dev/null +++ b/examples/ch2-08/hello-swig-v1/hello_test.go @@ -0,0 +1,12 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +package hello + +import ( + "testing" +) + +func TestSayHello(t *testing.T) { + SayHello() +} diff --git a/examples/ch2-08/hello-swig-v1/runme.go b/examples/ch2-08/hello-swig-v1/runme.go new file mode 100644 index 0000000..dbcca03 --- /dev/null +++ b/examples/ch2-08/hello-swig-v1/runme.go @@ -0,0 +1,14 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// +build ignore + +package main + +import ( + hello "." +) + +func main() { + hello.SayHello() +} diff --git a/examples/ch2-08/hello-swig-v2/Makefile b/examples/ch2-08/hello-swig-v2/Makefile new file mode 100644 index 0000000..84726dc --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/Makefile @@ -0,0 +1,11 @@ +# Copyright © 2017 ChaiShushan . +# License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +# use go generate + +default: + swig -go -cgo -intgosize 64 -o swig_wrap.cc hello.i + go run runme.go + +clean: + -rm a.out diff --git a/examples/ch2-08/hello-swig-v2/hello.cc b/examples/ch2-08/hello-swig-v2/hello.cc new file mode 100644 index 0000000..c1246bc --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/hello.cc @@ -0,0 +1,8 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +#include + +void SayHello() { + std::cout << "Hello, World!" << std::endl; +} diff --git a/examples/ch2-08/hello-swig-v2/hello.go b/examples/ch2-08/hello-swig-v2/hello.go new file mode 100644 index 0000000..f49e4e6 --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/hello.go @@ -0,0 +1,73 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 3.0.12 + * + * This file is not intended to be easily readable and contains a number of + * coding conventions designed to improve portability and efficiency. Do not make + * changes to this file unless you know what you are doing--modify the SWIG + * interface file instead. + * ----------------------------------------------------------------------------- */ + +// source: hello.i + +package hello + +/* +#define intgo swig_intgo +typedef void *swig_voidp; + +#include + + +typedef long long intgo; +typedef unsigned long long uintgo; + + + +typedef struct { char *p; intgo n; } _gostring_; +typedef struct { void* array; intgo len; intgo cap; } _goslice_; + + +extern void _wrap_Swig_free_hello_679051ef4cde42b8(uintptr_t arg1); +extern uintptr_t _wrap_Swig_malloc_hello_679051ef4cde42b8(swig_intgo arg1); +extern void _wrap_SayHello_hello_679051ef4cde42b8(void); +#undef intgo +*/ +import "C" + +import "unsafe" +import _ "runtime/cgo" +import "sync" + + +type _ unsafe.Pointer + + + +var Swig_escape_always_false bool +var Swig_escape_val interface{} + + +type _swig_fnptr *byte +type _swig_memberptr *byte + + +type _ sync.Mutex + +func Swig_free(arg1 uintptr) { + _swig_i_0 := arg1 + C._wrap_Swig_free_hello_679051ef4cde42b8(C.uintptr_t(_swig_i_0)) +} + +func Swig_malloc(arg1 int) (_swig_ret uintptr) { + var swig_r uintptr + _swig_i_0 := arg1 + swig_r = (uintptr)(C._wrap_Swig_malloc_hello_679051ef4cde42b8(C.swig_intgo(_swig_i_0))) + return swig_r +} + +func SayHello() { + C._wrap_SayHello_hello_679051ef4cde42b8() +} + + diff --git a/examples/ch2-08/hello-swig-v2/hello.i b/examples/ch2-08/hello-swig-v2/hello.i new file mode 100644 index 0000000..5d5374c --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/hello.i @@ -0,0 +1,9 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +%module hello + +%inline %{ +extern void SayHello(); +%} + diff --git a/examples/ch2-08/hello-swig-v2/runme.go b/examples/ch2-08/hello-swig-v2/runme.go new file mode 100644 index 0000000..dbcca03 --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/runme.go @@ -0,0 +1,14 @@ +// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com). +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// +build ignore + +package main + +import ( + hello "." +) + +func main() { + hello.SayHello() +} diff --git a/examples/ch2-08/hello-swig-v2/swig_wrap.cc b/examples/ch2-08/hello-swig-v2/swig_wrap.cc new file mode 100644 index 0000000..c82f261 --- /dev/null +++ b/examples/ch2-08/hello-swig-v2/swig_wrap.cc @@ -0,0 +1,257 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 3.0.12 + * + * This file is not intended to be easily readable and contains a number of + * coding conventions designed to improve portability and efficiency. Do not make + * changes to this file unless you know what you are doing--modify the SWIG + * interface file instead. + * ----------------------------------------------------------------------------- */ + +/* source: hello.i */ + +#define SWIGMODULE hello +/* ----------------------------------------------------------------------------- + * This section contains generic SWIG labels for method/variable + * declarations/attributes, and other compiler dependent labels. + * ----------------------------------------------------------------------------- */ + +/* template workaround for compilers that cannot correctly implement the C++ standard */ +#ifndef SWIGTEMPLATEDISAMBIGUATOR +# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) +# define SWIGTEMPLATEDISAMBIGUATOR template +# elif defined(__HP_aCC) +/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ +/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ +# define SWIGTEMPLATEDISAMBIGUATOR template +# else +# define SWIGTEMPLATEDISAMBIGUATOR +# endif +#endif + +/* inline attribute */ +#ifndef SWIGINLINE +# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) +# define SWIGINLINE inline +# else +# define SWIGINLINE +# endif +#endif + +/* attribute recognised by some compilers to avoid 'unused' warnings */ +#ifndef SWIGUNUSED +# if defined(__GNUC__) +# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +# elif defined(__ICC) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +#endif + +#ifndef SWIG_MSC_UNSUPPRESS_4505 +# if defined(_MSC_VER) +# pragma warning(disable : 4505) /* unreferenced local function has been removed */ +# endif +#endif + +#ifndef SWIGUNUSEDPARM +# ifdef __cplusplus +# define SWIGUNUSEDPARM(p) +# else +# define SWIGUNUSEDPARM(p) p SWIGUNUSED +# endif +#endif + +/* internal SWIG method */ +#ifndef SWIGINTERN +# define SWIGINTERN static SWIGUNUSED +#endif + +/* internal inline SWIG method */ +#ifndef SWIGINTERNINLINE +# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE +#endif + +/* exporting methods */ +#if defined(__GNUC__) +# if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +# ifndef GCC_HASCLASSVISIBILITY +# define GCC_HASCLASSVISIBILITY +# endif +# endif +#endif + +#ifndef SWIGEXPORT +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# if defined(STATIC_LINKED) +# define SWIGEXPORT +# else +# define SWIGEXPORT __declspec(dllexport) +# endif +# else +# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) +# define SWIGEXPORT __attribute__ ((visibility("default"))) +# else +# define SWIGEXPORT +# endif +# endif +#endif + +/* calling conventions for Windows */ +#ifndef SWIGSTDCALL +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# define SWIGSTDCALL __stdcall +# else +# define SWIGSTDCALL +# endif +#endif + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ +#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE) +# define _SCL_SECURE_NO_DEPRECATE +#endif + +/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */ +#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES) +# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0 +#endif + +/* Intel's compiler complains if a variable which was never initialised is + * cast to void, which is a common idiom which we use to indicate that we + * are aware a variable isn't used. So we just silence that warning. + * See: https://github.com/swig/swig/issues/192 for more discussion. + */ +#ifdef __INTEL_COMPILER +# pragma warning disable 592 +#endif + + +#include +#include +#include +#include +#include + + + +typedef long long intgo; +typedef unsigned long long uintgo; + + +# if !defined(__clang__) && (defined(__i386__) || defined(__x86_64__)) +# define SWIGSTRUCTPACKED __attribute__((__packed__, __gcc_struct__)) +# else +# define SWIGSTRUCTPACKED __attribute__((__packed__)) +# endif + + + +typedef struct { char *p; intgo n; } _gostring_; +typedef struct { void* array; intgo len; intgo cap; } _goslice_; + + + + +#define swiggo_size_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1]; +#define swiggo_size_assert(t, n) swiggo_size_assert_eq(sizeof(t), n, swiggo_sizeof_##t##_is_not_##n) + +swiggo_size_assert(char, 1) +swiggo_size_assert(short, 2) +swiggo_size_assert(int, 4) +typedef long long swiggo_long_long; +swiggo_size_assert(swiggo_long_long, 8) +swiggo_size_assert(float, 4) +swiggo_size_assert(double, 8) + +#ifdef __cplusplus +extern "C" { +#endif +extern void crosscall2(void (*fn)(void *, int), void *, int); +extern char* _cgo_topofstack(void) __attribute__ ((weak)); +extern void _cgo_allocate(void *, int); +extern void _cgo_panic(void *, int); +#ifdef __cplusplus +} +#endif + +static char *_swig_topofstack() { + if (_cgo_topofstack) { + return _cgo_topofstack(); + } else { + return 0; + } +} + +static void _swig_gopanic(const char *p) { + struct { + const char *p; + } SWIGSTRUCTPACKED a; + a.p = p; + crosscall2(_cgo_panic, &a, (int) sizeof a); +} + + + + +#define SWIG_contract_assert(expr, msg) \ + if (!(expr)) { _swig_gopanic(msg); } else + + +static void Swig_free(void* p) { + free(p); +} + +static void* Swig_malloc(int c) { + return malloc(c); +} + + +extern void SayHello(); + +#ifdef __cplusplus +extern "C" { +#endif + +void _wrap_Swig_free_hello_679051ef4cde42b8(void *_swig_go_0) { + void *arg1 = (void *) 0 ; + + arg1 = *(void **)&_swig_go_0; + + Swig_free(arg1); + +} + + +void *_wrap_Swig_malloc_hello_679051ef4cde42b8(intgo _swig_go_0) { + int arg1 ; + void *result = 0 ; + void *_swig_go_result; + + arg1 = (int)_swig_go_0; + + result = (void *)Swig_malloc(arg1); + *(void **)&_swig_go_result = (void *)result; + return _swig_go_result; +} + + +void _wrap_SayHello_hello_679051ef4cde42b8() { + SayHello(); + +} + + +#ifdef __cplusplus +} +#endif +