The Linux environment USES g++ to compile the C++ method summary

  • 2020-11-03 22:40:03
  • OfStack

A single source file generates an executable

Here is the code for a simple C++ program saved in file helloworld.cpp:


/* helloworld.cpp */
#include <iostream>
int main(int argc,char *argv[])
{
  std::cout << "hello, world" << std::endl;
  return(0);
}

The program USES cout, defined in the header file iostream, to write a simple string to standard output. This code can be compiled to an executable with the following command:


$ g++ helloworld.cpp

The compiler g++ identifies the file as C++ source code file by checking the suffix name of the file specified on the command line. Compiler default actions: Compile source code files to generate object files (object file), link object files and functions in libstdc++ libraries to get executable programs. Then delete the object file. Because the executable file name is not specified on the command line, the compiler USES the default a.out. The program can be run as follows:


$ ./a.out
hello, world

A more common approach is to specify the file name of the executable with the -ES25en option. The following command will produce an executable named helloworld:


$ g++ helloworld.cpp -o helloworld

Enter the program name on the command line to make it run:


$ ./helloworld
hello, world

The program g++ is a special version of gcc that sets the default language of C++. When linked, it automatically USES C++ standard library instead of C standard library. By following the naming conventions of the source code and specifying the name of the corresponding library, it is possible to compile linked C++ programs using gcc, as shown in the following example:


$ gcc helloworld.cpp -lstdc++ -o helloworld

Option -ES43en (ell) transforms the name following it to the library name libstdc++.a by adding the prefix lib and suffix.a. It then looks for the library in the standard library path. The compilation process and output files for gcc are exactly the same as for g++.

On most systems, a program named c++ is installed when GCC is installed. If installed, it is equivalent to g++, as shown in the following example, and is also used 1 to:


$ c++ helloworld.cpp -o helloworld

Multiple source files generate executables

If more than one source file is specified in the g++ command, they are all compiled and linked into a single 1 executable. Below is a header file named speak.h; It contains the definition of a class that contains only 1 function:


/* speak.h */
#include <iostream>
class Speak
{
  public:
    void sayHello(const char *);
};

The contents of the file speak.cpp are listed below: The body of the function containing the sayHello() function:


/* speak.cpp */
#include "speak.h"
void Speak::sayHello(const char *str)
{
  std::cout << "Hello " << str << "\n";
}

In file ES74en. cpp is a program using the Speak class:


/* hellospeak.cpp */
#include "speak.h"
int main(int argc,char *argv[])
{
  Speak speak;
  speak.sayHello("world");
  return(0);
}

The following command links the compilation of the above two source files into a single 1 executable:


$ g++ helloworld.cpp
0

PS: Say 1 why the file "speak.h" is not mentioned in the command (the reason is that "include" #include" speak.h" is included in "speak.cpp", which means that the file "speak.h" will be searched in the current directory before searching the system header directory. "speak.h" is in the directory, no longer specified in the command).

Source files generate object files

The -ES100en option is used to tell the compiler to compile the source code but not to execute the link and output the result as an object file. The default name of the file is the same as the source file name, with the suffix changed to.o. For example, the following command compiles the source file hellospeak.cpp and generates the object file hellospeak.o:


$ g++ helloworld.cpp
1

The command g++ also recognizes the.o file and passes it as an input file to the linker. The following commands compile the source file as an object file and link it into a single 1 executable:


$ g++ helloworld.cpp
2

Option -o can be used for more than just naming executables. It is also used to name other files that the compiler outputs. For example: The following command generation will produce exactly the same executable as above, except that the intermediate object file has a different name:


$ g++ -c hellospeak.cpp -o hspk1.o 
$ g++ -c speak.cpp -o hspk2.o 
$ g++ hspk1.o hspk2.o -o hellospeak

Compilation preprocessing

Option -ES121en causes g++ to perform no other actions after processing the source code with the compiled preprocessor. The following command preprocesses the source file helloworld.cpp and displays the results in standard output:


$ g++ helloworld.cpp
4

The source code for ES128en.cpp listed earlier in this article is only six lines long, and the program does nothing but display one line of text, but the pre-processed version will be more than 1,200 lines long. This is mainly due to the inclusion of the header iostream, which in turn contains other header files, as well as the definition of several classes that handle input and output.

The GCC suffix for the preprocessed file is.ii, which can be generated by the -ES135en option, for example:


$ g++ helloworld.cpp
5

Generate assembly code

Option -S instructs the compiler to compile the program into assembly language, output assembly language code and end. The following command will generate the assembly language file helloworld. s from the C++ source file:


$ g++ helloworld.cpp
6

The resulting assembly language depends on the target platform of the compiler.

Creating a static library

A static library is a collection of 1 series of object files generated by the compiler. The object file in the library or the object file in the directory is the same when linking a program. The members of a library include normal functions, class definitions, object instances of classes, and so on. Another name for the static library is archive (archive), and the tool for managing this archive is called ar.

In the following example, we create two object modules and then use them to generate a static library.

The header file ES160en.h contains the prototype of the function sayHello() and the definition of the class Say:


$ g++ helloworld.cpp
7

say. cpp is the source code for one of the two object files that we want to add to the static library. It contains the definition body of the sayString() function in the Say class; The declaration of an instance of class Say librarysay is also included:


/* say.cpp */
#include "say.h"
void Say::sayString()
{
  std::cout << string << "\n";
}
 
Say librarysay("Library instance of Say");

The source file sayhello. cpp is the source code for the second object file that we want to add to the static library. It contains the definition of the function sayhello() :


$ g++ helloworld.cpp
9

The following sequence of commands compiles the source file into an object file and commands ar to store it in the library:


$ g++ -c sayhello.cpp
$ g++ -c say.cpp
$ ar -r libsay.a sayhello.o say.o

The program ar creates a new library libsay.a with the parameter r and inserts the object files listed on the command line. In this way, if the library does not exist, the parameter -r creates a new library, and if the inventory is present, the original module is replaced with a new module.

Here is the main program, ES193en. cpp, which calls the code in the library libsay. a:


/* saymain.cpp */
#include "say.h"
int main(int argc,char *argv[])
{
  extern Say librarysay;
  Say localsay = Say("Local instance of Say");
  sayhello();
  librarysay.sayThis("howdy");
  librarysay.sayString();
  localsay.sayString();
  return(0);
}

The program can be compiled and linked with the following commands:


$ g++ saymain.cpp libsay.a -o saymain

When the program is running, the following output is produced:


$ ./a.out
hello, world
3

ps: If there are multiple cpp files in one folder that need to be compiled, in addition to makefile, you can also use "g++ *. cpp-o hello" and" Name of the executable hello generated for compilation ". Make sure that the cpp files and the header files they refer to are in the same directory when compiling.


Related articles: