[CMake] CUDA language support with host compiler flags

Robert Maynard robert.maynard at kitware.com
Wed Feb 27 09:00:11 EST 2019


You need to guard the flags with `$<COMPILE_LANGUAGE:CXX>` the
evaluation on a given compiler id is done for all sources of a target,
and not on each target source file.

So you will need something like:

set(cxx_flags "$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wall
-Wextra -pedantic>
                                               $<$<CXX_COMPILER_ID:MSVC>:/W4")
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>)

On Fri, Feb 22, 2019 at 4:11 AM Máté Ferenc Nagy-Egri via CMake
<cmake at cmake.org> wrote:
>
> Hi All!
>
> I am trying to compile CUDA code with controlling both host and device compiler flags.
>
> Currently my CMakeLists.txt looks like:
>
> ```
> cmake_minimum_required(VERSION 3.8) # CUDA language support
>
> project(CUDA_test LANGUAGES CXX CUDA)
>
> if (MSVC)
>   string(REGEX REPLACE "/W[0-9]" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
> endif (MSVC)
>
> set(Hdrs)
>
> set(Srcs Main.cu)
>
> add_executable(${PROJECT_NAME} ${Hdrs} ${Srcs})
>
> target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
>
> target_compile_options(${PROJECT_NAME} PRIVATE $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wall -Wextra -pedantic>
>                                                $<$<CXX_COMPILER_ID:MSVC>:/W4>)
>
> set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON
>                                                  CUDA_STANDARD 14
>                                                  CUDA_STANDARD_REQUIRED ON
>                                                  CUDA_EXTENSIONS OFF
>                                                  CXX_STANDARD 14
>                                                  CXX_STANDARD_REQUIRED ON
>                                                  CXX_EXTENSIONS OFF)
>
> source_group ("Headers" FILES ${Hdrs})
> source_group ("Sources" FILES ${Srcs})
> ```
>
> However, when I compile the code I get the following error:
>
> [1/3] Building CUDA object CMakeFiles/CUDA_test.dir/Main.cu.o
> FAILED: CMakeFiles/CUDA_test.dir/Main.cu.o
> /usr/bin/nvcc   -I/var/tmp/src/e75971a9-7e91-6137-abfa-df34048cc171/GCC-Debug-WSL -g   -Wall -Wextra -pedantic -std=c++14 -x cu -dc /var/tmp/src/e75971a9-7e91-6137-abfa-df34048cc171/GCC-Debug-WSL/Main.cu -o CMakeFiles/CUDA_test.dir/Main.cu.o && /usr/bin/nvcc   -I/var/tmp/src/e75971a9-7e91-6137-abfa-df34048cc171/GCC-Debug-WSL -g   -Wall -Wextra -pedantic -std=c++14 -x cu -M /var/tmp/src/e75971a9-7e91-6137-abfa-df34048cc171/GCC-Debug-WSL/Main.cu -MT CMakeFiles/CUDA_test.dir/Main.cu.o -o CMakeFiles/CUDA_test.dir/Main.cu.o.d
> nvcc fatal   : Unknown option 'Wall'
> ninja: build stopped: subcommand failed.
>
> CMake seems to pass -Wall -Wextra -pedantic to the device compiler as well, even though it is neither GNU nor Clang, but CUDA. How can I specify warning and similar flags separatly for the host and device compilers?
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list