페이지

2020년 6월 19일 금요일

Compiling and Executing Go Programs

* Now that you know how to create and edit files, you can generate new programs. The most commonly used languages in the CS Department at the moment are C++, C, and Java.

The most popular C++ and C compilers are g++ and gcc. 
The Structure of C++ and C Programs

* Although not really a Unix-specific topic, it's hard to discuss how to compile code under any operating system wityhout a basic understanding how programs are put together.

* The source code for a C++(or C) program is contained in a number of text files called source files. Very simple programs might be contained within a single source file, but as our programs grow larger and more complicated, programmers try to keep things manageable by splitting the code into multiple source files, no one of which should b terribly long.

* There are two dirrenrent kinds of source files: header files and not-header files. Header files are generally given names ending in ".h". Non-header files are generally given names ending in ".cpp" for C++ code and ".c" for C code.

* Header and non-header files are treated differently when we build programs. Each non-header file i compiled separately from the others (Figure 7.1, "Building 1 program from many files"). This helps keep the compilation times reasonable, particularly when we are fixing bugs in a program and may have changed only one or two non-header files. Only those changed files need to b recompiled.

* Header files are not compiled directly, Instead, header files are included into other source files via #include. In fact, when you invoke a C/C++ compiler, before the "real" compiler starts, it runs a pre-processor whose job is to handle the special instructions that begin with #. In the case of #include statements, the pre-processor simply grabs the relevant header file and sticks it content into the program right at the spot of the #include.


#include <iostream>
#incldue <string>

using namesapce std;
int main()
{
    string greeting = "Hello!";
    cout << greeting << endl;
    return 0;
}

* This can result in a dramatic increase in the amount of code that actually gets processed. The code shown here, for example, is pretty basic. But the #include statements bring in a entire library of I/O and string-related declarations from the C++ standard library. Here, for example, is the output of the pre-processor for one compiler. (If you look at the very end, you can recognize the main code for this program.)

* A header file can be #included from any number of other header and non-header files. That is, in fact, the whole point of having header files. Header files should contain declarations of things that need to be shared by multiple other source files. Non-header files should declare only things that do not need to be shared.

* As we go though all the compilation steps required to build a program, anything that appears in a non-header file will be processed exactly once by the compiler.
Anything that appears in a header file may be processed multiple times by the compiler.




















댓글 없음: