0% found this document useful (0 votes)
58 views

Tutorial 02: How To Build A C/C++ Program?: Part 2: Build Pipeline

This document discusses the build pipeline for C and C++ programs. It covers compilers like GCC and g++, build processes, C and C++ dialects, standard libraries, and header conventions.

Uploaded by

Parthapratim Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Tutorial 02: How To Build A C/C++ Program?: Part 2: Build Pipeline

This document discusses the build pipeline for C and C++ programs. It covers compilers like GCC and g++, build processes, C and C++ dialects, standard libraries, and header conventions.

Uploaded by

Parthapratim Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Tutorial T02

Partha Pratim
Das

Tutorial Recap
Programming in Modern C++
Objectives & Tutorial T02: How to build a C/C++ program?: Part 2: Build Pipeline
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC Partha Pratim Das
C/C++ Dialects
C Dialects
C++ Dialects Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Standard Library
C Std. Lib.
C++ Std. Lib.
[email protected]
std
Header Conventions
All url’s in this module have been accessed in September, 2021 and found to be functional
Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.1


Tutorial Recap

Tutorial T02

Partha Pratim • Understood the differences and relationships between source and header files
Das
• Understood how CPP can be harnessed to manage code during build
Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.2


Tutorial Objective

Tutorial T02

Partha Pratim • What is the build pipelines? Especially with reference to GCC
Das
• How to work with C/C++ dialects during build?
Tutorial Recap

Objectives &
• Understanding C/C++ Standard Libraries
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.3


Tutorial Outline

Tutorial T02
1 Tutorial Recap
Partha Pratim
Das
2 Build Pipeline
Tutorial Recap Compilers, IDE, and Debuggers
Objectives &
Outline
gcc and g++
Build Pipeline
Build with GCC
Compilers
gcc and g++ 3 C/C++ Dialects
Build with GCC
C Dialects
C/C++ Dialects
C Dialects
C++ Dialects
C++ Dialects

Standard Library
4 Standard Library
C Std. Lib. C Standard Library
C++ Std. Lib.
std C++ Standard Library
Header Conventions std
Tutorial Summary Header Conventions

5 Tutorial Summary
Programming in Modern C++ Partha Pratim Das T02.4
Build Pipeline

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Build Pipeline
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.5


Build Pipeline

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Source: GNU Compiler Collection, Wikiwand Accessed 13-Sep-21

Programming in Modern C++ Partha Pratim Das T02.6


Build Pipeline

Tutorial T02
• The C preprocessor (CPP) has the ability for the inclusion of header files, macro expansions,
Partha Pratim
Das
conditional compilation, and line control. It works on .c, .cpp, and .h files and produces .i
files
Tutorial Recap

Objectives &
• The Compiler translates the pre-processed C/C++ code into assembly language, which is a
Outline machine level code in text that contains instructions that manipulate the memory and
Build Pipeline processor directly. It works on .i files and produces .s files
Compilers
gcc and g++ • The Assembler translates the assembly program to binary machine language or object code. It
Build with GCC
works on .s files and produces .o files
C/C++ Dialects
C Dialects • The Linker links our program with the pre-compiled libraries for using their functions and
C++ Dialects
generates the executable binary. It works on .o (static library), .so (shared library or
Standard Library
C Std. Lib.
dynamically linked library), and .a (library archive) files and produces a.out file
C++ Std. Lib.
std File extensions mentioned here are for GCC running on Linux. These may vary on other OSs
Header Conventions
and for other compilers. Check the respective documentation for details. The build pipeline,
Tutorial Summary
however, would be the same.

Programming in Modern C++ Partha Pratim Das T02.7


Compilers

