-
Notifications
You must be signed in to change notification settings - Fork 17
/
import-c.slide
126 lines (71 loc) · 2.23 KB
/
import-c.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import "C"
Calling C From Go
27 Mar 2013
Tags: go c
Miki Tebeka
Technical Lead, Adconion
http://mikitebeka.com
@tebeka
* Why C?
After all we have Go, which is much better :)
However, there is a *huge* volume of software written in C.
We'd like to re-use it and not re-write it.
* import "C" (A.K.A cgo)
Provides an easy bridge between Go and C
The Downside
- Hard to find docs (see reference slide)
- Pay attention to memory leaks
- Compilation time grows
- For C++ you need `extern`"C"` and C like wrapper
- Users need C compilers and libraries installed
- Cross compilation [[https://code.google.com/p/go/issues/detail?id=4714][does not work]]
Note that are other options (such as [[http://www.swig.org/][swig]])
* Minimal Example
.play import-c/sqrt.go
* Strings
.play import-c/str.go
* Ahhhhhh!
.image import-c/picard1.jpg 400 600
* Strings
.play import-c/str2.go
* But ...
.image import-c/picard2.jpg 400 600
* Strings
.play import-c/str3.go
* Structs
.play import-c/struct.go
* Calling Go from C
`callback.c`
.code import-c/callback/callback.c
`callback.h`
.code import-c/callback/callback.h
* Calling Go from C
`callback.go`
.code import-c/callback/callback.go
* Go Style Errors
.play import-c/err.go
* Case Study - snowball
- [[https://bitbucket.org/tebeka/snowball]]
- Port of [[http://snowball.tartarus.org/][snowball]] stemmer to Go
- Had to flatten directories and rewrite #include
- Wasn't that painful
.play import-c/stem.go
* snowball - imports and definition
.code import-c/snowball.go /START_IMPORT/,/END_IMPORT/
* snowball - New and free
.code import-c/snowball.go /START_NEW/,/END_NEW/
* snowball - Stem
snowball uses `sb_symbol*` for string
.code import-c/snowball.go /START_STEM/,/END_STEM/
* snowball - List
The list of languages is a `static`char*` array with a `NULL` sentinal
.code import-c/snowball.go /START_LIST/,/END_LIST/
* References
- [[http://golang.org/cmd/cgo/][cgo docs]]
- [[http://golang.org/doc/articles/c_go_cgo.html][C? Go? Cgo!]]
- [[http://golang.org/misc/cgo/][cgo examples]]
- [[https://code.google.com/p/go-wiki/wiki/cgo][cgo wiki]]
- [[http://stackoverflow.com/search?q=%5Bgo%5D+cgo][StackOverflow]]
* Lets Hack!
.image import-c/real-programmers.jpg 400 600