简体   繁体   中英

PyCuda C++ kernel "error: this declaration may not have extern "C" linkage"

I tried using std::tuple in my kernel code, but received many error: this declaration may not have extern "C" linkage errors that pointed to utility and tuple

It complains on the include. The following repros for me.

from pycuda.compiler import SourceModule
mod = SourceModule("""#include <tuple>""")

Do I need to do something special in my kernel code or in my Python code to specify I want to use the C++ compiler?

Cuda version: 11.8

PyCuda version: 2022.2.1

Do I need to do something special in my kernel code or in my Python code to specify I want to use the C++ compiler?

To be clear, you are using the C++ compiler. But PyCUDA automagically wraps the code you pass into a SourceModule instance in extern “C” unless you explicitly tell it not to:

Unless no_extern_c is True, the given source code is wrapped in extern “C” { … } to prevent C++ name mangling.

The underlying reason from a C++ perspective is that templated instances of types and functions can't resolve with C linkage, thus the error.

However, even after you fix that problem, prepared to be disappointed. CUDA supports a lot of C++ language features, but it doesn't support the standard library and you can't use std::tuple within kernel code. NVIDIA does provide their own (very limited) reimplementation of the C++ standard library, and it does have a basic tuple type. That might work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM