nvcc: Compile CUDA Code in CI
nvcc compiles CUDA C++ source (.cu) by splitting device and host code, compiling device code itself and handing host code to a supported host compiler.
nvcc is the entry point for building CUDA. It orchestrates the device compiler and your host compiler (gcc/clang/cl), so most build failures are really host-compiler mismatches.
What it does
nvcc parses a .cu file, compiles the device (GPU) portions to PTX/cubin, compiles or forwards the host portions to the system host compiler, and links them into an object or executable. It accepts many gcc-style flags directly.
Common usage
nvcc kernel.cu -o kernel
# compile to an object only
nvcc -c kernel.cu -o kernel.o
# add include/library paths and pass a flag to the host compiler
nvcc kernel.cu -I./include -L./lib -lmylib \
-Xcompiler "-fPIC" -o kernelOptions
| Flag | What it does |
|---|---|
| -o <file> | Output file name |
| -c | Compile to object, do not link |
| -arch=sm_XX | Target GPU compute capability |
| -I <dir> / -L <dir> / -l<lib> | Include path, library path, link a library |
| -Xcompiler <arg> | Pass an argument straight to the host compiler |
| -ccbin <path> | Choose a specific host compiler |
In CI
Pin the host compiler with -ccbin when the runner ships a gcc newer than the toolkit supports. Compile a tiny .cu at the start of the job to prove the toolchain works before building the whole project.
Common errors in CI
"#error -- unsupported GNU version! gcc versions later than N are not supported!" means the host gcc is newer than the toolkit allows; use -ccbin to point at an older gcc. "cuda_runtime.h: No such file or directory" means the CUDA include path is missing from the environment. "cannot find -lcudart" means the CUDA lib64 directory is not on the library path.