首先撰写golang程序exportgo.go:

    1. package main
    2. import "C"
    3. import "fmt"
    4. //export PrintBye
    5. func PrintBye() {
    6. fmt.Println("From DLL: Bye!")
    7. }
    8. //export Sum
    9. func Sum(a int, b int) int {
    10. return a + b;
    11. }
    12. func main() {
    13. // Need a main function to make CGO compile package as C shared library
    14. }

    编译成 DLL 文件:

    1. go build -buildmode=c-shared -o exportgo.dll exportgo.go

    编译后得到 exportgo.dll 和 exportgo.h两个文件。


    Golang 编译成DLL文件 - 图1