-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# include "hello.h" | ||
|
||
void hello(char *s){ | ||
printf("hello cgo! \n %s",s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# include <stdio.h> | ||
|
||
void hello( char *s); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# cgo | ||
|
||
- 编译c函数为动态链接库 | ||
|
||
``` | ||
# include <stdio.h> | ||
void hello( char *s); | ||
``` | ||
|
||
```c | ||
# include "hello.h" | ||
void hello(char *s){ | ||
printf("hello cgo! \n %s",s); | ||
} | ||
``` | ||
- 编译成 动态链接库 | ||
``` | ||
gcc -shared -fPIC -o libhello.dll hello.c | ||
``` | ||
- 编写 go main函数 | ||
```go | ||
// test_hello.go | ||
package main | ||
/* | ||
#cgo LDFLAGS: -L. -lhello | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include"hello.h" | ||
*/ | ||
import "C" | ||
import ( | ||
"unsafe" | ||
) | ||
func main(){ | ||
s:=" hello\n"; | ||
cs:=C.CString(s); | ||
C.hello(cs); | ||
defer C.free(unsafe.Pointer(cs)); | ||
} | ||
``` | ||
|
||
- 运行 | ||
|
||
``` | ||
go run test_hello.go | ||
``` | ||
|
||
- https://golang.org/cmd/cgo/#hdr-C_references_to_Go | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
/* | ||
#cgo LDFLAGS: -L. -lhello | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include"hello.h" | ||
*/ | ||
import "C" | ||
import ( | ||
"unsafe" | ||
) | ||
|
||
func main(){ | ||
s:=" hello\n"; | ||
cs:=C.CString(s); | ||
C.hello(cs); | ||
defer C.free(unsafe.Pointer(cs)); | ||
|
||
|
||
} |