The GCC compiler uses a concise guide (especially how multiple files are compiled)

Source: Internet
Author: User

GCC processing of files requires the steps of the pre-processor-to-compile-by-assembly link, resulting in an executable file that corresponds to different file types, as follows:
FILE.C C program Source files
FILE.I C program after preprocessing file
File.cxx a C + + program source file, or it can be file.cc/file.cpp/file.c++
FILE.II C + + program preprocessing file
File.h/C + + header file
FILE.S Compilation Program Files
FILE.O Target code files

gcc [Options] File list
-ansi enforcing full ANSI consistency
-C compiles or assembles only, generates a target code file, generates. o files for. C,. I,. s, and other files are ignored
-S compiles only, does not compile and link, generates. S files for. C,. I, and other files are ignored
-E is preprocessed only and sends the preprocessed. i file to standard output, and the rest of the files are ignored
-o file creates an executable file and saves it in file instead of the default a.out
-G generates extended symbol tables for debugging and troubleshooting for GDB debugging, remember-G and-o are not usually used together
-W Cancels all warnings
-W gives a more detailed warning
-o [num] optimization, you can specify 0-3 as the optimization level, level 0 indicates no optimization
-X language defaults to-X none, which depends on the suffix name to determine the file type, plus the-X LAN determines all subsequent file types until the next-X appears
-D macro[=] similar to a # define in the source program, the macro in the-D macro can be recognized by the source program, such as gcc-d num-d file=\ "bbs.txt\" Hello.c-o Hello, the first-D option defines the macro NUM, in the program You can use #ifdef to check whether it is set, the second-D defines the macro file, which is available in the source program
-U macro similar to the source program at the beginning of the definition #undef macro, that is, to cancel a macro definition in the source program
-I dir adds the dir directory to the directory of the search header file, and takes precedence over the default search directory in GCC, when there are multiple-I options to search in the order in which they appear
-L dir adds the Dir directory to the directory list of the function library files specified by the search-lname option, and takes precedence over the GCC default search directory, with multiple-l options, in the order in which they appear
-lname using the library Libname.a when linking, the linker looks for the library file under the directory specified by-l dir and/lib,/usr/lib directory, and when the-static option is not used, Dynamic linking with libname.so if a shared function library libname.so is found
-fpic generates location-independent object code that can be used to construct shared function libraries
-static Disable and Share library links
-shared try to link to shared libraries (default)


Here are some examples of using excerpts from some blogs:
Http://hi.baidu.com/eng_cheng/blog/item/37c2f3aee20a79c47dd92ab2.html


1. Compiling a single file
VI hello.c//create source file hello.c
Gcc-o Hello hello.c//compile as executable hello, by default the resulting executable file name is A.out
./hello//Execute file, if only write Hello is wrong, because the system will be the hello when the command to execute, and then error


2. Compiling multiple source files
vi message.c
gcc-c message.c         //Output MESSAGE.O file, is a compiled target code file
VI main.c
gcc-c main.c           //output MAIN.O file
gcc-o all MAIN.O message.o   //Perform the work of the connection phase, and then generate the all executable
./ALL
Note: GCC has built-in rules for how to compile multiple source files into one executable, so several of the preceding individual steps can be simplified to a single command.
VI message.c
VI main.c
gcc-o all message.c main.c
./ All


3. Using an external function library
GCC is often used in conjunction with external repositories that contain standard routines, and almost every Linux application relies on the GNU C function library glibc.
VI trig.c
Gcc-o TRIG-LM trig.c
GCC's-lm option tells GCC to view the math library LIBM provided by the system. Libraries are typically located in the directory/lib or/usr/lib.

4. Shared function library and static function library
Static libraries: Each time the application is compiled with a statically connected library, the code of any referenced library function is directly included in the final binary.
Shared libraries: Contains a single global version of each library function, which is shared across all applications.
VI message.c
VI hello.c
Gcc-c hello.c
Gcc-fpic-c message.c
Gcc-shared-o libmessge.so MESSAGE.O
where 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 the variable, because it is not yet possible to know which address space the application that uses the message code will link to. This compiles the output of the file MESSAGE.O can be used to establish a shared function library. The-shared tag transforms a target code file into a shared function library file.
Gcc-o all-lmessage-l. hello.o
The-lmessage tag tells GCC to use the shared database libmessage.so,-l during the connection phase. The tag tells the GCC library that it might be in the current directory, find the current directory first, or the GCC connector will only look for the System function library directory, in this case, can not find the available function library 。

The GCC compiler uses a concise guide (especially how multiple files are compiled)

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.