[Repost] [Linux] Clarify the relationship between gcc, glibc, libstdc ++

[Linux] Clarify the relationship between gcc, glibc, libstdc ++

\
Bounty Micheal
2019.06.21 21:18:08 Word Count 1,993 Reading 249

When you write C / C ++ code under Linux, do you encounter many problems with compilation and linking? 

Reporting glibc, gcc, g ++ and other related errors from time to time, it is often impossible to get started, and it is quite confusing. This is also a problem that often occurs during the compilation and linking process.

This article is not to introduce how to compile and link, but to sort out some of the concepts and problems encountered in the process of compiling and linking. Especially libc, glib, glibc, eglibc, libc ++, libstdc ++, gcc, g ++.

 

Speaking from libc.

libc is the original standard C library under Linux, which is the place defined by the header file #include <stdio.h> that was originally written when hello world was written. It was gradually replaced by glibc, which is the legendary GNU C Library. In addition to libc, there are klibc and uclibc. Now as long as you know that glibc is the most used, some mainstream Linux operating systems such as Debian, Ubuntu, Redhat, etc. use glibc or its variants, which will be described below.

What did glibc do?

glibc is the lowest-level API in the Linux system, and almost any other runtime library depends on glibc. 

The main function of glibc is the encapsulation of system calls. If you think about it, how can you use the fopen function to open files directly in C code? Opening the file will eventually trigger the sys_open system call in the system, and the intermediate processing is done by glibc. This article details how glibc interacts with upper-level applications and system calls. In addition to encapsulating system calls, glibc itself also provides some necessary functions of upper-layer application functions, such as string, malloc, stdlib, linuxthreads, locale, signal, etc.

What is eglibc? 

The e here means Embedded, which is the variant glibc mentioned earlier.

The main feature of eglibc is to better support the embedded architecture, which can support different shells (including embedded), but it is binary compatible with glibc, which means that if your code depends on the eglibc library before, then after changing to glibc No need to recompile. The ubuntu system uses eglibc (not glibc), don't believe it, you execute ldd -version or /lib/i386-linux-gnu/libc.so.6 (64-bit system runs / lib / x86_64-linux-gnu) , It will display the version information of eglibc / glibc in your system. Mentioned here is libc.so.6, this file is the generated library file compiled by eglibc / glibc.

 
 

A glib looks very similar, so what is it?

Glib is also a C program library, but it is relatively lightweight. Glib encapsulates the data types in C language into its own data types, provides the definition and processing functions of data structures commonly used in C language, interesting macros and portable Encapsulation, etc. (Note: glib is portable, indicating that you can use it under linux or under windows).

Does it have anything to do with glibc?

In fact, unless your program code will use the data structures or functions in the glib library, the glib library will not be installed by default in the ubuntu system (it can be manually installed by apt-get install libglib2.0-dev), the famous Both GTK + and Gnome use the glib library.

Want to learn more about glib? You can refer to GLib Reference Manual

GLib provides core application components for libraries and programs written in C language. It provides the core object system used in GNOME, the implementation of the main loop, and a set of tool functions for manipulating strings and common data structures.

https://developer.gnome.org/glib

 

Seeing this, you should know how important these libraries are? 

During the compilation process, the C code you wrote may appear to be clearly defined in these libraries, but there will also be errors such as 'Undefined', 'Unreference', etc. At this time, you may wonder whether these libraries are faulty. ? Is it time to start a new gilbc / eglibc? It is emphasized here that before you prepare to replace / upgrade these libraries, you should think about it. Do you really want to replace / upgrade? You have to know what you are doing! You should always know how much the impact of glibc / eglibc is. No matter what program you deploy before, ls, cd, mv, ps, etc. of the Linux system all depend on it. Many people have had painful lessons in replacement / upgrade, Even the entire system crashed and could not start. Therefore, it is strongly not recommended to replace / upgrade these libraries!

 

Of course, if you write C ++ code, there are two other libraries that should be taken seriously. Is libc ++ / libstdc ++ related to these two libraries? Have.

 

Both are C ++ standard libraries. libc ++ is a C ++ standard library specially rewritten for the clang compiler, so libstdc ++ is naturally a matter of gcc.

The relationship between libstdc ++ and gcc is like clang and libc ++. The differences are not described in detail here.

Talk about the relationship between libstdc ++ and glibc. 

libstdc ++ and gcc are bundled together, which means that libstdc ++ will be installed when installing gcc. So why are glibc and gcc not bundled together? Compared to glibc, libstdc ++ provides a standard library for c ++ programs, but it does not deal with the kernel. For system-level events, libstdc ++ first interacts with glibc to communicate with the kernel. Compared to glibc, libstdc ++ is less basic.

After talking about these libraries, what are these libraries used for? Of course, it is necessary to link them with your program! At this time, I have to talk about gcc (of course, there are compilers such as clang and llvm mentioned earlier, this article will not elaborate on their differences).

The C code you write. The C file is first converted to an assembly .S file by gcc, and then the assembler as converts the .S file to machine code .o files, and the generated .o files are then combined with other .o files, or as mentioned before The libc.so.6 library file is linked to generate an executable file through the ld linker. Of course, when you compile your code to use gcc, the gcc command has already helped you do all these details.

What does g ++ do? 

Slowly, don't think that gcc can only compile C code, and g ++ can only compile c ++ code. The suffix is ​​.c, gcc treats it as a C program, and g ++ as a c ++ program, and the suffix is ​​.cpp, both will be considered as c ++ programs. Note that although c ++ is a superset of c, both The requirements for grammar are different.

During the compilation phase, g ++ will call gcc. For c ++ code, the two are equivalent, but because the gcc command cannot be automatically connected to the library used by the C ++ program, you need this, gcc -lstdc ++, so if your Makefile is not manually With the libstdc ++ library, you will usually be prompted with an error asking you to install the g ++ compiler.

Ok, let's talk about this, clarifying the relationship between these libraries and the compiler, I believe it will help you solve the errors encountered during the compilation and linking process.

 

reference

 

Clarify the relationship between gcc, libc, libstdc ++

https://blog.csdn.net/haibosdu/article/details/77094833

 

GCC, the GNU Compiler Collection

http://gcc.gnu.org

 

GLib Reference Manual

https://developer.gnome.org/glib

http://www.chongh.wiki/blog/2016/05/25/lib-compile-link

 

Compile and install gcc8.1.0

https://blog.csdn.net/longji/article/details/80400339

 

Compile and install gcc5.3.0 on Linux

https://www.cnblogs.com/damonxu/p/6434265.html

 

Compile gcc to solve libstdc ++ version problem

https://blog.csdn.net/na_beginning/article/details/53413997

 

index of /software/gcc/releases

http://ftp.tsukuba.wide.ad.jp/software/gcc/releases

 

Upgrade and install GCC

https://www.cnblogs.com/lijuanhu321/p/7822119.html

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12679306.html