GCC of Linux development tools

Source: Internet
Author: User

Linux Development Tools I. GCC primer (top) 1.GCC related concepts gcc (GNU c Compiler) compiler, originally supported in C language, now supports C, C + +, Java,Pascal, Ada, COBOL language, etc. support a variety of hardware platforms;GCC is more than just a local editor, he can cross-build cross-platform, GCC has multiple language front-end for parsing different languages; GCC is a modular design that can add support for new languages and new CPU architectures; GCC is free software; 2.GCC Compiler procedure Hello.c--> Preprocessing (CPP) hello.i--> compiler (CCL) hello.s--> Assembler (as) hello.o,printf.o--> Linker (LD) Hellosource program (text), modified source program (text), assembler (text), relocatableProgram (binary) executable target program (binary)#gcc-E (expansion of preprocessing macro definitions) Hello.c-o hello.i (target file) #gcc-S (compile) Hello.i-o hello.s#gcc-c (assembler) Hello.s-o hello.o (notifies GCC to unlink step, which compiles the source code andin the final generation of the target file)#gcc hello.o-o Hello#./hellohello once completed #gcc Hello.c-o hello#gcc-c hello.c (only to the third step, do not join the link) 3.-wall use, prevent the expected error, error alert #gcc- Wall Hello.c-o Hello correct case not prompt, error reminder 4. Modular compilation: #include <stdio.h>void hellofn (), void Main (), two compile links for function files #gcc-wall HELLOFN.C Main.c-o Man#lsmain#./mainhello can also be used to generate a link file for two files, which will be delivered to the target file #gcc-wall-c hellofn.c-o hellofn.o#gcc-wall-c ma In.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, and if there are many, independently compiled files in the project, You need to set up a makfile script to make the big rulesthe compilation of modules;Summary: GCC's concept GCC features GCC compilation process gcc common options gcc compilation multi-file second, GCC introduction (II.) 01. header Files and library files when programming in C and other languages, we need header files to provide a constantand a declaration of the system function library call. A library file is a set of pre-compiled functions that are written according to the principle of reusability.They are usually composed of a set of functions that are associated with a common task. For example, to handlea function (ncurses library) and a database access routine (dbm Library) are displayed on the screen. 02. Use the 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): The program links the library's code to the executable when it compiles the link. RideThe runtime will no longer need a static library. Dynamic library (. So or. sa): Programs are run to link shared code, and multiple programs sharecode that uses the library. An executable file linked to a shared library contains only a table of the function entry addresses it uses, not the entire machine code of the destination file where the external function resides. Before the executable starts running, the machine code for the external function has the operating system from the diskThe shared library is copied into memory, a process called dynamic linkingShared libraries can be shared among multiple programs, and all dynamic links use smaller executables, sectionsaves disk space. The operating system uses a virtual memory mechanism to allow a shared library in physical memory to beall processes that are used in the library are common, saving memory and disk space. 04. Generate the 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#./mainhel Lo World!!! #gcc-wall main.o-o Main-lhello (Libhello can be abbreviated as Lhello)/usr/bin/ld:cannot find-lhello#gcc-wall-l. (Find Lhello This library under current path) After the link is delivered to the main executable file, the LHELLO.A static library file can be deleted #rm Lhello.a#./mainhello world!!! #vi ~/.bash_profileexport Library PATH =/(Specify environment variables to search for libraries in this directory) 05. Build Shared library Share: Represents the generation of shared library formats Fpic: Generating location-independent codes (position Independent) #gcc-shared-fpic hello_fn.o-o libhello.so#lslibhello.so#gcc-wall-l. Main.o-o Main-lhello (when the static library and the shared library exist simultaneously, Priority call to Shared Library) #cp Main/tmp#./main./main error: ... The first method: #cp Libhello.so/usr/lib#./mainhello World!!! The second method: Change Ld_library_path (current user) #vi ~/.bash_profile increase the file path third method: Ldconfig, configure Ld.so.conf,ldconfig update ld.so.cache#vi/etc /ld.so.conf (Modify Path) #ldconfig (Update command) #ldd main (which libraries the main program contains) #ldd hello (which libraries are included with Hello) Summary: Using external libraries:-lhello static libraries and shared libraries: three ways to generate a static library to generate a dynamic library

GCC of Linux development tools

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.