GCC (2) [version 2]

Source: Internet
Author: User

Header files and library files

1. Summary

When using C and other languages for program design, we need the header file to provide the definition of constants and the declaration of system and library function calls.

Library files are a set of pre-compiled functions that are written in accordance with the reusable principle. They are usually composed of a group of interrelated functions used to complete a common task. For example, functions (ncurses Library) and database access routines (DBM database) used to handle screen display.

 

2. advantages of using the database:

1) Modular Development

2) reusability

3) maintainability

 

3. Location of header files and library files

/Usr/include and the include folder under its subdirectories

/Usr/local/include and the include folder under its subdirectories

/Usr/lib

/Usr/local/lib

/Lib

 

 

4. Use the external library [-I option]

//例程#include <math.h>#include <stdio.h>int main(){    double ans = pow(2.0,3.0);    printf("pow(%lf,%lf) = %lf\n",2.0,3.0,ans);    return 0;}

Compile:

Gcc-wall Calc. C-o calc-LM

#-LM indicates the link to the libm. So or libm. A library file

 



Static and shared libraries

1. Summary

Static Library (. A): When the program compiles the link, it links the library code to the executable file. Static databases are no longer needed when the program runs, and static databases occupy a large disk space.

Shared Library (. So/. SA): the code of the shared library is linked only when the program is running. Multiple programs can share the code of the used library. [The. A file cannot be found in the/usr and/lib directories of the latest Linux [centos 6.4].]

 

 

2. Advantages of shared libraries:

An executable file linked to the shared library only contains a table of the function entry address it uses, rather than the entire machine code of the target file where the external function is located.

Before the executable file starts running, the machine code of the external function is copied from the shared library on the disk to the memory by the operating system. This process is called Dynamic Link (Dynamic Linking)

Shared libraries can be shared among multiple programs, so dynamic links make the executable files smaller, saving disk space. The operating system uses the virtual memory mechanism to allow a shared library in the physical memory to be used by all processes in the database, saving both memory and disk space.



Generate static library

-A static library is generated when multiple. O files are packaged together.

 

1) GCC-C hello_fn.c

# Same as gcc-wall-C hello_fn.c-O hello_fn.o

2) Ar RCS libhello. A hello_fn.o search. o

# Arar is the GNU archive tool, with the "replace and create" function of the "cross-sectional Scripting (RvS)" command. It is equivalent to packaging the. o file into a. a file, and the. o file can have multiple

 

3) GCC-Wall Main. c libhello. A-O main

# Compile each module and generate an executable file by link [method 1]

4) GCC-wall-L. Main. C-o main-lhello

#-L. Indicates searching for the library file in the current directory.-lhello [omitted lib]. In this case, even if you delete the static library libhello. A, you can run the main file [method 2]

 



Library search path

1) Search for the directory specified by-I-l from left to right.

2) directory specified by Environment Variables

You can define c_include_path/cpp_include_path (header file search path) and LIBRARY_PATH (library file search path) to save them in ~ /. Bash_profile. In ubuntu, you can also store these definitions in ~ /. Bashrc

 

3) Directories specified by the system:/usr/include,/usr/lib, etc.

 


Generate shared library

Gcc-shared-FPIC hello_fn.o-O hello_fn.so

 

Note:

1) shared indicates that the shared library is generated.

2)-FPIC indicates generating position independent code)

3) database naming rules: libxxx. So [. Version Number]

 

 


Use shared library

1) GCC main. O-o main-L.-lhello

# This command is the same as the command for using a static library. However, when the shared library and static library coexist, the shared library is preferentially used.

L: Link to the shared library, as long as the Library name (remove lib and version number)

L: path of the Linked Library.

 

2) run the Shared Library

Method 1) copy the. So file to the path of the system shared library, which generally refers to/usr/lib

Method 2) IN ~ /. Configure the LD_LIBRARY_PATH variable in the bash_profile file.

Export LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATH

.~ /. Bash_profile

 

Method 3) Configure/etc/lD. So. conf. After the configuration is complete, call ldconfig to update lD. So. cache.

 

3) LDD command: used to view the shared library to be loaded when the program is running

 


GCC (2) [version 2]

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.