In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, Xiaobian will bring you about how to use Gcc common commands in Linux system. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
GCC stands for GNU C Compiler. After years of development, GCC has not only been able to support C language; it now also supports Ada, C++ language, Java, etc., the following good tutorial network for everyone to explain in detail the Linux system Gcc common commands.
GCC Profile
Gcc (GNU C Compiler) is a powerful multi-platform compiler launched by GNU. It is one of the representative works of GNU.
gcc is a supercompiler that compiles executable programs on a variety of hardware platforms, with an average efficiency of 20%-30% higher than that of ordinary compilers.
The Gcc compiler compiles and links C, C++ source programs, assembly routines, and object programs into executable files. If no executable file name is given, gcc will generate a file named a.out.
In Linux, executable files have no uniform suffix, and the system distinguishes executable files from non-executable files by file attributes.
2 Simple compilation example programs are as follows:
//test.c
#include int main(void)
{
printf("Hello World!\ n");
return 0;
}
This program, compiled in one step, is:
gcc test.c -o test
In essence, the compilation process is divided into four stages, namely preprocessing (also known as pre-compilation), compilation (Compilation), assembly (Assembly) and linking (Linking).
2.1 Preconditioning gcc -E test.c -o test.i or gcc -E test.c
You can output test.c preprocessed code stored in test.i file. Open the test.i file, take a look, and you'll understand. The latter instruction is to output the preprocessed code directly in the command line window.
The-E option of gcc allows the compiler to stop after preprocessing and output the preprocessing results. In this case, the preprocessing results in the insertion of the contents of the stdio.h file into test.c.
2.2 After compilation into assembly code (Compilation) preprocessing, you can directly compile the generated test.i file to generate assembly code:
gcc -S test.i -o test.s
The-S option of gcc indicates that during program compilation, after assembly code is generated, stop, -o outputs assembly code files.
2.3 Assembly For the assembly code file test.s generated in the previous section, the gas assembler is responsible for compiling it into an object file, as follows:
gcc -c test.s -o test.o
2.4 Linking The gcc linker is provided by gas and is responsible for linking the object file of the program with all the additional object files required to generate the executable file. Additional object files include static and dynamic link libraries.
For the test.o generated in the previous section, connect it with the C standard input and output library, and finally generate the program test
gcc test.o -o test
In the command line window, execute./ Test, let it say HelloWorld!
Compilation of multiple program files Usually the whole program is composed of multiple source files, and accordingly a number of compilation units are formed, which can be well managed using GCC. Suppose you have a program consisting of two source files test1.c and test2.c. To compile them and eventually generate the executable test, you can use the following command:
gcc test1.c test2.c -o test
If more than one file is being processed at the same time, GCC still follows the pre-processing, compilation, and linking process. If you dig deeper, the above command is roughly equivalent to executing the following three commands in sequence:
gcc -c test1.c -o test1.o
gcc -c test2.c -o test2.o
gcc test1.o test2.o -o test
Error detection gcc -pedantic illcode.c -o illcode
The-pedantic compilation option does not guarantee full compatibility with ANSI/ISO C standards, it is only used to help Linux programmers get closer to that goal. Or in other words, the-pedantic option helps programmers detect some, but not all, code that does not conform to ANSI/ISO C standards, and in fact only those cases that require compiler diagnostics in ANSI/ISO C standards are likely to be detected and warned by GCC.
In addition to-pedantic, GCC has a number of other compilation options that can also generate useful warning messages. Most of these options start with-W, and the most valuable of them is-Wall, which allows GCC to generate as many warning messages as possible.
gcc -Wall illcode.c -o illcode
The warning message given by GCC, while not strictly wrong, could well be the wrong shelter. A good Linux programmer should try to avoid warning messages and keep his code standard and robust. So treating warning messages as coding errors is commendable! So, compile the program with the-Werror option, and GCC stops compiling at all warning points, forcing the programmer to modify his code as follows:
gcc -Werror test.c -o test
5 library file connection development software, completely do not use the third-party library situation is relatively rare, usually need to rely on the support of many function libraries to complete the corresponding function. From the programmer's point of view, a library is actually a collection of header files (.h) and library files (so, or lib, dll). While most functions under Linux place header files in/usr/include/and library files in/usr/lib/by default, library files used by Windows are primarily located in Visual Stido's include and lib directories and in the system folder. But sometimes, we want to use the library no longer in these directories, so GCC must use its own way to find the required header files and library files at compile time.
For example, our program test.c uses c to connect mysql on linux. At this time, we need to download MySQL Connectors C library from mysql official website. After downloading and extracting, there is an include folder containing mysql connectors header file and a lib folder containing binary so file libmysqlclient.so.
The path to the include folder is/usr/dev/mysql/include, and the lib folder is/usr/dev/mysql/lib
5.1 Compile to executable file First we have to compile test.c into the target file, this time we need to execute
gcc –c –I /usr/dev/mysql/include test.c –o test.o
5.2 Link Finally we link all the object files into executable files:
gcc –L /usr/dev/mysql/lib –lmysqlclient test.o –o test
Library files under Linux are divided into two categories: dynamic link libraries (usually ending in.so) and static link libraries (usually ending in.a), the only difference between the two is whether the code required for program execution is loaded dynamically at runtime or statically at compile time.
5.3 By default, GCC prefers dynamic link libraries when linking, considering static link libraries only when dynamic link libraries do not exist, if necessary, you can add the-static option at compile time to force static link libraries.
The library files libmysqlclient.so and libmysqlclient.a are required for linking in the/usr/dev/mysql/lib directory. In order for GCC to use only static link libraries for linking, you can use the following command:
gcc –L /usr/dev/mysql/lib –static –lmysqlclient test.o –o test
Search path order when static library links:
\1. ld looks for the GCC parameter-L \2. Find the environment variable LIBRARY_PATH\3. /lib /usr/lib /usr/local/lib is the default directory that was written in the program when compiling gcc.
Dynamic link time, execution time search path order:
\1. Dynamic library search path specified when compiling object code\2. Dynamic library search path specified by environment variable LD_LIBRARY_PATH\3. Dynamic library search path specified in configuration file/etc/ld.so.conf\4. Default dynamic library search path/lib \5. Default dynamic library search path/usr/lib
Related environment variables: LIBRARY_PATH environment variables: Specify the search path for program static link library files LD_LIBRARY_PATH environment variables: Specify the search path for program dynamic link library files.
The above is how to use Gcc common commands in Linux system shared by Xiaobian. If there is similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.