dongyisa6254
2013-09-02 11:02
浏览 36
已采纳

编译时按体系结构排除go源文件

I am writing a Go program for Windows which contains several packages. One of these packages is using CGo to call a few functions defined in some .h and .c files. These .c files are dependent on windows.h .

Since developing on the Windows platform is incredibly tedious I would like to make a mockup of the functions in this file and develop on Linux instead. But when I try to compile I get:

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

Since the go tool tries to compile my Windows dependent files. Is there some way around this? I know that putting something like

#ifdef ..
import x
#endif

is not best practice but in this case I need something to allow compiling only the "Linux" files.

图片转代码服务由CSDN问答提供 功能建议

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

由于在Windows平台上进行开发非常繁琐,因此我想对该文件中的功能进行建模,然后在Linux上进行开发。 但是当我尝试编译时,我得到:

 致命错误:windows.h:没有这样的文件或目录
   
 
 <  p>因为go工具会尝试编译Windows依赖文件。 有办法解决吗? 我知道放置 
 
 
  #ifdef .. 
import x 
#endif 
   
 
 

之类的东西 最佳实践,但在这种情况下,我需要允许仅编译“ Linux”文件的内容。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dounieqi6959 2013-09-02 11:09
    已采纳

    Quoting from the build constraints documentation:

    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:

    • the target operating system, as spelled by runtime.GOOS
    • the target architecture, as spelled by runtime.GOARCH
    • the compiler being used, either "gc" or "gccgo"
    • "cgo", if ctxt.CgoEnabled is true
    • "go1.1", from Go version 1.1 onward
    • any additional words listed in 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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题