Compiling a C++ Program
CS2141 – Software Development using C/C++
Compiling a C++ Program g++ g++ is the GNU C++ compiler. A - - PowerPoint PPT Presentation
CS2141 Software Development using C/C++ Compiling a C++ Program g++ g++ is the GNU C++ compiler. A program in a file called hello.C: #include <iostream> using namespace std; int main( ) { cout << Hello world!
CS2141 – Software Development using C/C++
Compiling a C++ Program 2
#include <iostream> using namespace std; int main( ) { cout << “Hello world!” << endl; }
executable called a.out.
Compiling a C++ Program 3
> g++ -o hello hello.C > ./hello Hello world!
complains about things that the programmer did on purpose, which is why it is turned off by default. > g++ -g -Wall -o hello hello.C
Compiling a C++ Program 4
listed on one line and compiled together:
> g++ -o quack quack.C moo.C
changed is inefficient.
> g++ -c quack.C > g++ -c moo.C
Compiling a C++ Program 5
the executable:
> g++ -o quack quack.o moo.o
needs to be recompiled:
> g++ -c quack.C > g++ -o quack quack.o moo.o
be linked with other object files to form an executable program.
Compiling a C++ Program 6
hello.C
#include <iostream> using namespace std int main( ) { int i, n; cin >> n; for( i = 0; i < cout << “Hell cout << “From “ return 0; } .file "hello.C .local _ZSt8__i .comm _ZSt8__i .text .align 2 .globl main .type main, @f main: .LFB1512: pushl %ebp .LCFI0: movl %esp, %e .LCFI1: subl $24, %eshello.s
01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100hello.o a.out
Compiler Assembler Linker
# 1 "hello.C" # 1 "<built-in>" # 1 "<command line # 1 "hello.C" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/includehello.ii
Preprocessor
Compiling a C++ Program 7
(cpp is the C and C++ preprocessor)
hello.C
#include <iostream> using namespace std int main( ) { int i, n; cin >> n; for( i = 0; i < cout << “Hell cout << “From “ return 0; } .file "hello.C .local _ZSt8__i .comm _ZSt8__i .text .align 2 .globl main .type main, @f main: .LFB1512: pushl %ebp .LCFI0: movl %esp, %e .LCFI1: subl $24, %eshello.s
01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100hello.o a.out
Compiler Assembler Linker
# 1 "hello.C" # 1 "<built-in>" # 1 "<command line # 1 "hello.C" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/includehello.ii
Preprocessor
Compiling a C++ Program 8
definitions and function prototypes.
functions and class methods.
header files using the #include directive: #include <file.h> or #include “file.h”
Compiling a C++ Program 9
paste the contents of the included file at that spot.
header from being included more than once in the same compilation unit:
#ifndef FILENAME_H // If FILENAME_H is not defined ... #define FILENAME_H // ... then define FILENAME_H and ... ... // ... whatever else ... #endif // ... up until here.
Compiling a C++ Program 10
hello.h, hello.C, and main.C
for a couple of functions:
#ifndef HELLO_H #define HELLO_H void hello( ); void goodbye( int ); #endif
Compiling a C++ Program 11
#include <iostream> using namespace std; #include “hello.h” void hello( ) { cout << “Hello world!” << endl; } void goodbye( int n ) { for( int i = 0; i < n; ++i ) cout << “Goodbye!” << endl; }
Compiling a C++ Program 12
#include “hello.h” int main( ) { hello( ); goodbye( 28 ); return 0; }
Compiling a C++ Program 13
> g++ -c hello.C > g++ -c main.C > g++ -o hello hello.o main.o
compile the program, a makefile can be used
Makefile, the program can be compiled by simply
running make:
> make
Compiling a C++ Program 14
looking something like this:
target: dependencies (tab) command (tab) another command ... ...
Compiling a C++ Program 15
hello: hello.C hello.h main.C g++ -o hello hello.C main.C
> make > ./hello Hello world! Goodbye! ...
make to always recompile everything
Compiling a C++ Program 16
hello: hello.o main.o g++ -o hello hello.o main.o hello.o: hello.C hello.h g++ -c hello.C main.o: main.C hello.h g++ -c main.C
were changed
Compiling a C++ Program 17
removes all the object files and executables:
# ... other targets ... clean: rm -f hello main.o hello.o
argument to make:
> make clean
Compiling a C++ Program 18
Variables can eliminate some of the redundancy
VARIABLENAME = value
${VARIABLENAME}
Compiling a C++ Program 19
CXX = g++ CXXFLAGS = -g EXEC = hello OBJS = hello.o main.o # List of object files needed to # build the executable. ${EXEC}: ${OBJS} ${CXX} ${CXXFLAGS} -o ${EXEC} ${OBJS} hello.o: hello.C hello.h ${CXX} ${CXXFLAGS} -c hello.C main.o: main.C hello.h ${CXX} ${CXXFLAGS} -c main.C clean: rm -f ${EXEC} ${OBJS}
Compiling a C++ Program 20
a file *.s2, where s1 and s2 are suffixes.
rule:
.SUFFIXES: s1 s2 ... sn
Compiling a C++ Program 21
.SUFFIXES: .C .o CXX = g++ CXXFLAGS = -g EXEC = hello OBJS = hello.o main.o ${EXEC}: ${OBJS} ${CXX} ${CXXFLAGS} -o ${EXEC} ${OBJS} .C.o: # Abstract rule ${CXX} ${CXXFLAGS} -c $< # Still need to list the dependencies for object files hello.o: hello.C hello.h main.o: main.C hello.h clean: rm -f ${EXEC} ${OBJS}