<bdo id='vr1qY'></bdo><ul id='vr1qY'></ul>

    <small id='vr1qY'></small><noframes id='vr1qY'>

      <tfoot id='vr1qY'></tfoot>

    1. <i id='vr1qY'><tr id='vr1qY'><dt id='vr1qY'><q id='vr1qY'><span id='vr1qY'><b id='vr1qY'><form id='vr1qY'><ins id='vr1qY'></ins><ul id='vr1qY'></ul><sub id='vr1qY'></sub></form><legend id='vr1qY'></legend><bdo id='vr1qY'><pre id='vr1qY'><center id='vr1qY'></center></pre></bdo></b><th id='vr1qY'></th></span></q></dt></tr></i><div id='vr1qY'><tfoot id='vr1qY'></tfoot><dl id='vr1qY'><fieldset id='vr1qY'></fieldset></dl></div>
    2. <legend id='vr1qY'><style id='vr1qY'><dir id='vr1qY'><q id='vr1qY'></q></dir></style></legend>
    3. 如何使用 CMake 将 C++ 编译为 CUDA

      How to compile C++ as CUDA using CMake(如何使用 CMake 将 C++ 编译为 CUDA)

        <legend id='3KFeD'><style id='3KFeD'><dir id='3KFeD'><q id='3KFeD'></q></dir></style></legend>

        • <bdo id='3KFeD'></bdo><ul id='3KFeD'></ul>
              <tbody id='3KFeD'></tbody>

            <small id='3KFeD'></small><noframes id='3KFeD'>

              <tfoot id='3KFeD'></tfoot>
                <i id='3KFeD'><tr id='3KFeD'><dt id='3KFeD'><q id='3KFeD'><span id='3KFeD'><b id='3KFeD'><form id='3KFeD'><ins id='3KFeD'></ins><ul id='3KFeD'></ul><sub id='3KFeD'></sub></form><legend id='3KFeD'></legend><bdo id='3KFeD'><pre id='3KFeD'><center id='3KFeD'></center></pre></bdo></b><th id='3KFeD'></th></span></q></dt></tr></i><div id='3KFeD'><tfoot id='3KFeD'></tfoot><dl id='3KFeD'><fieldset id='3KFeD'></fieldset></dl></div>
              1. 本文介绍了如何使用 CMake 将 C++ 编译为 CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在编写一些可以编译为 C++ 或 CUDA 的代码.在后一种情况下,它使用 CUDA 内核,在前一种情况下,它只运行常规代码.

                I'm writing some code that can be compiled as C++ or as CUDA. In the latter case, it makes use of CUDA kernels, in the former it just runs conventional code.

                创建了一个名为 test.cpp 的文件后,我可以手动编译它:

                Having created a file named test.cpp, I can compile it manually thus:

                g++ test.cpp          # build as C++ with GCC
                nvcc -x cu test.cpp   # build as CUDA with NVCC
                

                where -x cu 告诉 nvcc 虽然它是一个 .cpp 扩展名,但我希望它把它当作 CUDA.到目前为止,一切都很好.

                where -x cu tells nvcc that although it's a .cpp extension, I'd like it to treat it as CUDA. So far, so good.

                然而,当我迁移到使用 CMake 时,我不知道如何做同样的事情.即:如何让CMake用NVCC而不是GCC编译.cpp文件.

                However, when I migrate to using CMake, I don't know how to do the same thing. That is: how to ask CMake to compile the .cpp file with NVCC, rather than GCC.

                cmake_minimum_required(VERSION 3.9)
                project(cuda_test LANGUAGES CUDA CXX)
                add_executable(cuda_test test.cpp)     # builds with GCC
                

                如果我创建一个指向原始文件的符号链接:

                If I create a symlink to the original file:

                ln -s test.cpp test.cu
                

                然后更改 CMakeLists.txt:

                then change CMakeLists.txt:

                add_executable(cuda_test test.cu)     # builds with NVCC
                

                但我希望能够在 CMake 中指定等效于 NVCC 的 -x 开关,而不是玩带有扩展的游戏.类似:

                But I'd like to be able to specify the equivalent of NVCC's -x switch within CMake, rather than playing games with extensions. Something like:

                set_target_properties(cuda_test PROPERTIES FORCE_LANGUAGE CUDA)
                

                甚至

                set_target_properties(test.cpp PROPERTIES FORCE_LANGUAGE CUDA)
                

                这种咒语存在吗?

                推荐答案

                默认情况下,CMake 会根据文件的扩展名为源文件选择编译器.但是您可以通过设置 LANGUAGE 文件的属性:

                By default, CMake chooses compiler for a source file according to the file's extension. But you may force CMake to use the compiler you want by setting LANGUAGE property for a file:

                set_source_files_properties(test.cpp PROPERTIES LANGUAGE CUDA)
                

                (这只是使用普通编译器选项为文件调用 CUDA 编译器.您仍然需要为该编译器传递其他选项,以便它可以使用不寻常的文件扩展名.)

                (This just calls CUDA compiler for a file using normal compiler options. You still need to pass additional options for that compiler, so it could work with the unusual file's extension.)

                这篇关于如何使用 CMake 将 C++ 编译为 CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)
                    <tfoot id='70M9h'></tfoot>

                        <tbody id='70M9h'></tbody>
                    • <small id='70M9h'></small><noframes id='70M9h'>

                    • <legend id='70M9h'><style id='70M9h'><dir id='70M9h'><q id='70M9h'></q></dir></style></legend>

                        • <bdo id='70M9h'></bdo><ul id='70M9h'></ul>

                        • <i id='70M9h'><tr id='70M9h'><dt id='70M9h'><q id='70M9h'><span id='70M9h'><b id='70M9h'><form id='70M9h'><ins id='70M9h'></ins><ul id='70M9h'></ul><sub id='70M9h'></sub></form><legend id='70M9h'></legend><bdo id='70M9h'><pre id='70M9h'><center id='70M9h'></center></pre></bdo></b><th id='70M9h'></th></span></q></dt></tr></i><div id='70M9h'><tfoot id='70M9h'></tfoot><dl id='70M9h'><fieldset id='70M9h'></fieldset></dl></div>