ホームページ  >  記事  >  バックエンド開発  >  Golang を DLL にパッケージ化できますか?

Golang を DLL にパッケージ化できますか?

angryTom
angryTomオリジナル
2020-03-17 16:09:184279ブラウズ

Golang を DLL にパッケージ化できますか?

Golang は dll にパッケージ化できますか?

Golang はプログラムを DLL ファイルにコンパイルできます。具体的な方法は次のとおりです。以下:

1. Golang は DLL をコンパイルするときに gcc を使用する必要があるため、最初に MinGW をインストールします。

Windows 64 ビット システムでは、64 ビット バージョンの MinGW をダウンロードする必要があります: https://sourceforge.net/projects/mingw-w64/

2. ダウンロード後、mingw-w64 を実行します。 -install.exe 、MingGWのインストールを完了します。

(推奨学習: Web サイト構築チュートリアル )

3. まず、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
}

4. それをコンパイルして、 DLL ファイル:

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

コンパイル後、2 つのファイル exportgo.dll exportgo.h が得られます。

5.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 の詳細については、PHP 中国語 Web サイトの golang チュートリアル 列に注目してください。

以上がGolang を DLL にパッケージ化できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。