Confusion of linking two cuda files

I originally wrote everything in one cuda file named main.cu, which is too big. Then I decide to split it and position the kernel part in a separate cuda file, say kernel.cu, and make it included in the main.cu

When editing the make file for them, it seems I even don’t need to mention the kernel.cu file at all and the make can find the kernel.cu file automatically via the include part. Is this correct?

Yes, with a caveat. The makefile doesn’t need to be changed for the compiler to find the included file. However, there now is a new dependency: You want main.cu be recompiled whenever kernel.cu changes, so that these changes are picked up for the generated binary. Unless your makefile has the necessary machinery for automatic dependency generation, you need to edit the makefile to reflect that dependency.

If e.g. the rule for main.cu in your makefile previously looked like this

main.o : main.cu someheader.h

        nvcc -arch compute_13 -c main.cu -o  main.o

you want to change that to

main.o : main.cu someheader.h kernel.cu

        nvcc -arch compute_13 -c main.cu -o  main.o

Some people would prefer to name kernel.cu as kernel.cuh to indicate it is not a separate compilation unit, but gets included into main.cu. This is a matter of personal taste though.

Thanks for the reply!

What if I use prefix rule in Makefile?

$(CU_SRC)= main.cu
$(CU_H)=kernel.cuh
$(C_H)=util.h
$(CU_OBJ)= $(CU_SRC).o

Why this doesn’t work?

.SUFFIXES: .cu .cuh .h

%.cu.o: %.cu %cuh %h
nvcc -arch compute_20 -c main.cu -o main.o

$(CU_OBJ): $(CU_SRC) $(CU_H) $(C_H)

What happens if you try your above makefile? I don’t immediately see anything wrong with it apart from the obvious missing dots:

%.cu.o: %.cu %.cuh %.h