首頁  >  文章  >  後端開發  >  詳解Golang編譯成DLL文件

詳解Golang編譯成DLL文件

藏色散人
藏色散人轉載
2020-12-31 15:06:444315瀏覽

下面由golang教學欄位來介紹Golang 編譯成 DLL 檔案的方法,希望對需要的朋友有幫助!

golang 編譯 dll 過程中需要用到 gcc,所以先安裝 MinGW。

windows 64 位元系統應下載MinGW 的64 位元版本: https://sourceforge.net/projects/mingw-w64/

下載後執行 mingw-w64-install.exe,完成MingGW 的安裝。

首先寫 golang 程式 exportgo.go:

package main

import "C"
import "fmt"

//export PrintBye
func PrintBye() {
    fmt.Println("From DLL: Bye!")
}

//export Sum
func Sum(a int, b int) int {
    return a + b;
}

func main() {
    // Need a main function to make CGO compile package as C shared library
}

編譯成 DLL 檔案:

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

編譯後得到 exportgo.dll 和 exportgo.h 兩個檔案。

參考exportgo.h 檔案中的函數定義,撰寫C# 檔案 importgo.cs:

using System;
using System.Runtime.InteropServices;

namespace HelloWorld
{
    class Hello 
    {
        [DllImport("exportgo.dll", EntryPoint="PrintBye")]
        static extern void PrintBye();

        [DllImport("exportgo.dll", EntryPoint="Sum")]
        static extern int Sum(int a, int b);

        static void Main() 
        {
            Console.WriteLine("Hello World!");
            PrintBye();

            Console.WriteLine(Sum(33, 22));
        }

編譯CS 檔案得到exe

csc importgo.cs

將exe 和dll 放在同一目錄下,運行。

>importgo.exe

Hello World!
From DLL: Bye!
55

golang 中的string 參數在C# 中可以如下引用:

public struct GoString
    {
        public string Value { get; set; }
        public int Length { get; set; }

        public static implicit operator GoString(string s)
        {
            return new GoString() { Value = s, Length = s.Length };
        }

        public static implicit operator string(GoString s) => s.Value;
    }
// func.go
package main

import "C"
import "fmt"

//export Add
func Add(a C.int, b C.int) C.int {
	return a + b
}

//export Print
func Print(s *C.char) {  
/* 
函数参数可以用 string, 但是用*C.char更通用一些。
由于string的数据结构,是可以被其它go程序调用的,
但其它语言(如 python)就不行了
*/
	print("Hello ", C.GoString(s)) //这里不能用fmt包,会报错,调了很久...
}
func main() {	
}                                                                                                                                        

編譯

#go build -ldflags " -s -w" -buildmode=c-shared -o func.dll func.go
還是有點大的,880KB,純C 編譯的只有48KB,應該是沒有包含全部的依賴吧,go是全包進來了

Go 呼叫

package main
import (
	"fmt"
	"syscall"
)

func main() {
	dll := syscall.NewLazyDLL("func.dll")
	add := dll.NewProc("Add")
	prt := dll.NewProc("Print")
	r, err, msg := add.Call(32, 44)
	fmt.Println(r)
	fmt.Println(err)
	fmt.Println(msg)
	
	name := C.CString("Andy")
	prt.Call(uintptr(unsafe.Pointer(name)))
}
out:
76
0
The operation completed successfully.
Hello Andy

Python 呼叫

from ctypes import CDLL, c_char_p
dll = CDLL("func.dll")
dll.Add(32, 33)
dll.Print(c_char_p(bytes("Andy", "utf8")))

C 呼叫

#include <iostream>
#include <windows.h>

using namespace std;
typedef int(*pAdd)(int a, int b);
typedef void(*pPrt)(char* s);

int main(int argc, char *argv[])
{
    HMODULE dll= LoadLibraryA("func.dll");
    pAdd add = (pAdd)GetProcAddress(dll, "Add");
    pPrt prt = (pPrt)GetProcAddress(dll, "Print");
    cout << add(321, 33) << endl;
    prt("Andy");
    FreeLibrary(dll);
    return 0;
}

更多相關技術文章,請訪問go語言教學欄位!

以上是詳解Golang編譯成DLL文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除