Tutorial T02 • The recommended compiler for the course is GCC, the GNU Compiler Collection - GNU
Partha Pratim
Project. To install it (with gdb, the debugger) on your system, follow:
Das
◦ Windows: How to install gdb in windows 10 on YoutTube
Tutorial Recap ◦ Linux: Usually comes bundled in Linux distribution. Check manual
Objectives &
Outline • You may also use online versions for quick tasks
Build Pipeline
Compilers
◦ GNU Online Compiler
gcc and g++ . From Language Drop-down, choose C (C99), C++ (C++11), C++14, or C++17
Build with GCC . To mark the language for gcc compilation, set -std=<compiler tag>
C/C++ Dialects
− Tags for C are: ansi, c89, c90, c11, c17, c18, etc.
C Dialects
C++ Dialects
− Tags for C++ are: ansi, c++98, c++03, c++11, c++14, c++17, c++20, etc.
Standard Library
− Check Options Controlling C Dialect and Language Standards Supported by GCC (Accessed
C Std. Lib. 13-Sep-21)
C++ Std. Lib.
std
◦ Code::Blocks is a free, open source cross-platform IDE that supports multiple compilers
Header Conventions including GCC, Clang and Visual C++
Tutorial Summary ◦ Programiz Online Compiler supports C18 and C++14
◦ OneCompiler supports C18 and C++17
• For a compiler, you must know the language version you are compiling for - check to confirm
Programming in Modern C++ Partha Pratim Das T02.8
What is GCC?

Tutorial T02

Partha Pratim • GCC stands for GNU Compiler Collections which is used to compile mainly C and
Das
C++ language
Tutorial Recap
• It can also be used to compile Objective C, Objective C++, Fortran, Ada, Go, and D
Objectives &
Outline
• The most important option required while compiling a source code file is the name of
Build Pipeline
Compilers
the source program, rest every argument is optional like a warning, debugging, linking
gcc and g++ libraries, object file, etc.
Build with GCC

C/C++ Dialects • The different options of GCC command allow the user to stop the compilation process
C Dialects
C++ Dialects
at different stages.
Standard Library • g++ command is a GNU C++ compiler invocation command, which is used for
C Std. Lib.
C++ Std. Lib.
preprocessing, compilation, assembly and linking of source code to generate an
std executable file. The different “options” of g++ command allow us to stop this process
Header Conventions

Tutorial Summary
at the intermediate stage.

Programming in Modern C++ Partha Pratim Das T02.9


What are the differences between gcc and g++?

Tutorial T02
g++ gcc
Partha Pratim
Das g++ is used to compile C++ program gcc is used to compile C program
g++ can compile any .c or .cpp files but they gcc can compile any .c or .cpp files but they will
Tutorial Recap
will be treated as C++ files only be treated as C and C++ respectively
Objectives &
Outline
Command to compile C++ program by g++ is: Command to compile C program by gcc is:
g++ fileName.cpp -o binary gcc fileName.c -o binary -lstdc++
Build Pipeline
Compilers
Using g++ to link the object files, files automat- gcc does not do this and we need to specify
gcc and g++ ically links in the std C++ libraries. -lstdc++ in the command line
Build with GCC g++ compiling .c/.cpp files has a few extra gcc compiling .c files has less predefined macros.
C/C++ Dialects macros gcc compiling .cpp files has a few extra macros
C Dialects
C++ Dialects
#define GXX WEAK 1
Standard Library
#define cplusplus 1
C Std. Lib.
C++ Std. Lib.
#define DEPRECATED 1
std #define GNUG 4
Header Conventions
#define EXCEPTIONS 1
Tutorial Summary #define private extern extern

Programming in Modern C++ Partha Pratim Das T02.10


Build with GCC: Options

Tutorial T02 [1] Place the source (.c) and header (.h) files in current directory
11-09-2021 10:46 157 fact.c
Partha Pratim
Das
11-09-2021 10:47 124 fact.h
11-09-2021 10:47 263 main.c
Tutorial Recap [2] Compile source files (.c) and generate object (.o) files using option “-c”. Note additions of files to directory
Objectives & $ gcc -c fact.c
Outline $ gcc -c main.c
Build Pipeline
Compilers
11-09-2021 11:02 670 fact.o
gcc and g++
11-09-2021 11:02 1,004 main.o
Build with GCC [3] Link object (.o) files and generate executable (.exe) file of preferred name (fact) using option “-o”. Note added
C/C++ Dialects
file to directory
C Dialects $ gcc fact.o main.o -o fact
C++ Dialects

