下面由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中文網其他相關文章!

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golang適合快速開發和並發編程,而C 更適合需要極致性能和底層控制的項目。 1)Golang的並發模型通過goroutine和channel簡化並發編程。 2)C 的模板編程提供泛型代碼和性能優化。 3)Golang的垃圾回收方便但可能影響性能,C 的內存管理複雜但控制精細。

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang在实际应用中表现出色,以简洁、高效和并发性著称。1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3漢化版
中文版,非常好用

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具