GCC Summary "Basic usage" "option" "Dynamic Static Library"

Source: Internet
Author: User

1.///////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////

Linux under C programming start one: Master gcc basic usage

It's best to start with the command line, so you can familiarize yourself with the whole process of writing, compiling, debugging, and executing. Writing programs can be written in vi or other editors.

Compilation uses the GCC command. To learn first you have to familiarize yourself with the use of the GCC command.

The GCC command provides a lot of command options, but not all of them are familiar with the first class to master a few commonly used on it, and then slowly learn other options, lest because too many options to attack the confidence of learning.


A. Common compilation command options

Suppose the source program file name is test.c.


1. No option to compile the link

Usage: #gcctest. c

Role: TEST.c preprocessing, assembly, compiling and linking to form executable files. The output file is not specified here, and the default output is a.out.


2. Option-O

Usage: #gcctest. C-o test

Role: TEST.c preprocessing, assembly, compiling, and linking to form the executable file test. The-o option is used to specify the file name of the output file.


3. Option-E

Usage: #gcc-e test.c-o test.i

Role: test.c preprocessing output test.i file.


4. Option-S

Usage: #gcc-S TEST.I

Function: test.i the preprocessing output file into Test.s file.


5. Option-C

Usage: #gcc-C TEST.S

Role: Compile output file Test.s compiler output TEST.O file.


6. No selection link

Usage: #gcctest. O-o test

Function: Link the compiled output file TEST.O to the final executable file test.


7. Option-O

Usage: #gcc-o1 test.c-o Test

Function: Compile the program with compile-level 1. Level is 1~3, the higher the level the better the optimization effect, but the longer the compilation time.


Two. Multi-source file compilation method


If you have more than one source file, there are basically two ways to compile it:

[Assuming there are two source files for TEST.c and TESTFUN.C]


1. Compile with multiple files

Usage: #gcctestfun. C Test.c-o Test

Role: TESTFUN.C and TEST.c are compiled and linked to the test executable file respectively.


2. Compile each source file separately, and then link to the target file after the compiled output.

Usage:

#gcc-C TESTFUN.C//TESTFUN.C compiled into TESTFUN.O

#gcc-C test.c//test.c compiled into TEST.O

#gcc-o testfun.o test.o-o Test//link TESTFUN.O and TEST.O to test


Compared to these two methods, the first method compiles with all the files, and the second method compiles the modified files only, and the unmodified files do not need to be recompiled.


GCC Programming Environment Foundation 5--compile-time header file and library file path designation


※ Preprocessing (preprocessing), compilation (compilation), assembly (assembly) and connection (linking)
※include header file, link database, System definition, A total of the following sources specify GCC to go there and find it.
The environment variable that was specified at compile time (in ~gcc/gcc/collect2.c:locatelib ()
, which is written in specs,
later with the
GCC environment variable specified by-d-i-l (Compile)
ld.so ( This is the time of the runtime)
=======================================
  1
=======================================
1. header file
gcc How to find the required header file at compile time:
※ So Headerfile's search will start with-I
※ Then find GCC environment variable C_include_path,cplus_include_path,objc_include_path
※ then find the default directory
/usr/include
/usr/local/ Include
/usr/lib/gcc-lib/i386-linux/2.95.2/include
/usr/lib/gcc-lib/i386-linux/2.95.2/... /.. /.. /.. /include/g++-3
/usr/lib/gcc-lib/i386-linux/2.95.2/... /.. /.. /.. /i386-linux/include
Library file But if there is a given prefix when you install GCC, then it is
/usr/include
Prefix/include
prefix/ Xxx-xxx-xxx-gnulibc/include
Prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include

2. library files
cos () The options for function libraries are more-LM
Compile the time:
※GCC will find L.
※ Find GCC's environment variable Library_path
※ Find the default directory/lib/usr/lib/usr/local/lib This is the original COMPILEGCC written in the program
=======================================
2
=======================================

2. Use the existing research and development library on Linux system
(1). View what calls the library file provides
You can use NM to command yourself to see what the library file provides
(2). View the definition of a function through a header file
=======================================
3
=======================================
3.GCC Options
Overall option (Overalloption): (-C (Generate target but not connected)-s (assembly)-E (preprocessing)-ofile (generate specified file)-pipe-v (display procedure)-xlanguage (the language in which the file is set to invalidate the suffix name ' C ', which can be With parameters ' objective-c ', ' c-header ', ' C + + ', ' cpp-output ', ' assembler ', and ' assembler-with-cpp ')
Language Options (languageoptions): (-ansi only supports the ANSI standard C syntax. This option will prohibit certain features of GNU C)
Preprocessor Options (preprocessoroption): (-aassertion-c-dd-dm-dn-dmacro[=defn]-e-h-idirafter dir-include File-imacros Fix File-iwithprefix dir-m-md-mm-mmd-nostdinc-p-umacro equivalent to #undefmacro-undef-dmacro in the C language defines macro macros with the string "1",-dmacro=de FN defines macro macros As string "DEFN"
Assembler Options (assembleroption): (-wa,option)
Connector options (linkeroption): (-llibrary-nostartfiles-nostdlib-static-shared-symbolic-xlinker option-wl,option-u Symbo L
Catalog options (directoryoption): (-bprefix-idir-i--ldir)
Warning Option (warningoption): (W does not generate all warnings,-wall generates all warning messages)
Debug Options (Debuggingoption): (-a-dletters-fpretend-float-g-glevel-gcoff-gxcoff-gxcoff+-gdwarf-gdwarf+-gstabs-gsta bs+-ggdb-p-pg-save-temps-print-file-name=library-print-libgcc-file-name-print-prog-name=program)
Optimization Options (optimizationoption): (-o0 does not optimize,-O or-o1 optimize generation code-O2 further optimize-O3 for further optimization, including-O2 functions)
Target machine option (targetoption): (-bmachine-v version)
Machine-related options (machinedependent option): (-m486 for

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.