user3727610
user3727610

Reputation:

check if nvcc is available in makefile

I have two versions of a function in an application, one implemented in CUDA and the other in standard C. They're in separate files, let's say cudafunc.h and func.h (the implementations are in cudafunc.cu and func.c). I'd like to offer two options when compiling the application. If the person has nvcc installed, it'll compile the cudafunc.h. Otherwise, it'll compile func.h.

Is there anyway to check if a machine has nvcc installed in the makefile and thus adjust the compiler accordingly?

Thanks a bunch,

Upvotes: 2

Views: 1537

Answers (2)

Robert Crovella
Robert Crovella

Reputation: 151954

This should work, included in your Makefile:

        NVCC_RESULT := $(shell which nvcc 2> NULL)
        NVCC_TEST := $(notdir $(NVCC_RESULT))
ifeq ($(NVCC_TEST),nvcc)
        CC := nvcc
else
        CC := g++
endif
test:
        @echo $(CC)

For GNU make, the recipe line(s) (after test:) actually start with tab characters.

With the above preamble, you can use conditionals based on the CC variable to control the remainder of your Makefile.

You would change the test: target to whatever target you want to be built conditionally.

As a test, just run the above with make. You should get output of nvcc or g++ based on whatever was detected.

Upvotes: 0

Phil Miller
Phil Miller

Reputation: 38118

You could try a conditional, like

ifeq (($shell which nvcc),) # No 'nvcc' found
func.o: func.c func.h
HEADERS += func.h
else
func.o: cudafunc.cu cudafunc.h
        nvcc -o $@ -c $< . . . 
CFLAGS += -DUSE_CUDA_FUNC
HEADERS += cudafunc.h
endif

And then in the code that will call this function, it can test #if USE_CUDA_FUNC to decide which header to include and which interface to call.

Upvotes: 2

Related Questions