Compile com nvcc
Veja como nvcc separa o código do hospedeiro e do dispositivo.
Compile com nvcc é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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 helloTargeting 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 helloCompute 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 helloLinking 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 appQuick 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. 🎯
Perguntas Frequentes
A aula “Compile com nvcc” é grátis?
Sim — o texto completo de “Compile com nvcc” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.
O que vou aprender em “Compile com nvcc”?
Veja como nvcc separa o código do hospedeiro e do dispositivo. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar CUDA Academy?
Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Compile com nvcc”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de CUDA Academy?
Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Versões do controlador, ambiente de execução e kit de ferramentas
- Leia nvidia-smi como um profissional
- Compile com nvcc
- Olá, GPU: seu primeiro arquivo .cu