21

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.

kfmfe04
  • 14,936
  • 14
  • 74
  • 140

3 Answers3

24

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).

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • 1
    Thank you so much! I've been looking for this simple info for two weeks and it was frustrating because in Modules folder of cmake there are so many files and it gets confusing if you just want to start small. Anyway thanks! Awesome answer! – Cristian Bidea Nov 30 '13 at 21:39
  • Wow! I wish I had found this answer a year ago when I first started trying to do this. Well written answer :) – DavidZemon Feb 09 '16 at 23:44
  • 1
    Any hope to get an example of the files you mentioned? – MajesticRa May 03 '19 at 19:19
  • by `CMAKE_MODULES_DIR` you mean `CMAKE_MODULE_PATH`. – linuxUser123 Jul 19 '19 at 20:00
2

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).

Krzysztof Kowalczyk
  • 3,513
  • 28
  • 28
  • 9
    I think the exception to this is when using `gccgo` and combining object files with existing C, Fortran, Ada or C++ projects, that already uses CMake. – Alexander Jun 02 '16 at 07:45
  • 21
    Or building intermediate files like protobuffers, or building distributable tarballs for release, or basically a bajillion other tasks that people seem to forget exist for people actually writing go projects that are more complicated than `go get` can handle. – Stephen Touset Jul 28 '16 at 23:54
  • how can I use `go build` to produce `.dll` lib for windows? – Hasan A Yousef Jun 06 '20 at 10:13
1

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

https://github.com/krumberg/cmake_go

KristianR
  • 420
  • 2
  • 6