Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Gcc command under Linux

Shulou Source: shulou.com Published: 2022-06-01 16:00:39 09月11日 Update

This article mainly introduces how to use the Gcc command under Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

GCC means only GNU C Compiler. After so many years of development, GCC has not only supported C language; it now supports Ada language, C++ language, Java language, Objective C language, Pascal language, COBOL language, and Mercury language which supports functional programming and logical programming, and so on.

Syntax gcc (options) (parameters) option-o: specify the generated output file;-E: perform compilation preprocessing only;-S: convert C code to assembly code;-wall: display warning messages;-c: only compile operations, not concatenation operations. Parameter C source file: specify the C language source code file. Example

Common compilation command options

Suppose the source program file name is test.c

No option compilation link

Gcc test.c

Preprocess, assemble, compile and link the test.c to form an executable file. No output file is specified here, and the default output is a.out.

Option-o

Gcc test.c-o test

Preprocess, assemble, compile and link the test.c to form an executable file test. The-o option specifies the file name of the output file.

Option-E

Gcc-E test.c-o test.i

Output the test.i file by preprocessing test.c.

Option-S

Gcc-S test.i

The preprocessing output file test.i is assembled into a test.s file.

Option-c

Gcc-c test.s

Compile the assembly output file test.s to output the test.o file.

No option link

Gcc test.o-o test

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

Option-O

Gcc-O1 test.c-o test

Use compiler optimization level 1 to compile the program. The level is 1-3, the higher the level, the better the optimization, but the longer the compilation time.

Compilation method of multi-source files

If you have multiple source files, there are basically two compilation methods:

Suppose you have two source files, test.c and testfun.c

Multiple files compiled together

Gcc testfun.c test.c-o test

Testfun.c and test.c are compiled separately and linked into test executable files.

Compile each source file separately, and then link the compiled output target file.

Gcc-c testfun.c # compiles testfun.c to testfun.ogcc-c test.c # compiles test.c to test.ogcc-o testfun.o test.o-o test # links testfun.o and test.o to test

Compared with the above two methods, the first method requires all files to be recompiled, while the second method can only recompile modified files, and unmodified files do not need to be recompiled.

Thank you for reading this article carefully. I hope the article "how to use Gcc commands under Linux" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Compile file output language link method preprocessing source file article support command level code parameter multiple file name programming compiler no two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Xiaomi Apple Docker