0Pricing
CUDA Academy · Lesson

Compiling with nvcc

How nvcc splits host and device code.

Compiling with nvcc is a free CUDA Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎯

Frequently asked questions

Is the “Compiling with nvcc” lesson free?

Yes — the full text of “Compiling with nvcc” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “Compiling with nvcc”?

How nvcc splits host and device code. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Compiling with nvcc” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Driver, Runtime, and Toolkit Versions
  2. Reading nvidia-smi Like a Pro
  3. Compiling with nvcc
  4. Hello GPU: Your First .cu File
← Back to CUDA Academy