David S.
David S.

Reputation: 128

CMake-based build of CUDA app fails - no files passed to linker

I'm trying to use CMake with a CUDA project of mine, but I'm having trouble getting it to build the executable when compiled on a system that has a CUDA-enabled device.

The CMakeLists.txt in question is below. It supports systems with and without CUDA-enabled devices, and builds just fine on my Macbook which doesn't have CUDA.

cmake_minimum_required (VERSION 2.8)
message(STATUS "CMake version: ${CMAKE_VERSION}")
project(stockModel)

# Grab the CUDA package
find_package(CUDA)
set(GPU_ACCELERATED ${CUDA_FOUND})

# Set directory and compilation flags for both g++ and nvcc
set(CMAKE_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}
    -gencode arch=compute_50,code=sm_50; -std=c++11; -lcurand;"
   )
set(CUDA_PROPAGATE_HOST_FLAGS off)

# Add directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/build/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core/)
if (${GPU_ACCELERATED})
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/support/)
endif()

# Setup environments, depending on GPU accel. status
set(SRCS build/main.cpp core/callModels.cpp)
set(INCS core/callModels.h)

if (${GPU_ACCELERATED})
  set(SRCS ${SRCS} support/prng.cu support/debugCFP.cu)
  set(INCS ${INCS} support/prng.h support/debugCFP.h)

  set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/core/callModels.cpp
                          PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ
                         )
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
                          -L/usr/local/cuda/lib64 -lcuda -lcudart"
 )
endif()

# Create executable
message(STATUS "Sources: ${SRCS}")
message(STATUS "Includes: ${INCS}")
cuda_add_executable(stockModel ${SRCS} ${INCS})

The error I get when I attempt to build on my Jetson TX1 is as follows:

...
[ 80%] Building CXX object CMakeFiles/stockModel.dir/main.cpp.o
[100%] Linking CXX executable stockModel
c++: fatal error: no input files
compilation terminated.
...

Any ideas as to what is going wrong here? Obviously it has something to do with the CUDA 'extras', but I'm at a loss as to what is causing this.

Let me know if you need more details.

Here is the relevant part of the verbose output:

...
[100%] Linking CXX executable stockModel
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/stockModel.dir/link.txt --verbose=1
/usr/bin/c++   -std=c++11 -pthread  
c++: fatal error: no input files
compilation terminated.

I've uploaded the full make VERBOSE=1 output to this gist on GitHub.

Upvotes: 1

Views: 625

Answers (1)

einpoklum
einpoklum

Reputation: 131435

CMake is sometimes finicky about spaces and list combinations. I know that doesn't sound like much of an explanation, but I'm not much of an expert.

What you need to do is replace this:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
                          -L/usr/local/cuda/lib64 -lcuda -lcudart"

with this:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/cuda/lib64 -lcuda -lcudart")

(single line). That should do it. At least - it does on my system (I created dummy source files with your files' names to try this out).

Upvotes: 2

Related Questions