How to compile CPP project with G + + under Linux

Time:2021-4-17

Suffix of related files in C + + Programming

1. Single source file generates executable program

Here is a file saved inhelloworld.cpp In a simple C + + program code:


/* 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 the standard output. The code can be compiled into an executable file with the following command:

$ g++ helloworld.cpp

Compiler G + + can identify the file as C + + source code file by checking the suffix of the file specified in the command line. The default action of compiler is to compile source code file, generate object file, link object file and libstdc++ Functions in the library get the executable program. Then delete the object file. Because the file name of the executable program is not specified in the command line, the compiler adopts the default a.out. The program can run as follows:

$ ./a.out
hello, world

A more common practice is to specify the file name of the executable program through the – O option. The following command produces an executable named HelloWorld:

 $ g++ helloworld.cpp -o helloworld

Enter the program name in the command line to make it run

 $ ./helloworld 
hello, world

Program G + + is a special version of C + + which sets the default language of GCC. When linking, it automatically uses C + + standard library instead of C standard library. It is feasible to compile linked C + + programs with GCC by following the naming rules of source code and specifying the names of corresponding libraries

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

Option – L (ELL) transforms the name following it to the library name libstdc + +. A by adding the prefix lib and suffix. A. It then looks up the library in the standard library path. The compilation process and output file of GCC are exactly the same as that of G + +.

On most systems, a program called C + + is installed when GCC is installed. If it is installed, it is equivalent to G + +, as shown in the following example

 $ c++ helloworld.cpp -o helloworld

2. Multiple source files generate executable programs

If more than one source file is specified in the G + + command, they will be compiled and linked into a single executable file. Here is a header file called speak. H; it contains the definition of a class that contains only one function:


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

Here are the files speak.cpp Content: containssayHello() Body of function:


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

Documents hellospeak.cpp Inside is a program that uses the speak class:


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

The following command compiles and links the above two source files into a single executable program:

 $ g++ hellospeak.cpp speak.cpp -o hellospeak

PS: Here’s why it’s not mentioned in the command“speak.h“The document (reason: in“ speak.cpp “Include” speak. H “in” include “speak. H” means to search for the file “speak. H” in the current directory before searching the system header file directory. And “speak. H” is in the directory, no need to specify it in the command).

3. Source files generate object files

Option – C is used to tell the compiler to compile the source code but not to execute the link. The output is an object file. The default file name is the same as the source file name, but the suffix is changed to. O. For example, the following command compiles the source filehellospeak.cpp And generate the object file hellospeak. O:

$ g++ -c hellospeak.cpp

The command G + + can also recognize. O files and pass them to the linker as input files. The following command compiles the source file as an object file and links it into a single executable program:


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

The – O option is not only used to name executable files. It is also used to name other files that the compiler outputs. For example, except that the object file in the middle has different names, the following command will generate the same executable file as above:


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

4. Compile preprocessing

Option – e enables G + + to use the compiler preprocessor to process the source code without performing other actions. The following command preprocesses the source file helloworld.cpp The results are displayed in the standard output

 $ g++ -E helloworld.cpp

As listed earlier in this articlehelloworld.cppSource code, only six lines, and the program in addition to showing a line of text, nothing to do, but the version after preprocessing will be more than 1200 lines. This is mainly because the header file iostream is included, and it contains other header files. In addition, there are several definitions of classes dealing with input and output.

The GCC suffix of the preprocessed file is. II, which can be generated by the – O option, for example:

 $ gcc -E helloworld.cpp -o helloworld.ii

5. Generate assembly code

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

$ g++ -S helloworld.cpp

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

6. Create a static library

A static library is a collection of object files generated by a compiler. When linking a program, the object file in the library or the object file in the directory is the same. Members of the library include ordinary functions, class definitions, object instances of classes, and so on. Another name of static library is archive, and the tool to manage this kind of archive is ar.

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

The header file say. H contains functionssayHello() The prototype of and the definition of class say:


/* say.h */
#include <iostream>
void sayhello(void);
class Say {
 private:
 char *string;
 public:
 Say(char *str)
 {
 string = str;
 }
 void sayThis(const char *str)
 {
 std::cout << str << " from a static library\n";
 }
 void sayString(void);
};

Here’s the file say.cpp Is one of the two object files we want to add to the static library. It is contained in the say class sayString() The definition body of the function; an instance of the class say, and the declaration of librarysay are also included


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

Say librarysay("Library instance of Say");

Source code filesayhello.cppIs the source code of the second object file we want to add to the static library. It contains the definition of the function sayhello():


/* sayhello.cpp */
#include "say.h"
void sayhello()
 {
 std::cout << "hello from a static library\n";
 }

The following command sequence compiles the source file into an object file, and the command ar stores it in the Library:


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

Program ar creates a new library with parameter – Rlibsay.a And insert the object file listed in the command line. In this way, if the library does not exist, the parameter – r will create a new library, and if the library exists, the original module will be replaced with a new module.

Here is the main programsaymain.cpp, which calls the code in libsay. A library:


/* 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, it produces the following output:

hello from a static library
howdy from a static library
Library instance of Say
Local instance of Say

summary

Here is the article about compiling CPP project with G + + in Linux. For more information about compiling CPP with G + + in Linux, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]