1

Introduction

I've been trying to compile my c++ program by using cmake + visual studio 2022 (msvc) and I have some problems with the cmakefile setup.

The problem

The program compiles when I don't have .cu files and it does not compile when I use .cu files.

Btw, in the c++ files I can use the cuda libraries.

This is the error I get in visual studio:

nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified

Cmake file

Here's the simplified version of my cmake file:

cmake_minimum_required(VERSION 3.16)
project(collisions LANGUAGES CXX CUDA)

# Add the CUDA libraries
find_package(CUDA REQUIRED)
enable_language(CUDA)

# Source code
file(GLOB PROJECT_SOURCES_CPP "src/*/*.cpp" "src/*/*/*.cpp")
file(GLOB PROJECT_SOURCES_CU "src/*/*.cu")

# Include OpenMP
find_package(OpenMP)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

add_executable(${PROJECT_NAME} ${PROJECT_SOURCES_CPP} ${PROJECT_SOURCES_CU})

add_dependencies(${PROJECT_NAME} glfw glad glm)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/)

if (MSVC)
    set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "/openmp:llvm")
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

target_link_libraries(${PROJECT_NAME} PRIVATE glfw glad glm OpenMP::OpenMP_CXX ${CUDA_LIBRARIES})

I tried a lot of things with ChatGPT but we could not find the problem. I would like to fix this cmake file so that I can compile my program.

  • 1
    1. You should upgrade to a newer version of CMake; CUDA support, in particular library discovery, has improved since v3.16. 2. `find_package(CUDA)` is no longer appropriate with newer versions of CMake - that module was created before CUDA became a first-class language in CMake. See [this question](https://stackoverflow.com/q/66327073/1593077) and [this answer](https://stackoverflow.com/a/66341005/1593077). – einpoklum Jul 16 '23 at 17:59

1 Answers1

1

I managed to fix it by myself.

Before (error showing):

if (MSVC)
    set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "/openmp:llvm")
endif()

After (issue fixed):

if (MSVC)
    set(my_cxx_flags /openmp::llvm)
    target_compile_options(${PROJECT_NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${my_cxx_flags}> )
endif()