FindCUDA: Error compiling CPP+CUDA code in XCode
Hello everyone!
I'm trying to create a CPP+CUDA CMake project out of this example code provided by Thrust maintainers.
Since I want the project to be used also in XCode, I'm using the find_package(CUDA)
to resort to FindCUDA.cmake
included in CMake 3.10.2.
Here is the CMakeLists that I set up for the project:
cmake_minimum_required(VERSION 3.9)
set(CMAKE_CXX_STANDARD 11)
project(ThrustCPPCUDA
LANGUAGES CXX)
find_package(CUDA REQUIRED)
set(SRC_FILES src/host.cpp)
set(HDR_FILES include/device.h)
set(CUDA_FILES src/device.cu)
set(CUDA_LIB_NAME cudadevice)
option(CUDA_LIB_DYN "Build CUDA files as dynamic libraries (default OFF - STATIC)" OFF)
if(CUDA_LIB_DYN)
set(CUDA_LIB_TYPE SHARED)
else()
set(CUDA_LIB_TYPE STATIC)
endif()
message(STATUS "CUDA_LIB_TYPE: " ${CUDA_LIB_TYPE})
set(CUDA_SEPARABLE_COMPILATION ON)
message(STATUS "CUDA_SEPARABLE_COMPILATION: " ${CUDA_SEPARABLE_COMPILATION})
cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS Auto)
message(STATUS "CUDA_ARCH_FLAGS: " ${CUDA_ARCH_FLAGS})
list(APPEND CUDA_NVCC_FLAGS ${CUDA_ARCH_FLAGS})
message(STATUS "CUDA_NVCC_FLAGS: " ${CUDA_NVCC_FLAGS})
set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
message(STATUS "CUDA_HOST_COMPILER: " ${CUDA_HOST_COMPILER})
cuda_include_directories("$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}>")
cuda_add_library(${CUDA_LIB_NAME} ${CUDA_FILES} ${CUDA_LIB_TYPE})
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HDR_FILES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CUDA_INCLUDE_DIRS}
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(${PROJECT_NAME} PRIVATE ${CUDA_LIBRARIES} ${CUDA_LIB_NAME})
cuda_build_clean_target()
As you can notice, there is an option()
where I can set whether the CUDA files will be compiled and linked as a static or dynamic library.
Here is a table that reports what happen if I compile with either make
or Xcode
:
make |
Xcode (either via IDE or xcodebuild from CLI) |
|
---|---|---|
DYNAMIC | compiles, libcudadevice.dylib is generated and linked to ThrustCPPCUDA cpp application |
fails, saying that error: unable to open output file '<build_folder>/CMakeFiles/cudadevice.dir/Debug/cudadevice_intermediate_link.o': 'No such file or directory' . Here the error is that the Debug folder is not created in the process. Issuing mkdir <build_folder>/CMakeFiles/cudadevice.dir/Debug and retrying the compilation succeed. |
STATIC | compiles, libcudadevice.a is generated and linked to ThrustCPPCUDA cpp application. |
fails, saying that error: unable to open output file '<build_folder>/CMakeFiles/cudadevice.dir/Debug/cudadevice_intermediate_link.o': 'No such file or directory' . Here the error is that the Debug folder is not created in the process. Issuing mkdir <build_folder>/CMakeFiles/cudadevice.dir/Debug and retrying produce the following error clang: error: no such file or directory: '/Users/Claudio/GitHub/tutorials/CUDA/3_CUDA-OpenCV/buildx/Debug/libcudadevice.a' . It appears that xcodebuild is not capable of creating CUDA static library. |
There may be something wrong in my CMakeLists, but it is strange (to me) that using make
I have no problem whatsoever.
Thanks!
PS: are you going to support CUDA language in Xcode anytime soon? That would be great
Edited by Brad King