11

I want to compile a very basic hello world level Cuda program under Linux. I have three files:

  • the kernel: helloWorld.cu
  • main method: helloWorld.cpp
  • common header: helloWorld.h

Could you write me a simple Makefile to compile this with nvcc and g++?

Thanks,
Gabor

Framester
  • 33,341
  • 51
  • 130
  • 192
Vereb
  • 14,388
  • 2
  • 28
  • 30
  • Please, tag your make-related questions with [make] tag. Rationale: http://meta.stackexchange.com/questions/24030/why-do-they-specify-makefile-tag-instead-of-make/26567#26567 – P Shved Oct 23 '09 at 20:44

4 Answers4

8

I've never heard of Cuda before, but from the online documentation it looks as if X.cu is supposed to be compiled into X.o, so having helloWorld.cu and helloWorld.cpp is not a good idea. With your permission I'll rename the "kernel" helloKernel.cu, then this should work:

NVCC = nvcc

helloWorld.o: helloWorld.cpp helloWorld.h
    $(NVCC) -c %&lt -o $@

helloKernel.o: helloKernel.cu
    $(NVCC) -c %&lt -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@

(Note that those leading spaces are tabs.)

If that works, try a slicker version:

NVCC = nvcc

helloWorld.o: %.o : %.cpp %.h
helloKernel.o: %.o : %.cu

%.o:
    $(NVCC) -c %&lt -o $@

helloWorld: helloWorld.o helloKernel.o
    $(NVCC) %^ -o $@
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Can you use nvcc on the normal cpp files? – teeks99 Oct 23 '09 at 14:24
  • I don't have access to nvcc, so I can't be certain, but according to the documentation, yes. For ordinary compiler tasks, nvcc hands the job off to a standard compiler like g++. – Beta Oct 23 '09 at 15:21
4

Just in case, here's my variant. I use it to compile CUDA projects on Mac, but I think it will suit Linux too. It requires CUDA SDK.

BINDIR = ./ # places compiled binary in current directory
EXECUTABLE := helloWorld

CCFILES := helloWorld.cpp
CUFILES := helloWorld.cu

# an ugly part - setting rootdir for CUDA SDK makefile
# look for common.mk - I don't know where SDK installs it on Linux -
# and change ROOTDIR accordingly 
ROOTDIR := /Developer/GPU\ Computing/C/common

include $(ROOTDIR)/../common/common.mk
fjarri
  • 9,546
  • 39
  • 49
  • 1
    I know the age of this post, but this information might come in handy for people starting to work with CUDA < 5.x. Actually, the accepted answer is relying on the common.mk file which is pulling in the cutil dependency as "-lcutil". Anyhow, for CUDA >= 5.x, usage of common.mk is no longer required/encouraged, as all Makefiles are said to be self-contained (s. also the Changelog) – Shadow Oct 30 '12 at 14:59
2

My version, verbose but transparent:

myapp: myapp.o
    g++ -fPIC -o $@ $< -L /usr/local/cuda/lib -lcudart

myapp.o: myapp.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c $<

matrixMul: matrixMul.o
    g++ -fPIC -o $@ $< -L /usr/local/cuda/lib -lcudart

# It MUST be named .cu or nvcc compiles as regular C !!! (no __global__)
matrixMul.o: matrixMul.cu
    /usr/local/cuda/bin/nvcc --compiler-options -fno-strict-aliasing \
    -I/usr/local/cuda/include \
    -DUNIX -O2 -o $@ -c $<
keraba
  • 554
  • 2
  • 9
1

Here is an example what my current project looks like. As you can see there is a few OpenGL libraries

ce : cudaExample.c cudaExample.h
    cp cudaExample.c cudaExample.cu
    /usr/local/cuda/bin/nvcc -arch=sm_20 -o ce -lglut -lGL -lGLU -lXext -lXmu -lX11 -lm  cudaExample.cu

then run make ce and ./ce

John Riselvato
  • 12,854
  • 5
  • 62
  • 89