2

following situation:

At the moment, I have a C++/CUDA project and I use make to produce two different executables: The first one is only a CPU version, which ignores the CUDA parts, and the second one is the GPU version.

I can run make to build both versions, make cpu to only build the CPU version and make gpu to only build the GPU version. For the CPU version, the Intel compiler is used , and for the GPU version, nvcc with g++ is used instead.

Why I consider using cmake:

Now I would also like to be able to build the project under Windows with nmake. Therefore, I thought that cmake was the appropriate solution to generate a make or nmake file based on the platform.

Problem:

However, it seems to be difficult to specify a compiler based on a target (Using CMake with multiple compilers for the same language).

Question:

Which is the best way to achieve the behavior as described above, building different executables for different architectures with different compilers out of the same code base as well for Windows as for Linux, by using cmake?

Community
  • 1
  • 1
Fabian
  • 547
  • 1
  • 4
  • 17
  • If you have a defined number of targets use toolchain files plus build scripts (see e.g. [here](http://stackoverflow.com/questions/36173840/how-to-instruct-cmake-to-use-the-build-architecture-compiler)). – Florian Jul 15 '16 at 10:13

1 Answers1

1

In your situation you can just use cmake's CUDA support: FindCUDA.

Your cmake lists would then look like:

find_package(CUDA)

add_executable(cpu ${SRC_LIST})
CUDA_ADD_EXECUTABLE(gpu ${SRC_LIST})
David Marquant
  • 2,039
  • 14
  • 12
  • I have the problem that parts of the project (which also include relevant code for the GPU version) are implemented in cpp files. Therefore, I added set_source_files_properties( ${src_file} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ ) in order to compile the file with nvcc. However, this breaks the CPU executable. Do you have an idea why this is the case? – Fabian Jul 27 '16 at 14:56
  • I assume that cmake builds the .o file from the .cpp file once with nvcc and reuses that for the CPU build. To work around that you could try separating CPU and GPU build in two CMakeLists included Wirth add_subdirectory. – David Marquant Jul 27 '16 at 15:18