GCC-related knowledge points under Linux

Source: Internet
Author: User

Summary:

Summarize the specific use of GCC, the related problems of dynamic library static library

Resources:

"Linux network Programming" isbn:9787302207177 P19

1 About GCC

GCC is a compilation toolset under Linux, an abbreviation for the GNU Compiler Collection, including compilers such as gcc,g++, and a toolset such as AR,NM

The GCC toolset compiles not only C + +, but also other languages such as Objective-c

GCC can also be cross-compiled

File extension Meaning:

Default path:

2 compilation Steps

Precompilation: Include the header file in the source code and replace some macros

Compiling and Optimizing: Programming assembly language

Assembly: Assembler, machine language, this step generates the target file. O

Link: The target file usually parses the variables and functions inside the file, and the referenced functions and variables are not resolved, and other already-written target files need to be introduced, usually referencing the library

3 compiling the program with GCC

Common options

-C Generate target file

-o Specifies the name of the generated file and does not specify a default file name

-E is only preprocessed, not compiled, compiled, and linked

-S compiles only to assembly language, does not assemble and link

-llibrary to search for libraries named library when linking

-ldir add dir to the path list of the search library file

-idir add dir to the path list of the search header file

-static only use static libraries to link, if static libraries and dynamic libraries exist in a directory, then only the static library

1. Compile the source file directly into an executable file

GCC hello.c    // generate Hello.out-o hello hello.c    // Specify the generated executable file named Hello 

2. Compile the source file into a target file and then compile it into an executable file

Gcc-c hello.c    // generate HELL.O-o Hello hello.o        //link, generate executable file Hello 
4 Static and Shared (dynamic) libraries

What is a library
Libraries exist in a large number of Windows platforms and Linux platforms.
In essence, a library is a binary form of executable code that can be loaded into memory by the operating system.
Because Windows and Linux have different platforms (primarily compilers, assemblers, and connectors), the binary of the libraries is incompatible.

Types of libraries
There are two types of libraries under Linux: Static libraries and shared libraries (dynamic libraries).
The difference between the two is that the code is loaded in a different time.
The code for the static library has been loaded into the executable program during compilation, so the volume is large.
The code for a shared library is loaded into memory when the executable is running, only a simple reference during compilation, so the code is small.

The significance of library existence
Libraries are written by someone else's existing, mature, reusable code that you can use but remember to abide by the license agreement.
In reality, every program relies on many underlying libraries, and it is not possible for everyone's code to start from scratch, so the existence of a library is extraordinary.
The benefit of a shared library is that if different applications call the same library, then only one instance of the shared library is needed in memory.

Attention

The library name is a string that does not contain the function library and extension

If static libraries and dynamic libraries exist under the system's search path, dynamic libraries are linked by default, and the-static option is required if you want to force a static library to be linked

Whether a static library or a dynamic library, it is created by an. o file. Therefore, we must first compile the source program hello.c through GCC into an. o file

Static link library

Mr. Cheng is the target file, which is generated using AR-CR

string. O          // generate static library  -o test main.c libstr.a    // use static library

Dynamic Link Library

Dynamic Link library has alias (soname), real name, link name, alias: Lib Library name. So, the real name is the alias based on the minor version number. The link name is the library name that the program uses when linking. In general, use a dynamic link library to always place library files in a directory, and then use a soft link to generate aliases

The "PIC" command-line tag tells GCC that the generated code does not include a reference to the specific memory location of the function and variable, because it is not yet known which memory address space the application using the message code will connect to. The compiled hello.o can be used to create a shared link library. To create a shared link library, you only need to use GCC's "-shared" tag.

Gcc-shared-fpic-o libmyhello.so hello.o

In order for the newly added dynamic link library to be shared by the system, it is necessary to run the dynamic Link Library Management command ldconfig. The role of Ldconfig is to search for dynamic link libraries in the system's default search path, and in the directories listed in the dynamic link library configuration file, create the link and cache files required by the dynamic link loader, and write the results to the cache file/etc/ld.so.cache when the search is complete.

Environment variables

Library_path environment variable: Specifies the program static link library file search path

LD_LIBRARY_PATH environment variable: Specifies the program dynamic link library file search path

Search path order when static library links:

1. LD will go to the parameters in the GCC command-l

2. Re-search for GCC environment variables Library_path

3. Find the default directory/lib/usr/lib/usr/local/lib This is the original compile GCC when written in the program

Dynamic Link-time, execution-time search path order:

1. The dynamic library search path specified when compiling the target code;

2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path;

3. The dynamic library search path specified in the configuration file/etc/ld.so.conf;

4. The default dynamic library search path/lib;

5. The default dynamic library search path/usr/lib.

GCC-related knowledge points 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.