Golang & CGO – 链接到 DLL 动态库

问题描述 投票:0回答:1

不幸的是,Golang 的 GitHub 存储库不允许我在那里发布问题,他们将我重定向到 StackOverflow (https://github.com/golang/go/wiki/Questions),所以就这样吧。

Go 语言将自己定位为 跨平台 语言。对我来说,跨平台语言是那种可以在所有支持的平台上使用的语言。

我想使用它链接到 DLL 动态库的能力。 Windows 操作系统上使用 DLL 库,Golang 以支持 Windows 操作系统而著称

图书馆是用

C
语言写的。众所周知,Golang 使用所谓的“CGO”功能支持
C
语言。

我知道

syscall
包。我想特意使用“CGO”功能

我知道Golang提供了一种使用

C
变量为CGO指定
CC
编译器的方法。我想知道在 Go 的 CGO 中使用
CLang
编译器(https://en.wikipedia.org/wiki/Clang)的正确方法是什么。

我试着搜索。所有信息都被分解成小而过时的片段。 Golang 每个月都在变化,所以我不能依赖过去的信息。

所以,我对 Go 语言的创建者和维护者的问题如下。

  1. 如何在 Go 中链接到
    C
    语言 DLL?
  2. 如何使用
    CLang
    CGO 编译器?
  3. 如何在 CGO 中查看编译和链接过程的输出(
    stdout
    对于
    C
    人)?

谢谢。

更新#1

go env -w "CC=clang"

导致:

go: reading go env config: read J:\Go\env: Incorrect function.

CC="clang.exe" go build .

导致

CC=clang.exe : The term 'CC=clang.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:1
+ CC="clang.exe" go build .
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CC=clang.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

还有这个

CC="D:\Program Files\LLVM\bin\clang.exe" go build . 

也导致错误, 和这个一样

CC="D:\\Program Files\\LLVM\\bin\\clang.exe" go build .

路径正确:

"D:\Program Files\LLVM\bin\clang.exe" -v
clang version 16.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: D:\Program Files\LLVM\bin
go dll clang cgo cc
1个回答
1
投票
  1. 使用 cgo ldflags,您可以添加链接标志以链接到库,例如

    // #cgo LDFLAGS: -lpng
    import "C"
    
  2. 使用

    CC
    环境变量,参见这个答案

  3. 就在go build的标准输出中

© www.soinside.com 2019 - 2024. All rights reserved.