Standard Library
11-09-2021 11:03 42,729 fact.exe
C Std. Lib. [4] Execute
C++ Std. Lib. $ fact
std Input n
Header Conventions
5
Tutorial Summary fact(5) = 120
[5] We can combine steps [2] and [3] to generate executable directly by compiling and linking source files in one
command
$ gcc fact.c main.c -o fact
Programming in Modern C++ Partha Pratim Das T02.11
Build with GCC: Options

Tutorial T02 [6] We can only compile and generate assembly language (.s) file using option “-S”
Partha Pratim $ gcc -S fact.c main.c
Das
11-09-2021 11:34 519 fact.s
Tutorial Recap 11-09-2021 11:34 1,023 main.s
Objectives & [7] To stop after prepossessing use option “-E”. The output is generated in stdout (redirected here to cppout.c).
Outline
$ gcc -E fact.c main.c >cppout.c
Build Pipeline
Compilers
11-09-2021 11:32 21,155 cppout.c
gcc and g++
Build with GCC Note that CPP:
C/C++ Dialects • Produces a single file containing the source from all .c files
C Dialects • Includes all required header files (like fact.h, stdio.h) and strips off unnecessary codes present there
C++ Dialects • Strips off all comments
Standard Library
• Textually replaces all manifest constants and expands all macros
C Std. Lib. [8] We can know the version of the compiler
C++ Std. Lib.
std
$ gcc --version
Header Conventions
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Tutorial Summary
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Programming in Modern C++ Partha Pratim Das T02.12


Build with GCC: Options

Tutorial T02

Partha Pratim
[9] When we intend to debug our code with gdb we need to use “-g” option to tell GCC to emit extra information for
Das
use by a debugger
$ gcc -g fact.c main.c -o fact
Tutorial Recap

Objectives &
Outline
[10] We should always compile keeping it clean of all warnings. This can be done by “-Wall” flag. For example if we
comment out f = fact(n); and try to build we get warning, w/o “-Wall”, it is silent
Build Pipeline
$ gcc -Wall main.c
Compilers
gcc and g++
Build with GCC
main.c: In function ’main’:
main.c:14:5: warning: ’f’ is used uninitialized in this function [-Wuninitialized]
C/C++ Dialects
printf("fact(%d) = %d\n", n, f);
C Dialects
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C++ Dialects

Standard Library $ gcc main.c


C Std. Lib.
C++ Std. Lib.
std With “-Werror”, all warnings are treated as errors and no output will be produced
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.13


Build with GCC: Options

Tutorial T02

Partha Pratim
[11] We can trace the commands being used by the compiler using option “-v”, that is, verbose mode
Das $ gcc -v fact.c main.c -o fact

Tutorial Recap Using built-in specs.


Objectives &
COLLECT_GCC=gcc
Outline COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Build Pipeline
Compilers
[truncated]
gcc and g++
Thread model: win32
Build with GCC gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
[truncated]
C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.14


Build with GCC: Summary of Options and Extensions
• gcc options and file extensions. Note that .c is shown as a placeholder for user provided source files. A detailed
Tutorial T02
list of source file extensions are given in the next point
Partha Pratim
Das Option Behaviour Input Output
Extension Extension
Tutorial Recap
-c Compile or assemble the source files, but do not link .c, .s, .i .o
Objectives & -S Stop after the stage of compilation proper; do not assemble .c, .i .s
Outline
-E Stop after the preprocessing stage .c To stdout
Build Pipeline -o file Place the primary output in file file (a.out w/o -o) .c, .s, .i Default for OS
Compilers -v Print the commands executed to run the stages of compilation .c, .s, .i To stdout
gcc and g++
Build with GCC

C/C++ Dialects
• Source file (user provided) extensions
C Dialects
C++ Dialects
Extension File Type Extension File Type
Standard Library .c C source code that .cpp, .cc, .cp, .cxx C++ source code that
C Std. Lib. must be preprocessed .CPP, .c++, .C must be preprocessed
C++ Std. Lib. .h C / C++ header file .H, .hp, .hxx, .hpp C++ header file
std .HPP, .h++, .tcc
Header Conventions .s Assembler code .S, .sx Assembler code that
Tutorial Summary must be preprocessed
* Varied extensions for C++ happened during its evolution due various adoption practices
* We are going to follow the extensions marked in red
Source: 3.1 Option Summary and 3.2 Options Controlling the Kind of Output Accessed 13-Sep-21

Programming in Modern C++ Partha Pratim Das T02.15


C / C++ Dialects

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
C / C++ Dialects
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.16


C Dialects

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Latest Version as of Sep-21: C18: ISO/IEC 9899:2018, 2018


Programming in Modern C++ Partha Pratim Das T02.17
C Dialects: Checking for a dialect

Tutorial T02 • We check the language version (dialect) of C being used by GCC in compilation using the following code
Partha Pratim /* File Check C Version.c */
Das #include <stdio.h>
int main() {
Tutorial Recap if (__STDC_VERSION__ == 201710L) printf("C18\n"); /* C11 with bug fixes */
Objectives & else if (__STDC_VERSION__ == 201112L) printf("C11\n");
Outline else if (__STDC_VERSION__ == 199901L) printf("C99\n");
Build Pipeline
else if (__STDC_VERSION__ == 199409L) printf("C89\n");
Compilers
else printf("Unrecognized version of C\n");
gcc and g++
Build with GCC return 0;
}
C/C++ Dialects
C Dialects • We can ask GCC to use a specific dialect by using -std flag and check with the above code for three cases
C++ Dialects
$ gcc -std=c99 "Check C Version.c"
Standard Library C99
C Std. Lib.
C++ Std. Lib. $ gcc "Check C Version.c"
std C11
Header Conventions

Tutorial Summary $ gcc -std=c11 "Check C Version.c"


C11

Default for this gcc is C11


Programming in Modern C++ Partha Pratim Das T02.18
C++ Standards

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Fixes on C++98: C++03: ISO/IEC 14882:2003, 2003


Latest Version as of Sep-21: C++20: ISO/IEC 14882:2020, 2020
Programming in Modern C++ Partha Pratim Das T02.19
C++ Dialects: Checking for a dialect

Tutorial T02
• We check the language version (dialect) of C++ being used by GCC in compilation using the following code
// File Check C++ Version.cpp
Partha Pratim
Das
#include <iostream>
int main() {
Tutorial Recap if (__cplusplus == 201703L) std::cout << "C++17\n";
else if (__cplusplus == 201402L) std::cout << "C++14\n";
Objectives &
Outline
else if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
Build Pipeline else std::cout << "Unrecognized version of C++\n";
Compilers
return 0;
gcc and g++
}
Build with GCC

C/C++ Dialects
• We can ask GCC to use a specific dialect by using -std flag and check with the above code for four cases
C Dialects $ g++ -std=gnu++98 "Check C++ Version.cpp"
C++ Dialects C++98
Standard Library
C Std. Lib.
$ g++ -std=c++11 "Check C++ Version.cpp"
C++ Std. Lib.
C++11
std
Header Conventions $ g++ -std=c++14 "Check C++ Version.cpp"
Tutorial Summary
C++14

$ g++ "Check C++ Version.cpp"


C++14
Default for this g++ is C++14
Programming in Modern C++ Partha Pratim Das T02.20
Standard Library

Tutorial T02

Partha Pratim
Das

Tutorial Recap

Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Standard Library
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.21


What is Standard Library?

