compilation - 编译时按架构排除 go 源文件

标签 compilation go

我正在为 Windows 编写一个包含多个包的 Go 程序。其中一个包是使用 CGo 调用在一些 .h 和 .c 文件中定义的一些函数。这些 .c 文件依赖于 windows.h 。

由于在 Windows 平台上进行开发非常乏味,我想制作一个模型,然后在 Linux 上开发此文件中的功能。但是当我尝试编译时,我得到:

fatal error: windows.h: No such file or directory

由于 go 工具试图编译我的 Windows 相关文件。有没有办法解决这个问题?我知道把像

#ifdef ..
import x
#endif

不是最佳实践,但在这种情况下,我需要一些东西来允许只编译“Linux”文件。

最佳答案

引自 build constraints文档:

A build constraint is a line comment beginning with the directive +build that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments.

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:

// +build linux,386 darwin,!cgo

corresponds to the boolean formula:

(linux AND 386) OR (darwin AND (NOT cgo))

A file may have multiple build constraints. The overall constraint is the AND of the individual constraints. That is, the build constraints:

// +build linux darwin
// +build 386

corresponds to the boolean formula:

(linux OR darwin) AND 386

During a particular build, the following words are satisfied:

  • 目标操作系统,由 runtime.GOOS 拼写
  • 目标架构,由 runtime.GOARCH 拼写
  • 正在使用的编译器,“gc”或“gccgo”
  • “cgo”,如果 ctxt.CgoEnabled 为真
  • “go1.1”,从 Go 版本 1.1 开始
  • ctxt.BuildTags 中列出的任何其他字词

If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

(example: source_windows_amd64.go) or the literals:

GOOS
GOARCH

(example: windows.go) where GOOS and GOARCH represent any known operating system and architecture values respectively, then the file is considered to have an implicit build constraint requiring those terms.

To keep a file from being considered for the build:

// +build ignore

(any other unsatisfied word will work as well, but “ignore” is conventional.)

To build a file only when using cgo, and only on Linux and OS X:

// +build linux,cgo darwin,cgo

Such a file is usually paired with another file implementing the default functionality for other systems, which in this case would carry the constraint:

// +build !linux,!darwin !cgo

Naming a file dns_windows.go will cause it to be included only when building the package for Windows; similarly, math_386.s will be included only when building the package for 32-bit x86.

关于compilation - 编译时按架构排除 go 源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18571661/

相关文章:

c - 如何在 Mac 上编译?

pointers - 通过引用设置结构中的字段

go - 一段时间后停止协程

go - 如何在Golang中的struct中访问嵌套字段

http - Go:如何组合两个(或更多)http.ServeMux?

go - Windows中的Golang cmd命令

java - Windows 中的 Tensorflow Java API

Java:消除死代码

c++ - 如何通过拷贝构造函数区分一个类中的每个变量

java - Java 中的静态导入