2016-11-13 6 views
5

Windows에서 Go v1.7에 대해 dll을 빌드하는 방법이 있습니까? Go로 dll 빌드하기

나는

갱신 가 좋아, 내가있어 창/AMD64에서 지원되지 않습니다하는 공유 = 고전

go build -buildmode=shared main.go 

하지만, 얻을

-buildmode을 시도 내 대답. 관심이있는 사람들을 위해 : 는 https://groups.google.com/forum/#!topic/golang-dev/ckFZAZbnjzU

+0

'-buildmode = shared' 어쨌든 DLL을하지 것이라고 이동 공유 라이브러리입니다. 당신은 아마도'buildmode = c-shared'를 찾고 있었을 것입니다. 아직 그 창문은 아직 작동하지 않았고, [issue # 11058] (https://golang.org/issue/11058)을 따라갈 수 있습니다. – JimB

답변

13
go build -buildmode=c-archive github.com/user/ExportHello 

====>ExportHello.a, ExportHello.h

Hello2.c

gcc -shared -pthread -o Hello2.dll Hello2.c ExportHello.a -lWinMM -lntdll -lWS2_32 

ExportHello.a 및 재수출에 내장 된 기능을 가지고 구축 == 것 ==> 생성 됨 Hello2.dll

7

DLL을 만드는 법을 보여준 github 프로젝트, user7155193의 답변 덕택에.

기본적으로 GCC를 사용하여 생성 된 golang .a 및 .h 파일에서 DLL을 빌드합니다.

먼저 함수 (또는 그 이상)를 내보내는 간단한 Go 파일을 만듭니다.

package main 

import "C" 
import "fmt" 

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

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

로 컴파일 :

그런 다음
go build -buildmode=c-archive exportgo.go 

당신이 .H에 연결하는 C 프로그램 (goDLL.c을)하고 운영자와 파일

#include <stdio.h> 
#include "exportgo.h" 

// force gcc to link in go runtime (may be a better solution than this) 
void dummy() { 
    PrintBye(); 
} 

int main() { 

} 

컴파일 위에 생성/GCC와 DLL을 연결하십시오 :

그런 다음 goDLL.dll을 다른 C 프로그램, freepascal/lazarus 프로그램 또는 선택한 프로그램에로드 할 수 있습니다.

DLL을로드하는 나사로/FPC 프로젝트에 전체 코드는 여기에 있습니다 : https://github.com/z505/goDLL