如何让CMake与Go编程语言配合工作?

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

How do I get CMake to work with the go programming language?

问题

我一直在使用CMake和C++来构建库和可执行文件,并希望能够在Go编程语言中使用相同的方法。

我需要采取哪些步骤来配置CMake,以便它可以与Go编程语言一起使用?

基本上,我的编译器是6g,它会生成一个编译好的foo.6文件 - 我通过6l foo.6将其发送给链接器,然后就完成了。我已经构建和安装了编译器和链接器。

显然,我可以为此编写一个简单的Makefile,但是在整个项目中始终使用CMake会更好。

非常感谢任何能帮助我入门的建议。

英文:

I have been using CMake with C++ to build libraries and executables and would like to use the same for the go programming language.

What steps do I need to take to configure CMake so it would work with the go programming language?

Essentially, my compiler is 6g which produces a compiled foo.6 - I send it to a linker via 6l foo.6 and I am done. I have the compiler and linker already built and installed.

Obviously, I can just write a simple Makefile for this, but it would be nice to use CMake consistently throughout my project.

TIA for any advice that would help me get started.

答案1

得分: 24

你可能希望为Go实现CMake的支持。大致来说,它涉及以下步骤:

  1. 创建CMakeDetermineGoCompiler.cmake模块,用于找到当前系统的Go编译器。

  2. 创建CMakeGoCompiler.cmake.in - 一个模板,将由CMakeDetermineGoCompiler进行配置。

  3. 创建CMakeTestGoCompiler.cmake模块,用于编译一个简单的Go源代码以检查编译器是否正常工作。

  4. 创建CMakeGoInformation.cmake,设置一些与语言相关的变量(如CMAKE_GO_LINK_EXECUTABLE等)。

这些内容应该放在CMAKE_MODULES_DIR中。您可以参考Java/CXX支持的实现方式。

或者,如果您不想干涉这些内部事务,您可以通过创建一个宏(macro()),来创建一系列自定义的目标/命令(请参阅add_custom_{target,command}()文档)来解决您的任务。

英文:

You might wish to implement CMake' support for Go. Roughly speaking it involves following steps:

  1. Create CMakeDetermineGoCompiler.cmake module, which would find Go
    compiler for the current system.

  2. Create CMakeGoCompiler.cmake.in - a
    template, which would be configured by CMakeDetermineGoCompiler.

  3. Create CMakeTestGoCompiler.cmake, module which would compile a
    simple go source to check if the compiler works.

  4. Create CMakeGoInformation.cmake, which would set some language-related
    variables (CMAKE_GO_LINK_EXECUTABLE and so on)

These things should be placed in CMAKE_MODULES_DIR. For reference you can take a look at how Java/CXX support is implemented.

Alternatively, if don't want to mess with such internal stuff, you can solve your task by creating a macro(), which would create a bunch of custom targets/commands (see add_custom_{target,command}() documentation).

答案2

得分: 2

最好不要使用CMake(或任何其他构建系统)。

Go语言有一种简单的内置方式来构建包:go build(它还使go getgo install工作)。go build设计上不需要像make或cmake这样的额外工具。

如果你使用CMake(或任何其他构建系统),你只会给自己增加麻烦(如果你计划使用其他人开发的库),或者给其他人增加麻烦(如果你计划开发供他人使用的库)。

英文:

It's in your best interest to not use CMake (or any other build system).

Go has a simple, built-in way to build packages: go build (which also makes go get and go install work). go build, by design, doesn't require additional tools like make or cmake.

If you use CMake (or any other build system) you'll just make life harder for yourself (if you plan to use libraries developed by others) or for other people (if you plan to develop libraries that are meant to be used by others).

答案3

得分: 1

我已经为使用CMake组合C/C++和Go创建了一个简单的go.cmake模块。

https://github.com/krumberg/cmake_go

英文:

I have created a simple go.cmake module for combining C/C++ and Go using CMake.

https://github.com/krumberg/cmake_go

huangapple
  • 本文由 发表于 2011年11月2日 18:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/7978517.html
匿名

发表评论

匿名网友

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

确定