-2

Hello I was working on a C++ project, and had a cmake file that was working just fine, until I tried to add cuda into the C++ project. I am building this project on the NVIDIA Jetson Nano.

I get this error while building:

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

The rest of the error underneath that looks like this:

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'

I run my cmake file using a script I called confgure.sh, which looks like this:


#!/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

I run my make file using a script I called build.sh, which looks like this:


#!/bin/sh

make -C build

My Cmake File looks like this:

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)

I am lost on how to get CUDA added to my project that already contains a bunch of C++ files, and I need to be able to call my .cu files from a .cpp file other then main.cpp, and I need to get this building in CMake, and I am doing it on the jetson nano. Any help on solving this error?

user904542
  • 6,965
  • 5
  • 20
  • 28
  • "I get this error while building:" that thing that you have posted immediately after that (or anywhere else in this question that I can see) is not an error. – Robert Crovella Jul 21 '21 at 19:38
  • Sorry, I meant to say, "I get the error: nvlink fatal : Could not open input file 'CMakeFiles/MY_APP.dir/src/MY_APP.cpp.o' (target: sm_35) I Will edit the question – user904542 Jul 21 '21 at 19:44
  • I fixed the mistake in the question. – user904542 Jul 21 '21 at 19:57

1 Answers1

0

You are mixing up a lot of CUDA-related definitions and commands, including some from an earlier "era" of CUDA support in CMake.

Among other things:

  1. Your CMakeLists.txt is overriding your environment setting for the CUDA compiler location.
  2. ... and actually, you shouldn't bother setting that location anyway, since you've already set the CUDA toolkit root.
  3. Don't use find_package(CUDA) with CMake versions of 3.17 or 3.18, or later. For all relevant toolkit-related paths, use find_package(CUDAToolkit)`, which does... well, less but also more.
  4. Don't use cuda_add_+suffix commands. Since CMake supports CUDA natively, you use regular add_executable, add_library etc.

There are further issues with your CMakeLists.txt file - not all of them CUDA-related, but that should be enough to get you started. It may not in itself resolve the specific bottom-line problem you have, though.

You may want to have a look at public repositories using CUDA and recent CMake versions to get an idea of how this is done.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • A lot of those CMake related concepts I was mixing up resulted from running into error after error, and scowering the internet for fixes to try to correct each error and then getting a different error after getting the prior error to go away, and then rinse and repeat, resulting in a messier and messier CMakeLists.txt. – user904542 Jul 21 '21 at 20:43
  • @user904542: Well, you likely found some less-than-ideal suggestion, plus, most of them regard earlier versions of CMake and are no longer relevant/advisable. Also, if your CUDA installation is linked to by `/usr/local/cuda`, you don't even need to bother with the wrapper script, and you should just stick to the usual invocation of CMake. – einpoklum Jul 21 '21 at 20:54
  • I got it to compile, and run using your tips. I got rid of the lines changing the nvcc compiler, and I added this line: set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON) (by looking at this example): https://github.com/jclay/modern-cmake-cuda/blob/master/CMakeLists.txt. Now I have tried to move forward by seeing if it will work with 2 .cu files, but I'm getting undefined extern symbol, apparently I have to compile with -dc or relocatable device code for that. Trying to see how to do that in cmake, I might need to post a new question if I get stuck. – user904542 Jul 21 '21 at 22:14
  • @user904542: Well, that's a pretty rudimentary example, but it's quite valid... you don't always need separable compilation, but sometimes you do. You can read about it in Kitware's CMake documentation or just by DDG'ing it. – einpoklum Jul 21 '21 at 22:55