如何使用伪CGO指令将子目录中的C文件作为go build的一部分添加进去?

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

How to add C files in a subdirectory as part of go build by using pseudo CGO directives?

问题

根据文档,使用cgo的go build命令会将包根目录下的所有C/C++文件包含在编译过程中。是否有一种方法可以通过使用CGO指令,使给定子目录中的C/C++文件也参与编译,而不仅限于根目录下的文件呢?

英文:

Per docs, go build with cgo will add any C/C++ files in the root of the package as part of the compilation. Is there a way to make C/C++ files in a given subdirectory to also be part of the compilation as well as the ones in the root by using CGO directives?

答案1

得分: 7

并不完全是。你唯一的选择是将子目录作为另一个Go包,但这样你就需要将所需的任何功能封装为导出的Go函数,并将其导入到你的项目中。

编译C/C++文件只是为了满足基本需求的便利,但如果有更复杂的需求,你需要单独构建源代码,并提供适当的CGO指令进行链接。Go工具链并不意味着是一个完整的构建工具。

英文:

Not really. The only option you have is to make the subdirectory another Go package, but then you'd have to wrap any functionality you need in exported Go functions, and import it into your project.

The compiling of C/C++ files is a convenience for basic requirements, but anything more complicated will require building your source separately, and providing the proper CGO directives for linking. The Go toolchain isn't meant to be a complete build tool.

huangapple
  • 本文由 发表于 2015年3月5日 23:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/28881072.html
匿名

发表评论

匿名网友

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

确定