Build a go project using cmake
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Let's say you're a CMake shop. Go on to assert that you'd like to use go for a project or two. Go has a very well thought out build system. Can it be made to play nice with CMake? Here's an attempt.


In your top level CMakeLists.txt file, include GolangSimple.cmake:

cmake_minimum_required(VERSION 3.0)

In some/subdirectory lies your go program. Let's say it's a simple clint to list out the keys in a redis store. It depends upon a go get provided module. Here's what you'd need in your subdirectory's CMakeLists.txt file:

ADD_GO_INSTALLABLE_PROGRAM(redis_lister # executable name
                           redis_lister.go # `package main` source file
                           go_redis) # everything else is a dependency

This will create a binary called $CMAKE_CURRENT_BINARY_DIR/some/subdirectory/redis_lister. When you build the install target it will put said binary in $PREFIX/bin/redis_lister.

If you want to set build flags, you can use the CMake variable CMAKE_GO_FLAGS. If you want to include modules local to redis_lister.go, include them in the "./module" style. Be sure to put your top level source file in the package main.

Under the hood

A directory ${CMAKE_BINARY_DIR}/go is created and set as the $GOPATH environment variable in all go get and go build calls. All subprojects share a $GOPATH.


go get and go build are the only directives to the go command that are accounted for.

I've tested this on OS X 10.9 and Ubuntu 14.04. I haven't tried it on Windows, though I would be deeply surprised if it worked.