英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论