栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

详解Golang编译成DLL文件

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解Golang编译成DLL文件

下面由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 mainimport "C"import "fmt"//export PrintByefunc PrintBye() {    fmt.Println("From DLL: Bye!")}//export Sumfunc 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.exeHello 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.gopackage mainimport "C"import "fmt"//export Addfunc Add(a C.int, b C.int) C.int {return a + b}//export Printfunc Print(s *C.char) {  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 mainimport ("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:760The operation completed successfully.Hello Andy

Python 调用

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

C++调用

#include #include 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文件的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/793897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号