ARM-LINUX-GCC common parameters Explain how GCC compilers are used

Source: Internet
Author: User

to find convenience, move forward from all the way.We need to compile the code that runs on the arm platform, UseThe Cross compiler for ARM-LINUX-GCC。 Below will ARM-LINUX-GCCSome common command parameters of the compile tool are introduced to everyone.
Before you do this, you'll first describe the compiler's working procedure, UseWhen GCC compiles a program, the compilation process is divided into four stages:
1. pretreatment (pre-processing)
2. Compiling (compiling)
3. Compilation (assembling)
4. Links (linking)
Linux programmers can make GCC end at any stage of the compilation, depending on their needs, in order to check or UseThe compiler outputs information at this stage, or controls the last generated binaries, in order to prepare for future debugging by adding different quantities and kinds of debug code. Like other commonly used compilers, GCC provides flexible and powerful code optimization capabilities that can be used to generate more efficient code execution.

Take file example.c as an example to illustrate its usage
0. ARM-LINUX-GCC -o example example.c
Without the-C,-S,-e parameters, the compiler will perform preprocessing, compilation, assembly, and connection operations to generate executable code directly.
The-o parameter is used to specify the output file, the output file name is example, and if the output file is not specified, the default output a.out

1. arm-linux-gcc -c-o example.o example.c
The-c parameter will preprocess, compile, and assemble the source program example.c, generating the EXAMPLE.O file
Remove the specified output option "-O example.o" auto output as EXAMPLE.O, so that here-o plus no plus can

2.arm-linux-gcc -s-o example.s example.c
The-s parameter will preprocess, compile, and generate EXAMPLE.S files for the source program example.c
-O option ditto

3.ARM-LINUX-GCC -e-o example.i example.c
The-e parameter will preprocess the source program example.c, generate the Example.i file (different versions, some will print the preprocessed content to the screen)
is to include, #define等进行文件插入及宏扩展等操作.

4.ARM-LINUX-GCC -v-o Example example.c
Plus the-v parameter, which displays compile-time details, compiler version, compile process, etc.

5.ARM-LINUX-GCC -g-o Example example.c
-G option to add debug information gdb can use , debugging with gdb is convenient.

6.ARM-LINUX-GCC -wall-o Example example.c
The-wall option opens all warning messages that need to be noticed, such as functions that were used before the declaration, variables that were not used after the declaration, and so on.

7.ARM-LINUX-GCC -ox-o Example example.c
-ox using the optimization option, the value of x is null, 0, 1, 2, 3
0 is not optimized, the purpose of optimization is to reduce the code space and improve execution efficiency, etc., but the corresponding compilation process time will be longer and occupy a large amount of memory space.

8.ARM-LINUX-GCC -i/home/include-o Example example.c
-idirname: Add the directory indicated by DirName to the program header file directory list. If the required files are not found in the default system and the current directory, go to the specified dirname directory to find them.

9.ARM-LINUX-GCC -l/home/lib-o Example example.c

-ldirname: Add the directory indicated by dirname to the directory list of the library file. By default, the connector LD looks for the desired library file in the system's default path (such as/usr/lib), which tells the connector to look in the directory specified by-L and then to the system's default path.

Ten.arm-linux-gcc –static-o libexample.a example.c

Static link library file

Several of the options that GCC uses frequently on the command line are:
-C only pre-process, compile, and assemble source programs without connection. The compiler produces a target file for each source program.

-o file to determine the output files to If the-o option is not used, the output of the default executable is a.out, the output of the target file and the assembly file to Source.suffix are SOURCE.O and SOURCE.S respectively, and the output of the preprocessed C source program is the standard output stdout.

The-dmacro or-dmacro=defn function is similar to the # define in the source program. For example:% gcc-c-dhave_gdbm-dhelp_file=\ "Help\" cdict.c where the first-D option defines a macro have_gdbm, in which you can use #ifdef to check whether it is set. The second-D option defines the macro Help_file as the string "Help" (the quotation marks are actually part of the macro definition as a result of the backslash), which is useful for controlling which file to open.

-umacro Some macros are automatically defined by the compiled program. These macros can often specify symbols for the type of computer system in which they are compiled, and users can add the-v option when compiling a program to see which macros are defined by GCC by default. If the user wants to cancel one of the macro definitions, use the-UMACRO option, which is equivalent to placing the #undef macro at the beginning of the source file to be compiled.

-idir adds the Dir directory to the directory list of the search header file and takes precedence over the default search directory in GCC. In the case of multiple-I options, search by the order of the-I option on the command line. Dir can use a relative path, such as-I.. /inc and so on.

-O to Optimize program compilation, the compiler tries to reduce the length and execution time of the compiled program, but its compilation speed is slower than optimization, and requires more memory.

-o2 allows for better optimizations than-O, which is slower to compile, but results in faster execution of the program.

-G produces an extended symbol table for debugging and troubleshooting. The-G option allows the program to debug with the GNU Debug program GDB. Optimizations and debugs are often incompatible, and using the-G and-O (-O2) option often causes the program to produce strange running results. So do not use the-G and-O (-O2) options at the same time.

-fpic or-fpic produces location-independent object code that can be used to construct a shared function library.

The above is the GCC compilation option. Connection options are also available on the GCC command line. In fact, GCC passes all unrecognized options to the connector ld. The connector LD combines several target files and library programs into an executable file that resolves references to external variables, external procedures, library programs, and so on. But we never need to explicitly invoke LD. It is very simple to use GCC commands to connect individual files, even if the library program is not listed on the command line, GCC can guarantee that some libraries will appear in the correct order.

The common connection options for GCC are the following:
-ldir adds the Dir directory to the directory list of the function library files specified by the search-l option and takes precedence over the GCC default search directory. In the case of multiple-l options, search by the order of the-l option on the command line. Dir can use relative paths. such as-L. /lib and so on.

-lname Use the function library Libname.a when connecting, the connector looks for the library file under the directory specified by the-ldir option and in the/lib,/usr/lib directory. When you do not use the-static option, if you find a shared function library libname.so, you use libname.so for dynamic connection.

-static prohibit connections to shared libraries.

-shared try to connect to a shared library

ARM-LINUX-GCC common parameters Explain how GCC compilers are used

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.