Compiling C and C++ Programs (2024)

gcc is the "GNU" C Compiler, and g++ is the "GNUC++ compiler, while cc and CC are the Sun C and C++ compilers also available on Sun workstations. Below are several examples that showhow to use g++ to compile C++ programs, although much of the informationapplies to C programs as well as compiling with the other compilers.

Example 1: Compiling a simple program

Consider the following example: Let "hello.C" be a file that contains the following C++ code.

 #include "iostream.h" int main() { cout << "Hello\n"; } 

The standard way to compile this program is with the command

 g++ hello.C -o hello 

This command compiles hello.C into an executable program named "hello" thatyou run by typing 'hello' at the command line. It does nothing more thanprint the word "hello" on the screen.

Alternatively, the above program could be compiled using the followingtwo commands.

 g++ -c hello.C g++ hello.o -o hello 

The end result is the same, but this two-step method first compiles hello.Cinto a machine code file named "hello.o" and then links hello.owith some system libraries to produce the final program "hello".In fact the first method also does this two-stage process of compilingand linking, but the stages are done transparently, and the intermediatefile "hello.o" is deleted in the process.

Frequently used compilation options

C and C++ compilers allow for many options for how to compile a program,and the examples below demonstrate how to use many of the more commonlyused options. In each example, "myprog.C" contains C++ sourcecode for the executable "myprog". In most cases options canbe combined, although it is generally not useful to use "debugging" and "optimization" optionstogether.

Compile myprog.C so that myprog contains symbolicinformation that enables it to be debugged with the gdb debugger.
g++ -g myprog.C -o myprog
Have the compiler generate many warnings about syntacticallycorrect but questionable looking code. It is good practice to always usethis option with gcc and g++.
g++ -Wall myprog.C -o myprog
Generate symbolic information for gdb and many warning messages.
g++ -g -Wall myprog.C -o myprog
Generate optimized code on a Solaris machine with warnings. The -Ois a capital o and not the number 0!
g++ -Wall -O -mv8 myprog.C -o myprog
Generate optimized code on a Solaris machine using Sun's own CC compiler.This code will generally be faster than g++ optimized code.
CC -fast myprog.C -o myprog
Generate optimized code on a Linux machine.
g++ -O myprog.C -o myprog
Compile myprog.C when it contains Xlib graphics routines.
g++ myprog.C -o myprog -lX11

If "myprog.c" is a C program, then the above commands will all work byreplacing g++ with gcc and "myprog.C" with "myprog.c". Beloware a few examples that apply only to C programs.

Compile a C program that uses math functions suchas "sqrt".
gcc myprog.C -o myprog -lm
Compile a C program with the "electric fence" library. Thislibrary, available on all the Linux machines, causes many incorrectlywritten programs to crash as soon as an error occurs. It is useful fordebugging as the error location can be quickly determined using gdb.However, it should only be used for debugging as the executable myprogwill be much slower and use much more memory than usual.
gcc -g myprog.C -o myprog -lefence

Example 2: Compiling a program with multiple sourcefiles

If the source code is in several files, say "file1.C" and "file2.C",then they can be compiled into an executable program named "myprog" usingthe following command:

 g++ file1.C file2.C -o myprog 

The same result can be achieved using the following three commands:

 g++ -c file1.C g++ -c file2.C g++ file1.o file2.o -o myprog 

The advantage of the second method is that it compiles each of the sourcefiles separately. If, for instance, the above commands were used to create "myprog",and "file1.C" was subsequently modified, then the followingcommands would correctly update "myprog".

 g++ -c file1.C g++ file1.o file2.o -o myprog 

Note that file2.C does not need to be recompiled, so the time requiredto rebuild myprog is shorter than if the first method for compiling myprogwere used. When there are numerous source file, and a change is only madeto one of them, the time savings can be significant. This process, thoughsomewhat complicated, is generally handled automatically by a makefile.

Compiling C and C++ Programs (2024)
Top Articles
Better Buy: Vanguard High Dividend Yield ETF vs. Schwab U.S. Dividend Equity ETF | The Motley Fool
Average Credit Analyst Salary - Overview, Salaries by Position
Navicent Human Resources Phone Number
Pollen Count Centreville Va
Bleak Faith: Forsaken – im Test (PS5)
NYT Mini Crossword today: puzzle answers for Tuesday, September 17 | Digital Trends
It may surround a charged particle Crossword Clue
Atvs For Sale By Owner Craigslist
Santa Clara College Confidential
Emmalangevin Fanhouse Leak
Produzione mondiale di vino
Graveguard Set Bloodborne
Busted Newspaper S Randolph County Dirt The Press As Pawns
Echo & the Bunnymen - Lips Like Sugar Lyrics
Eva Mastromatteo Erie Pa
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
3S Bivy Cover 2D Gen
The Grand Canyon main water line has broken dozens of times. Why is it getting a major fix only now?
Gayla Glenn Harris County Texas Update
All Breed Database
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Jc Green Obits
Hdmovie2 Sbs
Cpt 90677 Reimbursem*nt 2023
What Equals 16
Keyn Car Shows
Miles City Montana Craigslist
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Imagetrend Elite Delaware
Ugly Daughter From Grown Ups
Mark Ronchetti Daughters
Eero Optimize For Conferencing And Gaming
Jambus - Definition, Beispiele, Merkmale, Wirkung
Save on Games, Flamingo, Toys Games & Novelties
American Bully Xxl Black Panther
#1 | Rottweiler Puppies For Sale In New York | Uptown
KM to M (Kilometer to Meter) Converter, 1 km is 1000 m
Game8 Silver Wolf
Joey Gentile Lpsg
Cookie Clicker The Advanced Method
Wasmo Link Telegram
Sams Gas Price Sanford Fl
Jamesbonchai
Yale College Confidential 2027
Studentvue Calexico
Rescare Training Online
How to Connect Jabra Earbuds to an iPhone | Decortweaks
City Of Irving Tx Jail In-Custody List
Tito Jackson, member of beloved pop group the Jackson 5, dies at 70
Horseneck Beach State Reservation Water Temperature
Morbid Ash And Annie Drew
De Donde Es El Area +63
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5966

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.