﻿# NVCC separate compilation: device linking and host linking are different stages

Trace relocatable CUDA device code through compilation, nvlink, and final host linking when a device helper moves into another translation unit or static library.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-nvcc-relocatable-device-link-host-link/
Language: en

## Identify the cross-file dependency

The [NVIDIA separate-compilation article](https://developer.nvidia.com/blog/separate-compilation-linking-cuda-device-code/) explains how device functions can be organized across source files. Moving a helper out of a kernel's translation unit changes a build dependency, even if the function declaration and mathematical behavior remain the same. A host linker finding ordinary C++ symbols is not sufficient evidence that the corresponding device-side call graph has been resolved.

## Generate relocatable device code deliberately

The [NVCC manual](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html) distinguishes executable device code from relocatable device code embedded in host objects. It documents `--device-c` as compilation with relocatable device code enabled, and `--device-link` as the stage producing executable device code from participating inputs. An object suffix alone does not identify which kind of embedded GPU code was produced. Inspect the actual compilation options rather than assuming every object can participate in the same device-link process.

## Preserve the final host-link inputs

The device-link output still participates in final host linking, and the manual explains why objects containing device code can also carry implicit host registration code. Omitting an apparently device-only object from the host link can therefore break the final artifact. Keep both link stages and their object sets visible when integrating a CUDA extension into a larger build system. Suppressing an unresolved-symbol error does not replace the missing stage or establish a working executable.

## Use the build-system interface consistently

The [CMake property](https://cmake.org/cmake/help/latest/prop_tgt/CUDA_SEPARABLE_COMPILATION.html) enables separate compilation for CUDA files of a target. Consider a refactor that moves a helper into a static library: review which targets compile relocatable code and where device symbol resolution occurs, instead of adding unrelated linker flags until an error disappears. Build a small cross-file invocation as a fixture and verify that the final application actually calls the intended device helper under the selected target architecture.

## Record the complete artifact path

Keep source ownership, compiler options, embedded device-code mode, device-link inputs, host-link inputs, and target architectures. Guides to [CUDA version scopes](/journal/guide-linux-cuda-version-scopes/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) provide context. Check the final executable after build changes. A meaningful fix reproduces the intended two-stage linking contract rather than depending on one development machine's accidental object visibility or confusing host-side library loading with resolution of device-side function references.
