試圖測驗cgo,所以我寫了以下內容:
//go:build lib
// build lib
package main
import "C"
import "fmt"
//export HelloWorld
func HelloWorld() {
fmt.Printf("hello world")
}
func main() {}
// go build -tags lib -buildmode=c-shared -o golib.a lib.go
并將其編譯為:
$ go build -tags lib -buildmode=c-shared -o golib.a lib.go
嘗試在另一個代碼中使用生成的共享庫:
//go:build app
// build app
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "golib.h"
import "C"
func main() {
C.HelloWorld()
}
// go run main.go
但我收到以下錯誤:
# command-line-arguments
Undefined symbols for architecture x86_64:
"_HelloWorld", referenced from:
__cgo_a844f0d618a1_Cfunc_HelloWorld in _x002.o
(maybe you meant: __cgo_a844f0d618a1_Cfunc_HelloWorld)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
# command-line-arguments
cgo-gcc-prolog:47:33: warning: unused variable '_cgo_a' [-Wunused-variable]
此外,我在 mac 上使用 VS 代碼收到以下錯誤:
go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990

uj5u.com熱心網友回復:
你不能在 Go 程式中使用 cgo 共享庫,因為你不能在同一個行程中有多個 Go 運行時。
嘗試這樣做會給出錯誤:
# command-line-arguments
cgo-gcc-prolog:67:33: warning: unused variable '_cgo_a' [-Wunused-variable]
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x43fd5e2]
goroutine 1 [running, locked to thread]:
runtime.throw({0x40a875b?, 0x1c00011b800?})
/usr/local/go/src/runtime/panic.go:992 0x71 fp=0x1c00004a960 sp=0x1c00004a930 pc=0x402f6d1
runtime: unexpected return pc for runtime.sigpanic called from 0x43fd5e2
stack: frame={sp:0x1c00004a960, fp:0x1c00004a9b0} stack=[0x1c00004a000,0x1c00004b000)
....
0x000001c00004aaa0: 0x0000000000000000 0x0000000000000000
runtime.sigpanic()
/usr/local/go/src/runtime/signal_unix.go:781 0x3a9 fp=0x1c00004a9b0 sp=0x1c00004a960 pc=0x4043449
exit status 2
要從 go 呼叫 c 代碼,有 3 種可能的方法:
- 行內編碼,如:
package main
//#include <stdio.h>
//int Add(int a, int b){
// printf("Welcome from external C function\n");
// return a b;
//}
import "C"
import "fmt"
func main() {
fmt.Println(C.Add(5, 2))
}
- 靜態鏈接,您同時擁有
.c和.h檔案,如:
// lib.c
#include <stdio.h>
int Add(int a, int b){
printf("Welcome from external C function\n");
return a b;
}
和
// libadd.h
int Add(int, int);
go 檔案是:
// main.go
package main
// #include "libadd.h"
import "C"
import "fmt"
func main() {
x := C.Add(1, 2)
fmt.Println(x)
}
我們必須將檔案運行為go run .或go run github.io/xxx
// go run main.go 將不起作用,因為它只會考慮 main.go,而不考慮 C 檔案
- 動態鏈接,您將上述
c檔案編譯為clang -shared -fpic -Wall -g lib.c -o libadd.so并將 go 檔案作為:
// main.go
package main
//#cgo CFLAGS: -g -Wall
//#cgo LDFLAGS: -L. -ladd
//#include "libadd.h"
import "C"
import "fmt"
func main() {
x := C.Add(1, 2)
fmt.Println(x)
}
這里你可以使用go run main.go通過硬編碼連接的庫,為了分發二進制檔案,共享庫loadadd.so需要以相同的二進制檔案分發并且存在于同一個檔案夾中。
我為每個案例上傳了單獨的檔案夾 [這里][1]
獎勵 要使用 ac 程式在 go 中呼叫生成的共享庫,我們可以使用以下命令:
// main.c
#include <stdio.h>
#include "libadd.h" // calling C file
#include "libgo.h" // calling shared library generated by GO
int main()
{
HelloWorld();
int x = Add(1, 2);
printf("%d",x);
return 0;
}
將檔案編譯為:``bash clang -o main -L. -ladd -lgo main.c // -ladd => -l(庫)是 libadd
[1]: https://github.com/hajsf/tutorial/tree/master/ffi
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422120.html
標籤:
