<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C Archives - ITEC4B</title>
	<atom:link href="https://itec4b.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>https://itec4b.com/tag/c/</link>
	<description>Information Technology Expert Consulting</description>
	<lastBuildDate>Thu, 02 Feb 2023 12:19:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
	<item>
		<title>Debian: C/C++ Development Environment</title>
		<link>https://itec4b.com/debian-c-c-development-environment/</link>
		
		<dc:creator><![CDATA[author]]></dc:creator>
		<pubDate>Wed, 01 Feb 2023 17:59:40 +0000</pubDate>
				<category><![CDATA[Debian]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>
		<guid isPermaLink="false">https://itec4b.com/?p=919</guid>

					<description><![CDATA[The GNU C Library (glibc) Reference ManualThe GNU C++ LibraryGCC online documentationGNU Make Manual ISO C programming language standardISO C++ Standards Package: build-essentialThis package is normally required for building Debian packages, it is OK to install it since it includes all what is necessary to get a C/C++ Development Environment. Package: gcc-docDocumentation for the GNU &#8230; <p class="link-more"><a href="https://itec4b.com/debian-c-c-development-environment/" class="more-link">Read more<span class="screen-reader-text"> "Debian: C/C++ Development Environment"</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p><a href="https://www.gnu.org/software/libc/manual">The GNU C Library (glibc) Reference Manual</a><br><a href="https://gcc.gnu.org/onlinedocs/libstdc++">The GNU C++ Library</a><br><a href="https://gcc.gnu.org/onlinedocs">GCC online documentation</a><br><a href="https://www.gnu.org/software/make/manual">GNU Make Manual</a><br><br><a href="https://www.open-std.org/JTC1/SC22/WG14/www/projects#9899">ISO C programming language standard</a><br><a href="https://www.open-std.org/JTC1/SC22/WG21/docs/standards">ISO C++ Standards</a></p>



<p><br><span style="text-decoration: underline;">Package: <strong>build-essential</strong></span><br>This package is normally required for building Debian packages, it is OK to install it since it includes all what is necessary to get a C/C++ Development Environment.</p>



<pre class="wp-block-code"><code># apt install build-essential</code></pre>



<p><span style="text-decoration: underline;">Package: <strong>gcc-doc</strong></span><br>Documentation for the GNU compilers (gcc, gobjc, g++)</p>



<pre class="wp-block-code"><code># apt install gcc-doc</code></pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p><span style="text-decoration: underline;">Virtual Package: <strong>libc-dev</strong></span><br>Package: <strong>libc<span style="text-decoration: underline;">&lt;version&gt;</span>-dev</strong><br>GNU C Library: Development Libraries and Header Files<br>Contains the symlinks, headers, and object files needed to compile and link programs which use the standard C library<br><br><span style="text-decoration: underline;">Package: <strong>libstdc++-&lt;version&gt;-dev</strong></span><br>GNU Standard C++ Library v3 (development files)<br>This package contains the headers and static library files necessary for building C++ programs which use libstdc++.<br><br><span style="text-decoration: underline;">Package: <strong>libstdc++-&lt;version&gt;-doc</strong></span><br>GNU Standard C++ Library v3 (documentation files)<br>This package contains documentation files for the GNU stdc++ library.<br><br><span style="text-decoration: underline;">Package: <strong>gcc</strong></span><br>GNU C compiler<br>This is the GNU C compiler, a fairly portable optimizing compiler for C.<br><br><span style="text-decoration: underline;">Package: <strong>g++</strong></span><br>GNU C++ compiler<br>This is the GNU C++ compiler, a fairly portable optimizing compiler for C++.</p>



<p><span style="text-decoration: underline;">Package: <strong>make</strong></span><br>Utility for directing compilation<br>GNU make is a utility which controls the generation of executables and other target files of a program from the program&#8217;s source files.<br>It determines automatically which pieces of a large program need to be (re)created, and issues the commands to (re)create them.<br>make can be used to organize any task in which targets (files) are to be automatically updated based on input files whenever the corresponding input is newer. It is not limited to building computer programs. Indeed, make is a general purpose dependency solver.<br><br><span style="text-decoration: underline;">Package: <strong>manpages-dev</strong></span><br>Manual pages about using GNU/Linux for development<br>These man pages describe the Linux programming interface, including these two sections:<br>2 = Linux system calls.<br>3 = Library calls (note that a more comprehensive source of information may be found in the glibc-doc and glibc-doc-reference packages).<br><br><span style="text-decoration: underline;">Package: <strong>ccache</strong></span><br>Compiler cache for fast recompilation of C/C++ code<br>It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>C/C++ Compiler Operations</title>
		<link>https://itec4b.com/c-compiler-operations/</link>
		
		<dc:creator><![CDATA[author]]></dc:creator>
		<pubDate>Mon, 30 Jan 2023 13:36:52 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>
		<guid isPermaLink="false">https://itec4b.com/?p=643</guid>

					<description><![CDATA[Sources : Delroy A. Brinkerhoff : Object-Oriented Programming using C++Brian Gough, Richard M. Stallman : An Introduction to GCC The process of translating source code into an executable program is called &#8220;compiling the program&#8221; or just &#8220;compiling&#8221;.We usually view the compilation process as a single action and generally refer to it as such.Nevertheless, a modern &#8230; <p class="link-more"><a href="https://itec4b.com/c-compiler-operations/" class="more-link">Read more<span class="screen-reader-text"> "C/C++ Compiler Operations"</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p>Sources : <br><span style="text-decoration: underline;">Delroy A. Brinkerhoff : Object-Oriented Programming using C++</span><br><span style="text-decoration: underline;">Brian Gough, Richard M. Stallman : An Introduction to GCC</span></p>



<p><br><span style="text-decoration: underline;"><strong>The process of translating source code into an executable program is called &#8220;compiling the program&#8221; or just &#8220;compiling&#8221;</strong></span>.<br>We usually view the compilation process as a single action and generally refer to it as such.<br>Nevertheless, a <span style="text-decoration: underline;"><strong>modern compiler actually consists of 4 separate programs</strong></span>:</p>



<pre class="wp-block-code"><code>- <strong>Preprocessor</strong>
  Expand macros and included header files

- <strong>Compiler</strong>
  Convert source code to assembly language

- <strong>Assembler</strong>
  Convert assembly language to machine code

- <strong>Linker</strong>
  Link object files and binary libraries, Create the final executable</code></pre>



<p>So here is the process :</p>



<p class="has-text-color" style="color:#0a58ca"><span style="text-decoration: underline;"><strong>Source Code &gt; Preprocessor &gt; Compiler &gt; Assembler &gt; Linker &gt; Executable Program</strong></span></p>



<p>A single program usually consist of multiple source code files.<br>It is both awkward and inconvenient to deal with large programs in a single source code file, and spreading them over multiple files has many advantages:</p>



<p>1. It breaks large, complex programs into smaller, independent conceptual units<br>Easier to understand, follow and maintain.<br><br>2. It allows multiple programmers to work on a single program at the same time<br>Each programmer works on a separate set of files.<br><br>3. It may speed up compilation (depending on the compiler system options used)<br>The compiler system stores the generated machine code in an object file, one object file for each source code file. The compiler system may not delete the object files, so if the source code file is unchanged, the linker uses the existing object code file.<br><br>4. It permits related programs to share files<br>For example, office suites often include a word processor, a slide show editor, and a spreadsheet.<br>By maintaining the User Interface code in one shared file, they can present a consistent User Interface.<br><br>5. Although less important, it allows software developers to market software as object code organized as (binary black box) libraries, which is useful when supplying code that interfaces with applications.</p>



<p></p>



<h2>Preprocessor</h2>



<p><strong><span style="text-decoration: underline;">The Preprocessor takes the source code, removes the comments, includes headers, and replaces macros</span></strong>.<br><br>The preprocessor handles statements or lines of code that begin with the &#8220;#&#8221; character, which are called &#8220;<strong>preprocessor directives</strong>&#8220;.<br><br>Note that directives are not C/C++ statements (and therefore do not end with a semicolon) but rather instruct the preprocessor to carry out some action.<br><br><span style="text-decoration: underline;">For each .c/.cpp file, the preprocessor handles directives that begin with the # character and creates a temporary file to store its output</span>.<br>The preprocessor reads and processes each file one at a time from top to bottom.<br><span style="text-decoration: underline;"><strong>It does not change the content of any of the source files it processes</strong></span>.<br><br><span style="text-decoration: underline;">The results are files which contains the source code merged with headers files and with all macros expanded</span>.<br>By convention, preprocessed files are given the file extension .i for C programs and .ii for C++ programs.<br>In practice, the preprocessed file is not saved to disk unless the <code>-save-temps</code> option is used.</p>



<p>Two of the most common directives, and the first that we will use, are <strong>#include</strong> and <strong>#define</strong>.</p>



<p><strong><span style="text-decoration: underline;">The #include Directive</span></strong><br><br><span style="text-decoration: underline;">When the preprocessor encounters the #include directive, it opens the <strong>header file</strong> and adds its contents into the temporary file</span>.<br>The symbols surrounding the name of the header file are important and determine where the preprocessor looks for the file.</p>



<p><code>#include &lt;name&gt;</code><br>The angle brackets denote a system include file that is part of the compiler itself (think of it as &#8220;library&#8221; code)<br>and directs the preprocessor to search for the file where the system header files are located (which varies from one compiler to another and from one Operating System to another).<br><br><code>#include "name.h"</code><br>The double quotation marks identify a header file that is written as a part of a program.<br>The quotation marks instruct the preprocessor to look for the header file in the current directory (i.e., in the same directory as the source code).<br>Header files that a programmer writes as part of an application program typically end with a .h extension.<br><br>You might see two kinds of system header files in a C++ program :<br>Older system header files end with a &#8220;.h&#8221; extension: &lt;name.h&gt;.<br>These header files were originally created for C programs, but may also be used with C++.<br>Newer system header files do not end with an extension: &lt;name&gt;, may only be used with C++.<br><br>File names appearing between &lt; and &gt; refer to system header files<br>File names appearing between an opening and closing &#8221; refer to header files written by the programmer as a part of the program.<br><br><span style="text-decoration: underline;">Note</span>:<br>The include directive does not end with a semicolon and there must be at least one space between the directive and the file name.</p>



<p></p>



<p><strong><span style="text-decoration: underline;">The #define Directive and Symbolic Constants</span></strong></p>



<p>The #define directive introduces a programming construct called a <strong>macro</strong>.<br>A simple macro only replaces one string of characters with another string.<br><br><span style="text-decoration: underline;">The #define directive is one (old) way of creating a symbolic constant</span> (also known as a named or manifest constant).<br><span style="text-decoration: underline;">The <strong>const</strong> and <strong>enum</strong> keywords are newer techniques for creating constants</span>.<br>It is a well-accepted naming practice to write the names of symbolic constants with all upper-case characters (this provides a visual clue that the name represents a constant).<br><br><span style="text-decoration: underline;">Note</span>:<br>The define directive does not end with a semicolon and there must be at least one space between the directive and the identifier, and between the identifier and the defined value; the defined value (the third part of the directive) is optional.</p>



<pre class="wp-block-code"><code>Stop after the Preprocessing stage. 
<span style="text-decoration: underline;">The output is in the form of preprocessed source code, which is sent to the standard output</span>.
Input files that don't require preprocessing are ignored.

$ gcc -E &lt;program_file_1&gt;.c &lt;program_file_2&gt;.c ... &lt;program_file_n&gt;.c

$ g++ -E &lt;program_file_1&gt;.cpp &lt;program_file_2&gt;.cpp ... &lt;program_file_n&gt;.cpp</code></pre>



<h2>Compiler</h2>



<p><span style="text-decoration: underline;"><strong>The Compiler translates source code into assembly code</strong></span><strong><span style="text-decoration: underline;"> for a specific processor</span></strong>.<br><br>As the Preprocessor processes each source code file one at a time and produces a single temporary file (for each source code file).<br>Similarly, the Compiler processes each temporary file one at a time and produces one assembly code file for each temporary file.<br><br><span style="text-decoration: underline;">The Compiler also detects syntax errors and provides the diagnostic output programmers use to find and correct those errors.<br></span>Despite all that the compiler does, its operation is transparent to programmers for the most part.</p>



<pre class="wp-block-code"><code>Stop after the stage of Compilation, do not Assemble. 
<span style="text-decoration: underline;">The output is in the form of an assembler code file</span> for each non-assembler input file specified.
By default, the assembler file name for a source file is made by replacing the suffix .c, .cpp, .i, .ii, etc., with .s
Input files that don't require compilation are ignored.

$ gcc -S &lt;program_file_1&gt;.c &lt;program_file_2&gt;.c ... &lt;program_file_n&gt;.c

$ g++ -S &lt;program_file_1&gt;.cpp &lt;program_file_2&gt;.cpp ... &lt;program_file_n&gt;.cpp</code></pre>



<h2>Assembler</h2>



<p><strong><span style="text-decoration: underline;">The Assembler translates assembly code into machine code the processor understands and can execute</span></strong>.<br><br><span style="text-decoration: underline;">The purpose of the Assembler is to convert assembly language into <strong>machine code</strong> and generate an <strong>object file</strong></span>. <br><br>When there are calls to external functions in the assembly source file, the Assembler leaves the addresses of the external functions undefined, to be filled in later by the linker.</p>



<pre class="wp-block-code"><code>Compile AND Assemble the source files, but do not Link.
<span style="text-decoration: underline;">The output is in the form of an object file for each source file</span>.
By default, the object file name for a source file is made by replacing the suffix .c, .cpp, .i, .ii, .s, etc., with .o
Unrecognized input files, not requiring compilation or assembly, are ignored.

$ gcc -c &lt;program_file_1&gt;.c &lt;program_file_2&gt;.c ... &lt;program_file_n&gt;.c

$ g++ -c &lt;program_file_1&gt;.cpp &lt;program_file_2&gt;.cpp ... &lt;program_file_n&gt;.cpp</code></pre>



<h2>Linker</h2>



<p>The final stage of compilation is the <strong><span style="text-decoration: underline;">linking of object files to create an executable program</span></strong>.<br><br>Object files contain machine code and information that the Linker uses to complete its tasks.<br>(Note that &#8220;object&#8221; in this context has nothing to do with the objects involved in Object-Oriented Programming)<br><br><strong>This is where all of the object files and any additional binary libraries are linked together to make the final program.</strong></p>



<p><span style="text-decoration: underline;"><strong>It takes each object files created by the Assembler and links them together, along with system and runtime libraries, to form a complete, executable program</strong></span>.<br><br><strong>An executable requires many external functions from system and runtime libraries</strong>.<br><span style="text-decoration: underline;"><strong>They contain functions that are necessary to run a program on a given architecture</strong></span><br>(linux-vdso.so.n, libc.so.n, ld-linux-x86-64.so.n (amd64), ld-linux.so.n (i386), etc.)</p>



<p><span style="text-decoration: underline;">A library is a binary file (usually not directly executable) containing compiled functions/programming code that may be used/called by other programs/applications</span>.<br><br>As a convention, a library name starts with &#8216;lib&#8217;, and the extension determines the type of the library:<br><strong>.a</strong> stands for <strong>archive (static library)</strong><br><strong>.so</strong> stands for <strong>shared object</strong> <strong>(dynamic library)</strong><br><br><strong><span style="text-decoration: underline;">Static Linking</span></strong> :<br><strong>The linker adds all the libraries the program needs inside the final executable file</strong> (<strong>content is included</strong>).<br>Static linking may simplify the process of distributing a program to multiple similar environments, since <strong>it already has everything it needs to run</strong>. But any update to the libraries dependencies won&#8217;t be effective until you perform a whole compilation and linking process again.<br><br><strong><span style="text-decoration: underline;">Dynamic Linking</span></strong> :<br><strong>The linker only places a reference to the required libraries in the final program</strong> (<strong>content is not included</strong>).<br><span style="text-decoration: underline;"><strong>The actual linking happens when the program is executed (loaded at runtime</strong>)</span>.<br>You don&#8217;t need to recompile the program if any update occurs to the libraries dependencies, but they all <strong>need to be present/installed on the system for the program to work</strong>.<br></p>



<pre class="wp-block-code"><code><strong><span style="text-decoration: underline;">Libraries </span></strong><span style="text-decoration: underline;"><strong>(binaries) </strong></span><strong><span style="text-decoration: underline;">Location</span></strong>

<strong>GNU C Library: Shared libraries   (package: libc&lt;n&gt;)</strong>
Contains the standard libraries that are used by nearly all programs on the system.

<strong>GNU Standard C++ Library v3       (package: libstdc++&lt;n&gt;)</strong>
Contains an additional runtime library for C++ programs built with the GNU compiler. 

Symbolic link /lib -&gt; /usr/lib
On Debian 64-bits amd64 architecture:   /lib/x86_64-linux-gnu/
On Debian 32-bits i386 architecture:    /lib/i386-linux-gnu/

<strong><span style="text-decoration: underline;">List of paths that ld (the linker) will search for libraries</span></strong>
The directories are searched in the order in which they are specified
$ ld --verbose | grep SEARCH_DIR | sed 's/; /\n/g'</code></pre>



<p></p>



<p>The name of the executable file depends on the hosting Operating System:<br>On Linux, Unix, and macOS systems, the linker produces a file named &#8216;a.out&#8217; by default.<br>On a Windows computer, the linker produces a file whose name ends with a .exe extension.</p>



<p>Users may also specify a name that overrides the default.<br><br>For example, if you want gcc to generate an executable with a specific name, use the -o option followed with the desired name:</p>



<pre class="wp-block-code"><code>$ gcc -o &lt;program_name&gt; &lt;program_file_1&gt;.c &lt;program_file_2&gt;.c ... &lt;program_file_n&gt;.c

$ g++ -o &lt;program_name&gt; &lt;program_file_1&gt;.cpp &lt;program_file_2&gt;.cpp ... &lt;program_file_n&gt;.cpp</code></pre>



<p>When the compiling finishes, temporary/intermediate files are removed.</p>



<pre class="wp-block-code"><code>This command shows all shared library dependencies (what libraries the executable requires)

$ ldd &lt;program_name&gt;</code></pre>



<pre class="wp-block-code"><code>readelf displays information about ELF format object files. 
The options control what particular information to display.
This program performs a similar function to objdump but it goes into more detail

$ readelf -a &lt;program_name&gt;</code></pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://itec4b.com/wp-content/uploads/2023/01/g-compilation-process.png"><img decoding="async" src="https://itec4b.com/wp-content/uploads/2023/01/g-compilation-process.png" alt="" class="wp-image-841" width="757" height="459" srcset="https://itec4b.com/wp-content/uploads/2023/01/g-compilation-process.png 1009w, https://itec4b.com/wp-content/uploads/2023/01/g-compilation-process-300x182.png 300w, https://itec4b.com/wp-content/uploads/2023/01/g-compilation-process-768x466.png 768w" sizes="(max-width: 757px) 100vw, 757px" /></a><figcaption class="wp-element-caption"><strong>g++ Compiler Operations</strong></figcaption></figure></div>


<h2><strong>Loader</strong></h2>



<p>This stage happens when the program starts up.<br>The program is scanned for references to shared libraries.<br>Any references found are resolved and the libraries are mapped into the program.</p>



<pre class="wp-block-code"><code>The <strong>dynamic linker/loader programs</strong> <strong>ld.so</strong> (or<strong> ld.so.n</strong>) and <strong>ld-linux.so</strong> (or <strong>ld-linux.so.n</strong>) find and load the shared objects (shared libraries) needed/used by a program, prepare the program to run, and then run it.

In Debian:
$ ls -l /lib/$( arch )-linux-gnu/ld-linux*

$ &lt;loader_program&gt; &lt;program_name&gt;</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
