How to build CUDA programs using CMake

CMake is a popular option for cross-platform compilation of code. CMake has support for CUDA built in, so it is pretty easy to build CUDA source files using it.

Let us assume that I want to build a CUDA source file named src/ To build this using CMake, we create a file named CMakeLists.txt containing these lines:

# CMakeLists.txt to build
cmake_minimum_required(VERSION 2.8)

# Specify binary name and source file to build it from

To build our program on Linux, we first run cmake to generate a Makefile and use that Makefile to build and run. On Windows, cmake would generate a Visual Studio solution, that can be used to build the code.

Steps to build and run program on Linux:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
$ ./hellocuda

Any non-trivial CUDA program will need special compilation flags, include directories, library directories and multiple source files. The CMakeLists.txt shown below is an example of how to configure all these features:

Typically nvcc calls both the host compiler and device compiler by itself on both .cu and .cpp files. The behavior of the Makefile generated by CMake is slightly different: it calls the default C++ compiler for .cpp files and calls nvcc for .cu files. This does not cause any problems, just something you might need to be aware of.

For information about other CMake commands for CUDA see the file /usr/share/cmake-2.8/Modules/FindCUDA.cmake and the files inside /usr/share/cmake-2.8/Modules/FindCUDA/.

Note: Include directories cannot be specified for a particular target using target_include_directories. This is a known limitation of the CUDA module of CMake currently. More details here.

Tried with: CMake 2.8.7, CUDA 5.0 and Ubuntu 12.04 LTS


5 thoughts on “How to build CUDA programs using CMake

  1. I’m trying to use Cmake to generate some code, but I have some problem at compile time.
    In particular if after the cmake command, if I try to execute the command make, I have the following error:
    [ 33%] Building NVCC (Device) object CMakeFiles/trainlenet.dir/
    nvcc fatal : redefinition of argument ‘std’
    CMake Error at (message):
    Error generating
    make[2]: *** [CMakeFiles/trainlenet.dir/] Error 1
    make[1]: *** [CMakeFiles/trainlenet.dir/all] Error 2

    Any ideas on how can I solve this problem?


Leave a Reply to Mary Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.