Specific use of Linux gcc command

Specific use of Linux gcc command

01. Command Overview

The gcc command uses the C/C++-based compiler launched by GNU. It is the most widely used compiler in the open source field. It has powerful functions and compiles code to support performance optimization.

gcc is the GNU Compiler Collection, which includes the front ends of C, C++, Objective-C, Fortran, Java, Ada, Go, and D, as well as libraries for these languages ​​(such as libstdc++, libgcj, etc.). GCC was originally intended to be a compiler written specifically for the GNU operating system. The GNU system is completely free software. Here, “free” means that it respects the freedom of the user.

02. Command format

Usage: gcc [options] file...

03. Common options

 -pass-exit-codes Return the highest error code when exiting a certain stage --help Display this help description --target-help Display target machine specific command line options --help={common|optimizers|params|target|warnings|[^]
 {joined|separate|undocumented}}[,...]
              Displays command line options for a specific type (use '-v --help' to display the command line arguments for the subprocess)
 --version Display compiler version information -dumpspecs Display all built-in spec strings -dumpversion Display the compiler version number -dumpmachine Display the compiler's target processor -print-search-dirs Display the compiler's search path -print-libgcc-file-name Display the name of the compiler's companion library -print-file-name=<library> Display the full path to <library> -print-prog-name=<program> Display the full path to the compiler component <program> -print-multiarch Display the target's normalized GNU triplet, used as
              a component in the library path
 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
 -pie Create a position independent executable
 -shared Create a shared library
 -x <language> specifies the language of the following input files. Allowed languages ​​include: c c++ assembler none
              'none' means to restore the default behavior, which is to guess the language of the source file based on the file extension.

04. Reference examples

4.1 Generate a default executable file

[deng@localhost bak]$ gcc test.c 
[deng@localhost bak]$

Preprocess, assemble, compile and link test.c into an executable file. If no output file is specified, the default output is a.out.

4.2 Specifying output files

[deng@localhost bak]$ gcc test.c -o test
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c
[deng@localhost bak]$ 
 

Preprocess, assemble, compile and link test.c to form the executable file test. The -o option is used to specify the output file name.

4.3 Preprocessing only, no compilation, assembly or linking

[deng@localhost bak]$ gcc -E test.c -o test.i 
[deng@localhost bak]$

Preprocess test.c and output test.i file.

4.4 Compile to assembly language without assembling and linking

[deng@localhost bak]$ gcc -S test.c -o test.s
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c test.i test.s
[deng@localhost bak]$

Assemble the preprocessed output file test.i into the test.s file.

4.5 Compile and assemble to object code without linking

[deng@localhost bak]$ gcc -c test.c -o test.o
[deng@localhost bak]$ ls
5th 6th 7th 8th 9th test test.c test.i test.o test.s
[deng@localhost bak]$

4.6 Generate executable file from target code

[deng@localhost bak]$ gcc test.o -o test
[deng@localhost bak]$

Link the compiled output file test.o into the final executable file test.

4.7 Specifying optimization levels when compiling

[deng@localhost bak]$ gcc -O1 test.c -o test
[deng@localhost bak]$

Compile the program using compilation optimization level 1. The levels are 1 to 3. The higher the level, the better the optimization effect, but the longer the compilation time.

4.8 Multi-file compilation

[deng@localhost bak]$ gcc testfun.c test.c -o test
[deng@localhost bak]$

Compile testfun.c and test.c separately and link them into the test executable file.

4.9 Multi-file compilation method 2

[deng@localhost bak]$ gcc -c test.c  
[deng@localhost bak]$ gcc -c testfun.c  
[deng@localhost bak]$ gcc test.o testfun.o -o test
[deng@localhost bak]$

This is the end of this article about the specific use of Linux gcc command. For more relevant Linux gcc command content, please search 123WORDPRESS.COM’s previous articles or the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to install Linux online software gcc online
  • Installation process of GCC compiler on Linux
  • Detailed explanation of gcc command usage under Linux system

<<:  How to use anti-shake and throttling in Vue

>>:  Win7 installation MySQL 5.6 tutorial diagram

Recommend

About vue component switching, dynamic components, component caching

Table of contents 1. Component switching method M...

Some lesser-known sorting methods in MySQL

Preface ORDER BY 字段名升序/降序, I believe that everyon...

Solution to the error when installing Docker on CentOS version

1. Version Information # cat /etc/system-release ...

Front-end JavaScript thoroughly understands function currying

Table of contents 1. What is currying 2. Uses of ...

Detailed explanation of memory management of MySQL InnoDB storage engine

Table of contents Storage Engine Memory Managemen...

Vue uses ECharts to implement line charts and pie charts

When developing a backend management project, it ...

Analysis of the Nesting Rules of XHTML Tags

In the XHTML language, we all know that the ul ta...

MySQL complete collapse query regular matching detailed explanation

Overview In the previous chapter, we learned abou...

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

Solution to the problem that Java cannot connect to MySQL 8.0

This article shares a collection of Java problems...

How to get the width and height of the image in WeChat applet

origin Recently, I am working on requirement A, i...

Disadvantages and reasonable use of MySQL database index

Table of contents Proper use of indexes 1. Disadv...

Detailed installation tutorial of mysql 5.7 under CentOS 6 and 7

You always need data for development. As a server...

JavaScript to achieve simple provincial and municipal linkage

This article shares the specific code for JavaScr...