Gcc for Linux development tools and gcc for development tools

Source: Internet
Author: User

Gcc for Linux development tools and gcc for development tools
Linux development tool 1. gcc entry (I) 1. gcc-related concepts gcc (gnu c Compiler) Compiler, initially supporting C language, now supports C, C ++, Java, Pascal, Ada, COBOL language, etc.; supports a variety of hardware platforms; gcc is not only a local editor, but also cross-platform cross-Compiling. gcc has multiple front-ends for parsing different languages. gcc is a modular design, support for new languages and new CPU architectures can be added; gcc is a free software; 2. gcc program compilation process hello. c --> pre-processing (cpp) hello. I --> Compiler (ccl) hello. s --> assembler (as) hello. o, printf. o --> linker (ld) hello source program (text)-> modified source program (text)-> Assembler Program (text)-> relocated Target Program (Binary) -> executable Target Program (Binary) # gcc-E (expansion of pre-processing macro definition) hello. c-o hello. I (target file) # gcc-s (Compilation) Hello. i-o hello. s # gcc-c (assembly) hello. s-o hello. o (notify gcc to cancel the link, that is, compile the source code and generate the target file at the end) # gcc hello. o-o hello #. /hellohello completed once # gcc hello. c-o hello # gcc-c hello. c (only step 3, do not join the link) 3. -Wall usage to prevent unexpected errors # gcc-Wall hello. if c-o hello is correct, no prompt is displayed. Error reminder 4. modular Compilation: # include <stdio. h> void hellofn (); void main (); Compilation link of the two function files # gcc-Wall hellofn. c main. c-o man # lsmain #. /mainhello can also be used to generate a link file for both files. The link is used to generate the target file # gcc-Wall-c hellofn. c-o Hellofn. o # gcc-Wall-c main. c-o main. o # gcc-Wall hellofn. o main. o-o main # lsmain #. /mainhello if one of the files changes, you only need to compile a part of it. If there are many files in the project that are compiled independently, you need to create a makfile script for large-scale compilation. Summary: gcc concepts gcc features gcc compilation process gcc common options gcc compilation multi-file II. gcc entry (below) 01. 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 function library calls. Library files are a set of pre-compiled functions and which functions are written in accordance with the reusable principle. They are usually composed of a group of associated functions used to complete a common task. For example, functions (ncurses Library) and database access routines (dbm database) used to handle screen display. 02. use external library (-l option) // calc. c # include <math. h> # include <stdio. h> int main (void) {double x = pow (2.0, 3.0); printf ("The cubed is % f \ n", x); return 0 ;} # gcc-Wall calc. c-o calc-lm # ls/usr/lib/libm. a # ls/usr/lib/libm. so03. static library and shared library static library (. a): When the program compiles the link, it links the library code to the executable file. Static libraries are no longer needed when the program is running. Dynamic library (. so or. sa): the shared code is linked only when the program is running. Multiple programs share the code of the used library. 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. All Dynamic Links use smaller executable files, 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. 04. generate static library ar (gnu archive tool) rcs (replace and create) for example: # ar rcs libhello. a hello_fn.o # gcc-Wall main. c libhello. a-o main #. /mainhello world !!! # Gcc-Wall main. o-o main-lhello (libhello can be abbreviated as lhello)/usr/bin/ld: cannot find-lhello # gcc-Wall-L. (find the library lhello in the current path) after the link generates the main executable file, lhello. a static library file can be deleted # rm lhello. a #. /mainhello world !!! # Vi ~ /. Bash_profileexport library path =/(specify the environment variable and search for the database in this directory) 05. generate shared library shared: generate the shared library format fPIC: Generate position independent # gcc-shared-fPIC hello_fn.o-o libhello. so # lslibhello. so # gcc-Wall-L. main. o-o main-lhello (when both the static library and the shared library exist, the shared library is called first) # cp main/tmp #. /main. /main error :...... Method 1: # cp libhello. so/usr/lib #./mainhello world !!! Method 2: Change LD_LIBRARY_PATH (current user) # vi ~ /. Bash_profile: add the file path. Method 3: ldconfig and configure ld. so. conf, ldconfig update ld. so. cache # vi/etc/ld. so. conf (modify path) # ldconfig (update command) # ldd main (which libraries are included in the main program) # ldd hello (which libraries are included in hello) Summary: use external libraries: -lhello static library and Shared Library: three methods are used to generate static library and dynamic library.

 

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.