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

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

ElementUI implements cascading selector

This article example shares the specific code of ...

How to solve the 10060 unknow error when Navicat remotely connects to MySQL

Preface: Today I want to remotely connect to MySQ...

Detailed explanation of Vue configuration request multiple server solutions

1. Solution 1.1 Describing the interface context-...

Implementation of React configuration sub-routing

1. The component First.js has subcomponents: impo...

Vue implements multi-tab component

To see the effect directly, a right-click menu ha...

MySQL slow query: Enable slow query

1. What is the use of slow query? It can record a...

Docker container accesses the host's MySQL operation

background: There is a flask project that provide...

Linux type version memory disk query command introduction

1. First, let’s have a general introduction to th...