cmake: Target-specific preprocessor definitions fo

2019-02-25 01:44发布

问题:

I'm using cmake 2.8.1 on Mac OSX 10.6 with CUDA 3.0.

So I added a CUDA target which needs BLOCK_SIZE set to some number in order to compile.

cuda_add_executable(SimpleTestsCUDA
                    SimpleTests.cu
                    BlockMatrix.cpp 
                    Matrix.cpp
)

set_target_properties(SimpleTestsCUDA PROPERTIES COMPILE_FLAGS -DBLOCK_SIZE=3)

When running make VERBOSE=1 I noticed that nvcc is invoked w/o -DBLOCK_SIZE=3, which results in an error, because BLOCK_SIZE is used in the code, but defined nowhere. Now I used the same definition for a CPU target (using add_executable(...)) and there it worked.

So now the questions: How do I figure out what cmake does with the set_target_properties line if it points to a CUDA target? Googling around didn't help so far and a workaround would be cool..

回答1:

I think the best way to do this is by adding "OPTIONS -DBLOCK_SIZE=3" to cuda_add_executable. So your line would look like this:

cuda_add_executable(SimpleTestsCUDA
                SimpleTests.cu
                BlockMatrix.cpp 
                Matrix.cpp
                OPTIONS -DBLOCK_SIZE=3
)

Or you can set it before cuda_add_executable:

SET(CUDA_NVCC_FLAGS -DBLOCK_SIZE=3)


回答2:

The only workaround I found so far is using remove_definitions:

remove_definitions(-DBLOCK_SIZE=3)
add_definitions(-DBLOCK_SIZE=32)

Doing this before a target seems to help.



标签: cmake cuda