FindCuda.cmake Macro Problem?

Hi all,

I am simultaneously learning CMake and how successfully compile a cuda project. As an exercise, I decided to take the cppIntegration project from the SDK, and figure out how to get it to compile with CMake, using the FindCuda.cmake utility downloaded from http://www.sci.utah.edu/~abe/FindCuda.html. I successfully got the example available in the download to work. Using that example as a template, I attempted to extract the relevant parts of the CMakeList.txt files and apply the same structure to the cppIntegration project. No Luck! I cannot get the cppIntegration project to successfully compile.

My directory structure is

“bin” (for the out-of-source build)

“CMake” (the provided code)

“src” (This contains the two *.cu files, the two *.cpp files of cppIntegration, and a CMakeLists.txt file)

CmakeLists.txt

The top directory CMakeLists.txt file looks like

[codebox]# The name of our project is “CPPINTEGRATION”. CMakeLists files in this project can

refer to the root source directory of the project as ${CPPINTEGRATION_SOURCE_DIR} and

to the root binary directory of the project as ${CPPINTEGRATION_BINARY_DIR}.

cmake_minimum_required (VERSION 2.6)

project (CPPINTEGRATION)

INCLUDE(${CMAKE_SOURCE_DIR}/CMake/cuda/FindCuda.cmake)

SUBDIRS(

src

)

[/codebox]

The CMakeList.txt in the “src” looks like

[codebox]# Add current directory to the nvcc include line.

CUDA_INCLUDE_DIRECTORIES(

${CMAKE_CURRENT_SOURCE_DIR}

)

INCLUDE_DIRECTORIES( ${FOUND_CUDA_NVCC_INCLUDE} ${FOUND_CUT_INCLUDE})

##################################################

Use the CUDA_COMPILE macro.

CUDA_COMPILE(CUDA_FILES cppIntegration_kernel.cu cppIntegration.cu)

ADD_EXECUTABLE(cuda_compile_example

${CUDA_FILES}

main.cpp

cppIntegration_gold.cpp

)

[/codebox]

cmake appears to find all the right files, includes, libraries (with a little help)

But the verbose make generates the following error

/sw/bin/cmake -H/Users/phillicl/miniMD/IncrementalCUDATest -B/Users/phillicl/miniMD/IncrementalCUDATest/bin --check-build-system CMakeFiles/Makefile.cmake 0

/sw/bin/cmake -E cmake_progress_start /Users/phillicl/miniMD/IncrementalCUDATest/bin/CMakeFiles /Users/phillicl/miniMD/IncrementalCUDATest/bin/CMakeFiles/progress.make

make -f CMakeFiles/Makefile2 all

make -f src/CMakeFiles/cuda_compile_example.dir/build.make src/CMakeFiles/cuda_compile_example.dir/depend

cd /Users/phillicl/miniMD/IncrementalCUDATest/bin && /sw/bin/cmake -E cmake_depends “Unix Makefiles” /Users/phillicl/miniMD/IncrementalCUDATest /Users/phillicl/miniMD/IncrementalCUDATest/src /Users/phillicl/miniMD/IncrementalCUDATest/bin /Users/phillicl/miniMD/IncrementalCUDATest/bin/src /Users/phillicl/miniMD/IncrementalCUDATest/bin/src/CMakeFiles/cuda_compile_example.dir/DependInfo.cmake --color=

make -f src/CMakeFiles/cuda_compile_example.dir/build.make src/CMakeFiles/cuda_compile_example.dir/build

Linking CXX executable cuda_compile_example

cd /Users/phillicl/miniMD/IncrementalCUDATest/bin/src && /sw/bin/cmake -E cmake_link_script CMakeFiles/cuda_compile_example.dir/link.txt --verbose=1

/usr/bin/c++ -O3 -DNDEBUG -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/cuda_compile_example.dir/main.o CMakeFiles/cuda_compile_example.dir/cppIntegration_gold.o -o cuda_compile_example /usr/local/cuda/lib/libcuda.dylib /usr/local/cuda/lib/libcudart.dylib

Undefined symbols:

“_runTest”, referenced from:

  _main in main.o

“_cutCheckCmdLineFlag”, referenced from:

  _main in main.o

ld: symbol(s) not found

collect2: ld returned 1 exit status

make[2]: *** [src/cuda_compile_example] Error 1

make[1]: *** [src/CMakeFiles/cuda_compile_example.dir/all] Error 2

make: *** [all] Error 2

As you can see, it does not appear to actually compile the *.cu files and thus has no object files! Its as if the macro is not working somehow. (I get an identical output if I “comment” out the “CUDA_COMPILE”)

What am I missing?

The CUDA_COMPILE macro shouldn’t be used directly.

Instead of ADD_EXECUTABLE, you should be using CUDA_ADD_EXECUTABLE, it will identify whether to use nvcc or g++ to compile based on the extension of the source file.

Yes, that definitely made a big difference! The trouble now is that I am getting a duplicated symbol when linking the files.

My “src/CMakeLists.txt” now looks like.

[codebox]

Add current directory to the nvcc include line.

CUDA_INCLUDE_DIRECTORIES(

${CMAKE_CURRENT_SOURCE_DIR}

${FOUND_CUT_INCLUDE}

)

INCLUDE_DIRECTORIES( ${FOUND_CUDA_NVCC_INCLUDE} ${FOUND_CUT_INCLUDE})

############################################################

##################

Use one executable only.

CUDA_ADD_EXECUTABLE(test

cppIntegration.cu

cppIntegration_kernel.cu

main.cpp

cppIntegration_gold.cpp

)[/codebox]

Here is my error!

Linking CXX executable test

cd /Users/phillicl/miniMD/IncrementalCUDATest/src/src && /sw/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1

/usr/bin/c++ -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/test.dir/cuda/cppIntegration.cu_test_generated.o CMakeFiles/test.dir/cuda/cppIntegration_kernel.cu_test_generated.o CMakeFiles/test.dir/main.o CMakeFiles/test.dir/cppIntegration_gold.o -o test /usr/local/cuda/lib/libcuda.dylib /usr/local/cuda/lib/libcudart.dylib

ld: duplicate symbol ___globfunc__Z6kernelPi in CMakeFiles/test.dir/cuda/cppIntegration_kernel.cu_test_generated.o and CMakeFiles/test.dir/cuda/cppIntegration.cu_test_generated.o

The SDK examples “#include” the _kernel files into the normal .cu file. So you either need to remove that #include or take the cppIntegration_kernel.cu off of the src list in CMakeLists.txt.

NM,

Fixed my own problem.
The issue was that the kernel is included in the other *.cu file and should not have been part of the CUDA_ADD_EXECUTABLE command.

And then I needed to link to the libcutil.a library (which on my system is located in a different part of the directory structure than the cutil.h. The latter is in common/inc. The former in in lib)

It compiles! Thanks for your help.