Tutorial T02 • A standard library in programming is the library made available across implementations of a
Partha Pratim language
Das
• These libraries are usually described in language specifications (C/C++); however, they may
Tutorial Recap also be determined (in part or whole) by informal practices of a language’s community (Python)
Objectives & • A language’s standard library is often treated as part of the language by its users, although the
Outline
designers may have treated it as a separate entity
Build Pipeline
Compilers
• Many language specifications define a core set that must be made available in all
gcc and g++ implementations, in addition to other portions which may be optionally implemented
Build with GCC
• The line between a language and its libraries therefore differs from language to language
C/C++ Dialects
C Dialects
• Bjarne Stroustrup, designer of C++, writes:
C++ Dialects What ought to be in the standard C++ library? One ideal is for a programmer to be able to find every
Standard Library interesting, significant, and reasonably general class, function, template, etc., in a library. However, the question
C Std. Lib.
here is not, ”What ought to be in some library?” but ”What ought to be in the standard library?” The answer
C++ Std. Lib. ”Everything!” is a reasonable first approximation to an answer to the former question but not the latter. A
std standard library is something every implementer must supply so that every programmer can rely on it.
Header Conventions
• This suggests a relatively small standard library, containing only the constructs that “every
Tutorial Summary
programmer” might reasonably require when building a large collection of software
• This is the philosophy that is used in the C and C++ standard libraries
Source: Standard library, Wiki Accessed 13-Sep-21
Programming in Modern C++ Partha Pratim Das T02.22
C Standard Library: Common Library Components

Tutorial T02 Component Data Types, Manifest Constants, Macros, Functions, ...
Partha Pratim stdio.h Formatted and un-formatted file input and output including functions
Das
• printf, scanf, fprintf, fscanf, sprintf, sscanf, feof, etc.
Tutorial Recap
stdlib.h Memory allocation, process control, conversions, pseudo-random numbers, search-
Objectives &
Outline
ing, sorting
Build Pipeline
• malloc, free, exit, abort, atoi, strtold, rand, bsearch, qsort, etc.
Compilers string.h Manipulation of C strings and arrays
gcc and g++
Build with GCC
• strcat, strcpy, strcmp, strlen, strtok, memcpy, memmove, etc.
C/C++ Dialects math.h Common mathematical operations and transformations
C Dialects • cos, sin, tan, acos, asin, atan, exp, log, pow, sqrt, etc.
C++ Dialects
errno.h Macros for reporting and retrieving error conditions through error codes stored in
Standard Library
C Std. Lib. a static memory location called errno
C++ Std. Lib. • EDOM (parameter outside a function’s domain – sqrt(-1)),
std
Header Conventions • ERANGE (result outside a function’s range), or
Tutorial Summary • EILSEQ (an illegal byte sequence), etc.
A header file typically contains manifest constants, macros, necessary struct / union types,
typedef’s, function prototype, etc.
Programming in Modern C++ Partha Pratim Das T02.23
C Standard Library: math.h
/* math.h
Tutorial T02
* This file has no copyright assigned and is placed in the Public Domain.
Partha Pratim * This file is a part of the mingw-runtime package.
Das * Mathematical functions.
*/
Tutorial Recap
#ifndef _MATH_H_
Objectives & #define _MATH_H_
Outline #ifndef __STRICT_ANSI__ // conditional exclusions for ANSI
Build Pipeline // ...
Compilers #define M_PI 3.14159265358979323846 // manifest constant for pi
gcc and g++ // ...
Build with GCC struct _complex { // struct of _complex type
C/C++ Dialects double x; /* Real part */
C Dialects double y; /* Imaginary part */
C++ Dialects };
Standard Library
_CRTIMP double __cdecl _cabs (struct _complex); // cabs(.) function header
C Std. Lib.
// ...
C++ Std. Lib.
#endif /* __STRICT_ANSI__ */
std // ...
Header Conventions _CRTIMP double __cdecl sqrt (double); // sqrt(.) function header
Tutorial Summary
// ...
#define isfinite(x) ((fpclassify(x) & FP_NAN) == 0) // macro isfinite(.) to check if a number is finite
// ...
#endif /* _MATH_H_ */
Source: C math.h library functions Accessed 13-Sep-21
Programming in Modern C++ Partha Pratim Das T02.24
C++ Standard Library: Common Library Components

