check if nvcc is available in makefile

青春壹個敷衍的年華 提交于 2019-12-07 19:37:30

问题


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,


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/24599434/check-if-nvcc-is-available-in-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!