You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
//#include <stdio.h>//void callC() {// printf("Calling C code!\n");//}import"C"import"fmt"funcmain() {
fmt.Println("A Go statement!")
C.callC()
fmt.Println("Another Go statement!")
}
#include<stdio.h>#include"callC.h"voidcHello() {
printf("Hello from C!\n");
}
voidprintMessage(char*message) {
printf("Go send me %s\n", message);
}
callC.go
package main
// #cgo CFLAGS: -I${SRCDIR}/callClib// #cgo LDFLAGS: ${SRCDIR}/callC.a// #include <stdlib.h>// #include <callC.h>import"C"import (
"fmt""unsafe"
)
funcmain() {
fmt.Println("Going to call a C function!")
C.cHello()
fmt.Println("Going to call another C function!")
myMessage:=C.CString("This is Mihalis!")
deferC.free(unsafe.Pointer(myMessage))
C.printMessage(myMessage)
fmt.Println("All perfectly done!")
}
$ gcc -c callClib/*.c
# 当前目录会产生 callC.o
$ ar rs callC.a *.o
# 产生 callC.a
$ file callC.a
callC.a: current ar archive random library
$ rm callC.o
$ go build callC.go
# 产生 可执行 的 callC.exe
$ .\callC.exe
书籍链接: https://www.amazon.com/Mastering-production-applications-concurrency-structures-ebook/dp/B07WC24RTQ
Code: https://github.com/PacktPublishing/Mastering-Go
The text was updated successfully, but these errors were encountered: