C++ with CUDA project in CMake gives error: nvlink fatal : Could not open input file 'CMakeFiles/MY_APP.dir/src/MY_APP.cpp.o' (target: sm_35)

C++ with CUDA project in CMake gives error: nvlink fatal : Could not open input file 'CMakeFiles/MY_APP.dir/src/MY_APP.cpp.o' (target: sm_35)

你好,我正在处理一个 C++ 项目,并且有一个运行良好的 cmake 文件,直到我尝试将 cuda 添加到 C++ 项目中。我正在 NVIDIA Jetson Nano 上构建这个项目。

构建时出现此错误:

nvlink fatal : Could not open input file 'CMakeFiles/MY_APP.dir/src/MY_APP.cpp.o' (target: sm_35)

下方的其余错误如下所示:

CMakeFiles/MY_APP.dir/build.make:552: recipe for target 

'CMakeFiles/MY_APP.dir/cmake_device_link.o' failed
make[2]: *** [CMakeFiles/MY_APP.dir/cmake_device_link.o] Error 1
make[2]: Leaving directory '/home/me/Code/MyApp/build'
CMakeFiles/Makefile2:127: recipe for target 'CMakeFiles/MY_APP.dir/all' failed
make[1]: *** [CMakeFiles/MY_APP.dir/all] Error 2
make[1]: Leaving directory '/home/me/Code/MY_APP/build'
Makefile:155: recipe for target 'all' failed
make: *** [all] Error 2
make: Leaving directory '/home/me/Code/MY_APP/build'

我 运行 我的 cmake 文件使用我称为 confgure.sh 的脚本,它看起来像这样:


#!/bin/sh

cmake -S . -B build -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 -DCMAKE_CUDA_COMPILER=/usr/local/cuda-10.2/bin/nvcc

我 运行 我的 make 文件使用了我称为 build.sh 的脚本,它看起来像这样:


#!/bin/sh

make -C build

我的 Cmake 文件如下所示:

cmake_minimum_required(VERSION 3.21.0)
project(MY_APP VERSION 0.0.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

enable_language(CUDA)

# Pass options to NVCC
set(
    CUDA_NVCC_FLAGS
    ${CUDA_NVCC_FLAGS};
    -O3 -gencode arch=compute_35,code=sm_35
    )

set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
FILE(GLOB_RECURSE MY_CUDA_SRCS src/*.cu)

configure_file(src/MyAppConfig.h.in MyAppConfig.h)

#collect cpp files
FILE(GLOB_RECURSE SRC src/*.cpp)

find_package(CUDA QUIET)
if(CUDA_FOUND)
    SET(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
    include_directories(${CUDA_INCLUDE_DIRS})
    get_filename_component(CUDA_LIBRARY_DIR ${CUDA_CUDART_LIBRARY} DIRECTORY)
    set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-L${CUDA_LIBRARY_DIR}")
    SET(ALL_CUDA_LIBS ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY} ${CUDA_cublas_LIBRARY})
    #${CUDA_CUDART_LIBRARY}
    #${CMAKE_CUDA_RUNTIME_LIBRARY}
    #)
    SET(LIBS ${LIBS} ${ALL_CUDA_LIBS})
    message(STATUS "CUDA_LIBRARIES: ${CUDA_INCLUDE_DIRS} ${ALL_CUDA_LIBS}")
    set(CUDA_PROPAGATE_HOST_FLAGS ON)
    set(CUDA_SEPARABLE_COMPILATION ON)
    list(APPEND CUDA_NVCC_FLAGS -gencode=arch=compute_35,code=sm_35)

    #collect CUDA files
    FILE(GLOB_RECURSE CUDA_SRC src/*.cu)

    #build static library
    #CUDA_ADD_LIBRARY(my_cuda_lib ${CUDA_SRC} STATIC)

    cuda_compile(cuda_objs ${CUDA_SRC})
    SET(SRC ${cuda_objs} ${SRC})

    SET(LIBS ${LIBS} ${my_cuda_lib})
endif()

link_libraries(${cuda_objs})

set_source_files_properties(${SRC} PROPERTIES LANGUAGE CUDA)

message("using cuda_add_executable")
cuda_add_executable(${PROJECT_NAME} ${SRC})

target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR})

target_link_libraries(${PROJECT_NAME} ${LIBS})

#DOWNLOAD ALL THE SUBMODULES
find_package(Git QUIET)
if (GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
    option(GIT_SUBMODULE, "Check submodules during build" ON)
    if (GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE}
                        submodule update --init --recursvie
                        WORKING_DIRECTORY {CMAKE_CURRENT_SOURCE_DIR}
                        RESULT_VARIABLE_GIT_SUBMOD_RESULT)
        if (NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR
                "git submodule update --init failed with ${GIT_SUMOD_RESULT},
                please check submodule")
        endif()
    endif()
endif()

#CHECK ALL THE SUBMODULES
if (NOT EXISTS
"${PROJECT_SOURCE_DIR}/external/Simple-Websocket-Server/CMakeLists.txt")
    message(FATAL_ERROR
            "The Simple-Websocket-Server submodule was not downloaded!
            GIT_SUBMODULE was turned off or failed. Please update submodule")
endif()

add_subdirectory(external/Simple-Websocket-Server)

include_directories(PUBLIC external/Simple-Websocket-Server)

find_package(PythonLibs REQUIRED)
find_package(pybind11 REQUIRED)

include_directories(${PYTHON_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME}
    curl pthread crypto boost_system jsoncpp ${PYTHON_LIBRARIES} cudart 
    #<some-of-my-other-libraries>
)

install(TARGETS ${PROJECT_NAME} DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/MyAppConfig.h" DESTINATION include)

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${MY_APP_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${MY_APP_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${MY_APP_VERSION_PATCH}")
include(CPack)

set(CMAKE_CUDA_COMPILE_WHOLE_COMPILATION 
    "${CMAKE_CUDA_COMPILER} ${_CMAKE_CUDA_EXTRA_FLAGS} -c ${MY_CUDA_SRCS}")

message(CMAKE_CUDA_COMPILE_WHOLE_COMPILATION)

我不知道如何将 CUDA 添加到我已经包含一堆 C++ 文件的项目中,我需要能够从 .cpp 文件调用我的 .cu 文件 main.cpp ,我需要在 CMake 中构建这座建筑,我正在 jetson nano 上进行。对解决此错误有帮助吗?

您混淆了很多与 CUDA 相关的定义和命令,包括 CMake 支持 CUDA 的早期“时代”的一些定义和命令。

除其他外:

  1. 您的 CMakeLists.txt 正在覆盖 CUDA 编译器位置的环境设置。
  2. ...实际上,无论如何您都不应该费心设置该位置,因为您已经设置了 CUDA 工具包根目录。
  3. 不要将 find_package(CUDA) 与 CMake 3.17 或 3.18 或更高版本一起使用。对于所有与工具包相关的路径,请使用 find_package(CUDAToolkit)`,它确实...好吧,更少但也更多。
  4. 不要使用 cuda_add_+后缀命令。由于 CMake 本机支持 CUDA,因此您可以使用常规 add_executableadd_library

您的 CMakeLists.txt 文件还有其他问题 - 并非所有问题都与 CUDA 相关,但这应该足以让您入门。不过,它本身可能无法解决您遇到的具体底线问题。

您可能想查看 public 个使用 CUDA 和最新 CMake 版本的存储库,以了解这是如何完成的。