0Pricing
CUDA Academy · Lektion

Mit nvcc kompilieren

So trennt nvcc Host- und Device-Code.

Mit nvcc kompilieren ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Meet nvcc

The CUDA compiler is called nvcc. It takes your .cu file with mixed CPU and GPU code and turns it into one runnable program. 🛠️

Two Kinds of Code

A .cu file holds two worlds: host code that runs on the CPU and device code that runs on the GPU. nvcc must handle each one differently.

The Great Split

nvcc splits your source automatically. It hands the host portions to your normal C++ compiler and keeps the device portions for itself.

Compiling Device Code

For the GPU side, nvcc generates a special assembly called PTX, then turns it into machine code your specific GPU can execute.

The Host Compiler

nvcc does not compile CPU code itself. It quietly calls your system's host compiler, like g++ or clang, to handle the ordinary C++.

Your First Build Command

Compiling looks just like g++. Point nvcc at your .cu file and name the output with the -o flag.

nvcc hello.cu -o hello

Targeting an Architecture

The -arch flag tells nvcc which GPU generation to build for. Matching it to your card produces faster, fully optimized device code.

nvcc -arch=sm_80 hello.cu -o hello

Compute Capability

That sm number is a compute capability, a label for a GPU's feature set. sm_80 means an Ampere card; older cards use smaller numbers.

Fatbins for Many GPUs

nvcc can pack code for several architectures into one fatbinary, so the same executable runs well on different GPUs without rebuilding.

Familiar Optimization Flags

Many g++ habits carry over. nvcc accepts -O levels and -g for debugging, passing them along to the right compiler stage.

nvcc -O3 hello.cu -o hello

Linking CUDA Libraries

Need a tuned library like cuBLAS? You link it just as in g++, adding the library name with an -l flag on the nvcc command.

nvcc app.cu -lcublas -o app

Quick Check

Let us check how nvcc divides the work.

Recap

You learned that nvcc splits a .cu file, compiles device code to GPU machine code, and hands host code to g++. The -arch flag targets your card. 🎯

Häufig gestellte Fragen

Ist die Lektion „Mit nvcc kompilieren“ kostenlos?

Ja — der vollständige Text von „Mit nvcc kompilieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit nvcc kompilieren“?

So trennt nvcc Host- und Device-Code. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um CUDA Academy zu starten?

Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Mit nvcc kompilieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?

Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Versionen von Treiber, Runtime und Toolkit
  2. nvidia-smi wie ein Profi lesen
  3. Mit nvcc kompilieren
  4. Hallo GPU: Ihre erste .cu-Datei
← Zurück zu CUDA Academy