mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
ch2-{06|08}: 例子代码
This commit is contained in:
parent
08d8c702d7
commit
2f3da52336
2
.gitignore
vendored
2
.gitignore
vendored
@ -24,6 +24,8 @@ _obj
|
|||||||
# macOS
|
# macOS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
*.so
|
*.so
|
||||||
*.dll
|
*.dll
|
||||||
*.obj
|
*.obj
|
||||||
|
20
examples/ch2-06/incorrect-dll-api/Makefile
Normal file
20
examples/ch2-06/incorrect-dll-api/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
22
examples/ch2-06/incorrect-dll-api/main.go
Normal file
22
examples/ch2-06/incorrect-dll-api/main.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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 <stdlib.h>
|
||||||
|
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))
|
||||||
|
}
|
8
examples/ch2-06/incorrect-dll-api/mystring/Makefile
Normal file
8
examples/ch2-06/incorrect-dll-api/mystring/Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
default:
|
||||||
|
gcc -shared -o libmystring.so mystring.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm *.so
|
26
examples/ch2-06/incorrect-dll-api/mystring/mystring.c
Normal file
26
examples/ch2-06/incorrect-dll-api/mystring/mystring.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "mystring.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
6
examples/ch2-06/incorrect-dll-api/mystring/mystring.h
Normal file
6
examples/ch2-06/incorrect-dll-api/mystring/mystring.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
char* make_string(const char* s);
|
||||||
|
|
||||||
|
void free_string(char* s);
|
20
examples/ch2-06/make-clib-dll/Makefile
Normal file
20
examples/ch2-06/make-clib-dll/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
17
examples/ch2-06/make-clib-dll/_test_main.c
Normal file
17
examples/ch2-06/make-clib-dll/_test_main.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
13
examples/ch2-06/make-clib-dll/main.go
Normal file
13
examples/ch2-06/make-clib-dll/main.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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
|
||||||
|
}
|
4
examples/ch2-06/make-clib-dll/number-win64.def
Normal file
4
examples/ch2-06/make-clib-dll/number-win64.def
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
LIBRARY number-win64.dll
|
||||||
|
|
||||||
|
EXPORTS
|
||||||
|
number_add_mod
|
60
examples/ch2-06/make-clib-dll/number.h
Normal file
60
examples/ch2-06/make-clib-dll/number.h
Normal file
@ -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
|
12
examples/ch2-06/make-clib-from-multi-pkg/Makefile
Normal file
12
examples/ch2-06/make-clib-from-multi-pkg/Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
19
examples/ch2-06/make-clib-from-multi-pkg/_test_main.c
Normal file
19
examples/ch2-06/make-clib-from-multi-pkg/_test_main.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "./number/number.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
21
examples/ch2-06/make-clib-from-multi-pkg/main.go
Normal file
21
examples/ch2-06/make-clib-from-multi-pkg/main.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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))
|
||||||
|
}
|
60
examples/ch2-06/make-clib-from-multi-pkg/main.h
Normal file
60
examples/ch2-06/make-clib-from-multi-pkg/main.h
Normal file
@ -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
|
11
examples/ch2-06/make-clib-from-multi-pkg/number/number.go
Normal file
11
examples/ch2-06/make-clib-from-multi-pkg/number/number.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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
|
||||||
|
}
|
1
examples/ch2-06/make-clib-from-multi-pkg/number/number.h
Normal file
1
examples/ch2-06/make-clib-from-multi-pkg/number/number.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
int number_add_mod(int a, int b, int mod);
|
11
examples/ch2-06/make-clib-shared/Makefile
Normal file
11
examples/ch2-06/make-clib-shared/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
17
examples/ch2-06/make-clib-shared/_test_main.c
Normal file
17
examples/ch2-06/make-clib-shared/_test_main.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
13
examples/ch2-06/make-clib-shared/main.go
Normal file
13
examples/ch2-06/make-clib-shared/main.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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
|
||||||
|
}
|
60
examples/ch2-06/make-clib-shared/number.h
Normal file
60
examples/ch2-06/make-clib-shared/number.h
Normal file
@ -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
|
11
examples/ch2-06/make-clib-static/Makefile
Normal file
11
examples/ch2-06/make-clib-static/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
17
examples/ch2-06/make-clib-static/_test_main.c
Normal file
17
examples/ch2-06/make-clib-static/_test_main.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
13
examples/ch2-06/make-clib-static/main.go
Normal file
13
examples/ch2-06/make-clib-static/main.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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
|
||||||
|
}
|
60
examples/ch2-06/make-clib-static/number.h
Normal file
60
examples/ch2-06/make-clib-static/number.h
Normal file
@ -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
|
8
examples/ch2-06/plugin/Makefile
Normal file
8
examples/ch2-06/plugin/Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
default:
|
||||||
|
go build -buildmode=plugin plugin.go -o plugin.so
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm *.so
|
23
examples/ch2-06/plugin/main.go
Normal file
23
examples/ch2-06/plugin/main.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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"
|
||||||
|
}
|
15
examples/ch2-06/plugin/plugin.go
Normal file
15
examples/ch2-06/plugin/plugin.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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) }
|
20
examples/ch2-06/use-clib-shared/Makefile
Normal file
20
examples/ch2-06/use-clib-shared/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
15
examples/ch2-06/use-clib-shared/main.go
Normal file
15
examples/ch2-06/use-clib-shared/main.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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))
|
||||||
|
}
|
8
examples/ch2-06/use-clib-shared/number/Makefile
Normal file
8
examples/ch2-06/use-clib-shared/number/Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
default:
|
||||||
|
gcc -shared -o libnumber.so number.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm *.so
|
5
examples/ch2-06/use-clib-shared/number/number.c
Normal file
5
examples/ch2-06/use-clib-shared/number/number.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
int number_add_mod(int a, int b, int mod) {
|
||||||
|
return (a+b)%mod;
|
||||||
|
}
|
1
examples/ch2-06/use-clib-shared/number/number.h
Normal file
1
examples/ch2-06/use-clib-shared/number/number.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
int number_add_mod(int a, int b, int mod);
|
11
examples/ch2-06/use-clib-static-v1/Makefile
Normal file
11
examples/ch2-06/use-clib-static-v1/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
15
examples/ch2-06/use-clib-static-v1/main.go
Normal file
15
examples/ch2-06/use-clib-static-v1/main.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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))
|
||||||
|
}
|
10
examples/ch2-06/use-clib-static-v1/number/Makefile
Normal file
10
examples/ch2-06/use-clib-static-v1/number/Makefile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
5
examples/ch2-06/use-clib-static-v1/number/number.c
Normal file
5
examples/ch2-06/use-clib-static-v1/number/number.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
int number_add_mod(int a, int b, int mod) {
|
||||||
|
return (a+b)%mod;
|
||||||
|
}
|
1
examples/ch2-06/use-clib-static-v1/number/number.h
Normal file
1
examples/ch2-06/use-clib-static-v1/number/number.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
int number_add_mod(int a, int b, int mod);
|
9
examples/ch2-06/use-clib-static-v2/Makefile
Normal file
9
examples/ch2-06/use-clib-static-v2/Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
default:
|
||||||
|
go build -o a.out
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm a.out
|
13
examples/ch2-06/use-clib-static-v2/main.go
Normal file
13
examples/ch2-06/use-clib-static-v2/main.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// 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))
|
||||||
|
}
|
10
examples/ch2-06/use-clib-static-v2/number/Makefile
Normal file
10
examples/ch2-06/use-clib-static-v2/number/Makefile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
5
examples/ch2-06/use-clib-static-v2/number/number.c
Normal file
5
examples/ch2-06/use-clib-static-v2/number/number.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "number.h"
|
||||||
|
|
||||||
|
int number_add_mod(int a, int b, int mod) {
|
||||||
|
return (a+b)%mod;
|
||||||
|
}
|
1
examples/ch2-06/use-clib-static-v2/number/number.h
Normal file
1
examples/ch2-06/use-clib-static-v2/number/number.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
int number_add_mod(int a, int b, int mod);
|
4
examples/ch2-06/use-clib-static-v2/z_link_number_c.c
Normal file
4
examples/ch2-06/use-clib-static-v2/z_link_number_c.c
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include "./number/number.c"
|
8
examples/ch2-08/hello-swig-v1/Makefile
Normal file
8
examples/ch2-08/hello-swig-v1/Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
default:
|
||||||
|
go run runme.go
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm a.out
|
8
examples/ch2-08/hello-swig-v1/hello.cc
Normal file
8
examples/ch2-08/hello-swig-v1/hello.cc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com).
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void SayHello() {
|
||||||
|
std::cout << "Hello, World!" << std::endl;
|
||||||
|
}
|
8
examples/ch2-08/hello-swig-v1/hello.swigcxx
Normal file
8
examples/ch2-08/hello-swig-v1/hello.swigcxx
Normal file
@ -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();
|
||||||
|
%}
|
12
examples/ch2-08/hello-swig-v1/hello_test.go
Normal file
12
examples/ch2-08/hello-swig-v1/hello_test.go
Normal file
@ -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()
|
||||||
|
}
|
14
examples/ch2-08/hello-swig-v1/runme.go
Normal file
14
examples/ch2-08/hello-swig-v1/runme.go
Normal file
@ -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()
|
||||||
|
}
|
11
examples/ch2-08/hello-swig-v2/Makefile
Normal file
11
examples/ch2-08/hello-swig-v2/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||||
|
# 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
|
8
examples/ch2-08/hello-swig-v2/hello.cc
Normal file
8
examples/ch2-08/hello-swig-v2/hello.cc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// Copyright © 2016 ChaiShushan (chaishushan{AT}gmail.com).
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void SayHello() {
|
||||||
|
std::cout << "Hello, World!" << std::endl;
|
||||||
|
}
|
73
examples/ch2-08/hello-swig-v2/hello.go
Normal file
73
examples/ch2-08/hello-swig-v2/hello.go
Normal file
@ -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 <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
9
examples/ch2-08/hello-swig-v2/hello.i
Normal file
9
examples/ch2-08/hello-swig-v2/hello.i
Normal file
@ -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();
|
||||||
|
%}
|
||||||
|
|
14
examples/ch2-08/hello-swig-v2/runme.go
Normal file
14
examples/ch2-08/hello-swig-v2/runme.go
Normal file
@ -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()
|
||||||
|
}
|
257
examples/ch2-08/hello-swig-v2/swig_wrap.cc
Normal file
257
examples/ch2-08/hello-swig-v2/swig_wrap.cc
Normal file
@ -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 <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user