The GCC compiler under Linux

Source: Internet
Author: User

1. Compiling a single file into an executable file

Generate the default executable file a.out

2. Compile a single file for the specified name of the executable file, use the-o option to specify the file name

Generate an executable file named Test

3. Compiling a single source file into a target file

(1) using the-C option to generate the target file

Generate a target file named MAIN.O

(2) Use the-O option to specify the target file name

Generate a target file named TEST.O

4. Compile multiple source files into target files at the same time

To generate a target file with the-C option

Simultaneously generate a target file named MAIN.O and PRINT.O

5. Compiling multiple source files into one executable file

(1) Use the-O option to specify the executable file name and compile the source file directly

Gcc-o Test MAIN.C print.c generates an executable file named Test

(2) Mr. Cheng is the target file, and then linked to the executable file

gcc-o test main.o PRINT.O link to multiple target files, generating an executable file named Test

6. Precompiled commands and generate assembly commands

(1) Pre-compiling with the-e option

gcc-o print.i-e print.c to pre-compile and generate precompiled result files with file name string.i

(2) Generate assembly using the-s option

Gcc-s print.c generate file name consistent (PRINT.S) assembly language file
Gcc-o Print.s-s Print.c Generating an assembly language file named Print.s

7. Create a static library

(1) The static library is a collection of target files, the target file is packaged into a library using the AR and-R options, and the library file is usually suffixed with ". a"

Ar-rcs libprint.a PRINT.O generate a static library with file name Libprint.a
Ar-rcs libleo.a PRINT.O STRING.O generates a static library with the file name Libleo.a, which contains a library of print and string functions

Note: The target file must be used for archiving and will fail on subsequent compiler links if the source file is used directly for archiving

(2) Link Static library

Gcc-o Test MAIN.C LIBPRINT.A generates an executable file named Test

(3) Using the-l option and the-L selection link to the static library

Gcc-o Test Main.c-lprint with the-l option, you do not need to specify the Lib prefix and extension, and you will find the print library in the system default directory, which may fail because there is no Print function library under the default system library. You need to specify an action path for the library using the-l option
Gcc-o Test main.c-l./-lprint Find the library in the current directory, if you need to find multiple directories, you can use the-l option continuously
Gcc-o Test main.c-l./-l./lib/-lprint-lstring find libraries in multiple directories

8. Create a dynamic library

A dynamic library is a library that the program loads at run time

(1) Generate dynamic libraries using the-shared option and generate location-independent code using the-fpic or-fpic option

string. C Generate a dynamic library with file name libleo.so

(2) Link Dynamic Library

Gcc-o Test main.c-l./-lleo generates an executable file named Test

(3) Run the executable file

Usually the program needs to load the dynamic library when executing, the dynamic library must be in the program's search directory, otherwise there will be a run-time error.

There are two ways to add a search directory:

-Add environment variable: Export ld_library_path=/home/leo/test $LD _library_path;

-Using the Ldconfig/home/leo/test command, if the directory list is refreshed without parameters, only the system default directory is added;

(4) Dynamic Loading library:

Use Dlopen (), Dlerror (), Dlsym (), and dlclose () related functions

Open the Dynamic library:

prototype void* Dlopen (constcharint  flag); void* Phandler = Dlopen ("./libleo.so", Rtld_lazy); Open the dynamic library, and the pointer to the library

Get the function pointer:

prototype void* DLSYM (voidchar* symbol); void " Print " ); where Pfunc is a function pointer

Print Error:

prototype Char* dlerror (); Char* perror = dlerror (), where perror is the error message string

Close the dynamic library

prototype void dlclose (void* phandler);d lclose (phandler);

To compile the executable file:

Gcc-o Test MAIN.C LIBLEO.SO-LDL

Common options for GCC:

-i+dir: Enlarge The search directory of the header file;

-l+dir: Enlarge The search directory of the link library;

-static: Use static library to link;

-G: Contains debug information;

-on: Optimization program;

-wall: Open all warning messages;

-werror: Treats all warning messages as error messages and interrupts compilation;

Get the latest GCC version:

Apt-get Install GCC

The GCC compiler under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.