How can I build a dll with cgo and .def file

huangapple go评论83阅读模式
英文:

How can I build a dll with cgo and .def file

问题

我想实现DLL转发(或DLL代理)。

在MinGW中,我可以使用带有.def文件的cpp源码,例如gcc -shared -o test.dll functions.def test.cpp

但是我不知道如何在Golang中实现它。

英文:

I want to implement dll forwarding(or dll proxy)

In mingw, I can use cpp source with .def file, like gcc -shared -o test.dll functions.def test.cpp

But I don't know how to implement it in golang

答案1

得分: 1

我知道如何解决这个问题。

使用extldflags。

第一种方法:

  1. 将.def文件编译为.exp文件
dlltool --def functions.def --output-exp evildll.exp
  1. 编译main.go

main.go

package main

import "C"

func main() {
	// 需要一个main函数以将CGO编译包作为C共享库
}

使用命令go build -buildmode=c-shared -o exportgo.dll -ldflags="-extldflags=-Wl,C:/Users/Akkuman/Desktop/go-dll-proxy/evildll.exp"

然后你就可以得到dll文件。

How can I build a dll with cgo and .def file

第二种方法

使用命令go build -buildmode=c-shared -o exportgo.dll -ldflags="-extldflags=-Wl,C:/Users/Akkuman/Desktop/go-dll-proxy/functions.def"

然后你就可以得到dll文件,但是会有一个额外的导出函数。

How can I build a dll with cgo and .def file

英文:

I know what to do to solve this problem.

use the extldflags

First Way:

  1. compile the .def file to .exp file
dlltool --def functions.def --output-exp evildll.exp
  1. compile main.go

main.go

package main

import "C"

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

use command go build -buildmode=c-shared -o exportgo.dll -ldflags="-extldflags=-Wl,C:/Users/Akkuman/Desktop/go-dll-proxy/evildll.exp"

then you can get the dll

How can I build a dll with cgo and .def file

Second Way

use command go build -buildmode=c-shared -o exportgo.dll -ldflags="-extldflags=-Wl,C:/Users/Akkuman/Desktop/go-dll-proxy/functions.def"

then you can get the dll, but with a additional export function

How can I build a dll with cgo and .def file

huangapple
  • 本文由 发表于 2021年8月13日 10:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/68766060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定