3

I previously asked: How to compile C++ as CUDA using CMake which was super helpful. But then realised I had a follow-on question.

What I want to do is compile one file twice, using different compilers. eg:

cmake_minimum_required(VERSION 3.9)
project(cuda_test LANGUAGES CUDA CXX)

add_executable(cuda_test_host test.cpp)     # build with GCC for host

set_source_files_properties(test.cpp PROPERTIES LANGUAGE CUDA)
add_executable(cuda_test_cuda test.cpp)     # build with NVCC for CUDA

But of course, the set_source_files_properties is not specific to a particular target, so both cuda_test_host and cuda_test_cuda end up being built by NVCC.

I've seen similar questions on StackOverflow suggesting making sub-directories with different CMake files in, but I'd like to avoid that if at all possible.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Edd Inglis
  • 1,067
  • 10
  • 22
  • You could use a symbolic link to give the same file another name. Add one to first target and another to the second. – eerorika Nov 14 '19 at 17:32
  • With [set_target_properties](https://cmake.org/cmake/help/latest/command/set_target_properties.html) you can modify some [properties](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets) on a target level. Not sure if this is what you want. – Former contributor Nov 14 '19 at 23:19
  • @eerorika, I had that idea in my first post. It's certainly very appealing as a one-off, but less practical in my wider setup. Thanks for your input though. – Edd Inglis Nov 15 '19 at 09:49
  • This is not supported in CMake. Language is a per-file property, not a binary per-file-and-target property (and certainly not a per-user-defined-target property). – einpoklum Dec 31 '21 at 20:18

0 Answers0