Tutorial T02 Component Data Types, Manifest Constants, Macros, Functions, Classes, ...
Partha Pratim iostream Stream input and output for standard I/O
Das
• cout, cin, endl, ..., etc.
Tutorial Recap
string Manipulation of string objects
Objectives &
Outline
• Relational operators, IO operators, Iterators, etc.
Build Pipeline
memory High-level memory management
Compilers • Pointers: unique ptr, shared ptr, weak ptr, auto ptr, & allocator etc.
gcc and g++
Build with GCC
exception Generic Error Handling • exception, bad exception, unexpected handler,
C/C++ Dialects terminate handler, etc.
C Dialects stdexcept Standard Error Handling • logic error, invalid argument, domain error,
C++ Dialects
length error, out of range, runtime error, range error, overflow error,
Standard Library
C Std. Lib.
underflow error, etc.
C++ Std. Lib. Adopted from C Standard Library
std
Header Conventions cmath Common mathematical operations and transformations
Tutorial Summary • cos, sin, tan, acos, asin, atan, exp, log, pow, sqrt, etc.
cstdlib Memory alloc., process control, conversions, pseudo-rand nos., searching, sorting
• malloc, free, exit, abort, atoi, strtold, rand, bsearch, qsort, etc.
Programming in Modern C++ Partha Pratim Das T02.25
namespace std for C++ Standard Library

Tutorial T02
C Standard Library C++ Standard Library
Partha Pratim
Das
• All names are global • All names are within std namespace
Tutorial Recap • stdout, stdin, printf, scanf • std::cout, std::cin
Objectives & • Use using namespace std;
Outline

Build Pipeline to get rid of writing std:: for every standard


Compilers
library name
gcc and g++
Build with GCC
W/o using W/ using
C/C++ Dialects #include <iostream> #include <iostream>
C Dialects using namespace std;
C++ Dialects

Standard Library
int main() { int main() {
C Std. Lib.
C++ Std. Lib.
std std::cout << "Hello World in C++" cout << "Hello World in C++"
Header Conventions
<< std::endl; << endl;
Tutorial Summary

return 0; return 0;
} }

Programming in Modern C++ Partha Pratim Das T02.26


Standard Library: C/C++ Header Conventions

Tutorial T02 C Header C++ Header


Partha Pratim C Program Use .h. Example: #include <stdio.h> Not applicable
Das Names in global namespace
Tutorial Recap
C++ Program Prefix c, no .h. Example: #include <cstdio> No .h. Example:
Names in std namespace #include <iostream>
Objectives &
Outline

Build Pipeline
• A C std. library header is used in C++ with prefix ’c’ and without the .h. These are in std namespace:
Compilers
#include <cmath> // In C it is <math.h>
gcc and g++
Build with GCC
...
C/C++ Dialects
std::sqrt(5.0); // Use with std::
C Dialects
C++ Dialects
It is possible that a C++ program include a C header as in C. Like:
Standard Library #include <math.h> // Not in std namespace
C Std. Lib. ...
C++ Std. Lib.
std
sqrt(5.0); // Use without std::
Header Conventions
This, however, is not preferred
Tutorial Summary
• Using .h with C++ header files, like iostream.h, is disastrous. These are deprecated. It is
dangerous, yet true, that some compilers do not error out on such use. Exercise caution.

Programming in Modern C++ Partha Pratim Das T02.27


Tutorial Summary

Tutorial T02

Partha Pratim • Understood the overall build process for a C/C++ project with specific reference to the
Das
build pipeline of GCC
Tutorial Recap
• Understood the management of C/C++ dialects and C/C++ Standard Libraries
Objectives &
Outline

Build Pipeline
Compilers
gcc and g++
Build with GCC

C/C++ Dialects
C Dialects
C++ Dialects

Standard Library
C Std. Lib.
C++ Std. Lib.
std
Header Conventions

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T02.28

You might also like