check if nvcc is available in makefile

2019-05-29 08:39发布

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,

2条回答
The star\"
2楼-- · 2019-05-29 09:15

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.

查看更多
Viruses.
3楼-- · 2019-05-29 09:26

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.

查看更多
登录 后发表回答