如何解决Rust用cc调用nvcc编译CUDA内核的错误?
Fixing nvcc Compilation Error When Linking CUDA Kernels to Rust via cc Crate
First, let's break down the root cause of your error. The truncated error message showing -cudar (instead of -cudart=shared) and the messed-up command line hints that your nvcc parameters are being split incorrectly by the cc crate.
Key Issues in Your Current build.rs
- Split
-gencodeparameter: NVCC requires-gencode arch=compute_61,code=sm_61to be passed as a single command-line argument, but you're splitting it into two separate.flag()calls. This confuses nvcc's argument parser, leading to malformed commands. - Misaligned argument parsing: The broken parameter handling cascades into other parts of the command line, causing fragments like
-cudar(a truncated-cudart=shared) to appear.
Corrected build.rs Implementation
Here's the fixed version with proper parameter handling:
extern crate cc; fn main() { let mut build = cc::Build::new(); // Enable CUDA compilation mode build.cuda(true); // Pass -cudart=shared as a single unbroken flag build.flag("-cudart=shared"); // Critical: Pass the entire -gencode argument as one flag (don't split!) build.flag("-gencode arch=compute_61,code=sm_61"); // Add your CUDA kernel source file build.file("kernel/kernel.cu"); // Compile to a static library for Rust to link against build.compile("kernel.a"); }
Additional Verification & Adjustment Tips
- Inspect the full command line: Run
cargo build -vvto see the complete nvcc command being executed. Verify that-cudart=sharedand-gencode arch=compute_61,code=sm_61appear as single, unbroken arguments. - Tweak optimization levels: If you need to override default settings (e.g., for Release builds), add
.opt_level(3)to the build chain to enable maximum optimization. - Confirm CUDA toolkit accessibility: Ensure
nvccis in your systemPATH, or set theCUDA_PATHenvironment variable if your toolkit is installed in a non-standard location.
Why This Works
- NVCC expects
-gencodeto be followed immediately by its architecture specification as part of the same argument. Splitting them makes nvcc interpret-gencodeas an incomplete option, triggering parsing errors that mess up subsequent parameters. - Keeping
-cudart=sharedas a single flag ensures nvcc correctly recognizes the CUDA runtime linkage mode without splitting the argument at the equals sign.
内容的提问来源于stack exchange,提问作者Ltei




