0

Presented below is my makefile

# Location of the CUDA Toolkit
CUDA_PATH  ?= /usr/local/cuda-6.0
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin g++
APP := app
OBJ := obj

source_files = $(shell find ./src -type f -regex '.*\.\(c\|cu\|cpp\)' | sed 's/^\.\/src\///')
obj_files = $(patsubst .c,obj/.o,$(patsubst .cpp,obj/.o,$(patsubst .cu,obj/.o,$(source_files))))

vpath %.c ./src/
vpath %.cpp ./src/
vpath %.cu ./src/

# Target rules
all: exe

%.o: %.cu
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@

exe: $(obj_files)
    $(NVCC) $^ -o $(APP)

run: exe
    ./$(APP)

clean:
    rm -f $(APP) obj/*.o

clobber: clean

I have two question:

1) How I can get makefile to work with cpp, c and cu files Now, I have

%.o: %.cu
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@

But, I want something like that

%.o: %.cu, %.cpp, %.c
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@

2) How I can place obj into different location. In my makefile I have variable $(OBJ) = obj and I want to put object files into this directory. How I can do this?

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • To answer question 2, to put object files into a directory you can either use "$ cp" (copy) at the end to manually copy files to the specified destination or use "$(NVCC) $+ -c -o $(OBJ)/$@ " – Santosh A Sep 26 '14 at 10:00
  • @SantoshA: Using those approaches, Make will not know how to build the prerequisites of the executable. And if you tell it explicitly, it will rebuild them even if they already exist and are up to date. – Beta Sep 26 '14 at 14:52
  • 1
    For your second question see [How to write a Makefile using different directories for targets and sources](http://stackoverflow.com/questions/18116685/how-to-write-a-makefile-using-different-directories-for-targets-and-sources/25953462#25953462) – Michaël Le Barbier Sep 26 '14 at 15:18

1 Answers1

1

1) You will need three separate rules. 2) It's not difficult, but you can get into trouble if the name of the target is not what the rule actually produces, so spell it out.

I'm not used to working with cuda code, so I'll assume that your recipe will work with C or C++ code as written.

$(OBJ)/%.o: %.cu
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@

$(OBJ)/%.o: %.cpp
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@

$(OBJ)/%.o: %.c
    -mkdir -p $(dir $@)
    $(NVCC) $+ -c -o $@
Beta
  • 96,650
  • 16
  • 149
  • 150