How Do You Know if You Have a C Compiler?

Bjarne Stroustrup's The C++ Programming Language has a chapter titled "A Tour of C++: The Nuts"—Standard C++. That chapter, in ii.two, mentions in half a page the compilation and linking process in C++. Compilation and linking are two very basic processes that happen all the time during C++ software evolution, just oddly plenty, they aren't well understood by many C++ developers.

Why is C++ source code carve up into header and source files? How is each role seen by the compiler? How does that affect compilation and linking? There are many more than questions like these that you may accept idea about but have come to have as convention.

Whether you are designing a C++ application, implementing new features for it, trying to accost bugs (particularly certain foreign bugs), or trying to make C and C++ lawmaking work together, knowing how compilation and linking works will save you a lot of time and brand those tasks much more pleasant. In this article, you will larn exactly that.

The article will explain how a C++ compiler works with some of the basic language constructs, answer some mutual questions that are related to their processes, and help yous work effectually some related mistakes that developers ofttimes make in C++ development.

Annotation: This article has some example source lawmaking that tin can exist downloaded from https://bitbucket.org/danielmunoz/cpp-article

The examples were compiled in a CentOS Linux machine:

          $ uname -sr Linux iii.10.0-327.36.3.el7.x86_64                  

Using g++ version:

          $ chiliad++ --version g++ (GCC) 4.eight.5 20150623 (Red Lid 4.eight.v-xi)                  

The source files provided should be portable to other operating systems, although the Makefiles accompanying them for the automated build process should be portable only to Unix-similar systems.

Each C++ source file needs to be compiled into an object file. The object files resulting from the compilation of multiple source files are so linked into an executable, a shared library, or a static library (the last of these being simply an annal of object files). C++ source files generally have the .cpp, .cxx or .cc extension suffixes.

A C++ source file can include other files, known as header files, with the #include directive. Header files have extensions like .h, .hpp, or .hxx, or have no extension at all like in the C++ standard library and other libraries' header files (like Qt). The extension doesn't matter for the C++ preprocessor, which volition literally supersede the line containing the #include directive with the entire content of the included file.

The first footstep that the compiler will exercise on a source file is run the preprocessor on it. Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.

Each header file can be opened multiple times during the preprocessing phase of all source files, depending on how many source files include them, or how many other header files that are included from source files also include them (there can be many levels of indirection). Source files, on the other mitt, are opened only once by the compiler (and preprocessor), when they are passed to it.

For each C++ source file, the preprocessor will build a translation unit of measurement by inserting content in it when it finds an #include directive at the same time that it'll exist stripping code out of the source file and of the headers when it finds conditional compilation blocks whose directive evaluates to false. It'll also exercise another tasks similar macro replacements.

Once the preprocessor finishes creating that (sometimes huge) translation unit, the compiler starts the compilation phase and produces the object file.

To obtain that translation unit of measurement (the preprocessed source code), the -East pick tin be passed to the g++ compiler, along with the -o choice to specify the desired name of the preprocessed source file.

In the cpp-article/hello-globe directory, there is a "how-do-you-do-globe.cpp" example file:

          #include <iostream>  int main(int argc, char* argv[]) {     std::cout << "Hi globe" << std::endl;     render 0; }                  

Create the preprocessed file by:

          $ 1000++ -E howdy-world.cpp -o howdy-world.ii                  

And encounter the number of lines:

          $ wc -l hello-world.ii  17558 hi-earth.two                  

It has 17,588 lines in my car. Yous can besides just run make on that directory and information technology'll practice those steps for you.

We tin can see that the compiler must compile a much larger file than the simple source file that nosotros see. This is because of the included headers. And in our example, we take included just 1 header. The translation unit becomes bigger and bigger as we proceed including headers.

This preprocess and compile process is like for C language. It follows the C rules for compiling, and the way information technology includes header files and produces object code is virtually the same.

How Source Files Import and Export Symbols

Allow's see at present the files in cpp-article/symbols/c-vs-cpp-names directory.

How functions are processed.

There is a simple C (non C++) source file named sum.c that exports two functions, i for calculation two integers and 1 for calculation 2 floats:

          int sumI(int a, int b) {     return a + b; }  float sumF(float a, float b) {     render a + b; }                  

Compile it (or run brand and all the steps to create the two example apps to exist executed) to create the sum.o object file:

          $ gcc -c sum.c                  

Now look at the symbols exported and imported by this object file:

          $ nm sum.o 0000000000000014 T sumF 0000000000000000 T sumI                  

No symbols are imported and two symbols are exported: sumF and sumI. Those symbols are exported as part of the .text segment (T), and so they are function names, executable lawmaking.

If other (both C or C++) source files desire to call those functions, they need to declare them before calling.

The standard manner to exercise it is to create a header file that declares them and includes them in whatever source file we want to call them. The header can take any name and extension. I chose sum.h:

          #ifdef __cplusplus extern "C" { 

0 Response to "How Do You Know if You Have a C Compiler?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel