It's best to start with the command line at the beginning, so you can get familiar with the whole process from writing, compiling, debugging, and executing. Writing programs can be written in vi or other editors.
Compile using the gcc command. To learn from the first step, you must be familiar with the use of the gcc command.
The gcc command provides a lot of command options, but not all of them are familiar. You can master a few common ones at the beginning of the course, and then slowly learn other options, so as to avoid the confidence of learning because there are too many options.
A. Common compiler command options
Suppose the source file is named test.c.
1. No option to compile the link
Usage: #gcc test.c
Role: pre-test, assemble, compile and link test.c to form an executable file. The output file is not specified here, and the default output is a.out.
2. Option -o
Usage: #gcc test.c -o test
Role: pre-test, assemble, compile and link test.c to form 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: pre-test test.c output test.i file.
4. Option -S
Usage: #gcc -S test.i
Role: compile the preprocessed output file test.i into a test.s file.
5. Option -c
Usage: #gcc -c test.s
Role: Compile the output file test.s to output the test.o file.
6. No option link
Usage: #gcc test.o -o test
Role: Link the compiled output file test.o to the final executable file test.
7. Option -O
Usage: #gcc -O1 test.c -o test
Role: Compile the program with compiler optimization level 1. The level is 1~3. The higher the level, the better the optimization effect, but the longer the compilation time.
2. Multi-source file compilation method
If you have multiple source files, there are basically two ways to compile:
[Assume there are two source files for test.c and testfun.c]
1. Compile multiple files together
Usage: #gcc testfun.c test.c -o test
Role: compile testfun.c and test.c respectively and link to test executable file.
2. Compile each source file separately, and then link to the target file output after compilation.
usage:
#gcc -c testfun.c //Compile testfun.c to testfun.o
#gcc -c test.c //Compile test.c to test.o
#gcc -o testfun.o test.o -o test //Link testfun.o and test.o to test
Compared with the above two methods, the first method requires all files to be recompiled when compiling, and the second method can only recompile the modified files, and the unmodified files do not need to be recompiled.
Gcc programming environment basis 5 - compile time header file and library file path specification
※Preprocessing, compilation, assembly and linking
※include header file, link database, system definition, a total of the following sources specify gcc to find.
Originally specified at compile time (in ~gcc/gcc/collect2.c:locatelib()
Written in the specs
Later specified with -D -I -L
Gcc environment variable setting (when compiling)
Ld.so environment variable (this is the run time)
Header file
How does gcc find the required header files at compile time:
※So the search for header file will start from -I
※ Then find the environment variable C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH
※Revisit 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 you have gcc, there is a given prefix, then
/usr/include
Prefix/include
Prefix/xxx-xxx-xxx-gnulibc/include
Prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include
2. Library file
Cos () and other libraries have more options -lm
When compiling:
※gcc will go to find -L
※ Look for the environment variable LIBRARY_PATH of gcc
※ Look for the default directory /lib /usr/lib /usr/local/lib This is written in the program when compile gcc
2. Utilize existing R&D libraries on Linux systems
(1). View which calls are provided by the library file
Can use the nm command to see which calls are provided by the library file
(2). View the definition of the function through the header file.
3.gcc option
Overall Option: (-c (generate target but not connected) -S (assembly) -E (preprocess) -o file (generate specified file) -pipe -v (display process) -x language (set The language used to locate the file, invalidating the suffix name `c', available parameters `objective-c', `c-header', `c++', `cpp-output', `assembler', and `assembler-with-cpp '))
LANGUAGE OPTIONS: (-ansi only supports ANSI standard C syntax. This option will disable certain features of GNU C)
Preprocessor Option: (-Aassertion -C -dD -dM -dN -Dmacro[=defn] -E -H -idirafter dir -include file -imacros file -iprefix file -iwithprefix dir -M -MD - MM -MMD -nostdinc -P -Umacro Equivalent to #undef macro -undef -DMACRO in C language Defines the MACRO macro with the string "1", -DMACRO=DEFN defines the MACRO macro with the string "DEFN")
Assembler option (ASSEMBLER OPTION) : (-Wa, option)
Connector option (LINKER OPTION): (-llibrary -nostartfiles -nostdlib -static -shared -symbolic -Xlinker option -Wl,option -u symbol )
Directory option (DIRECTORY OPTION) : (-Bprefix -Idir -I- -Ldir)
Warning OPTION: (-w does not generate all warning messages, -Wall generates all warning messages)
DEBUGGING OPTION :(-a -dletters -fpretend-float -g -glevel -gcoff -gxcoff -gxcoff+ -gdwarf -gdwarf+ -gstabs -gstabs+ -ggdb -p -pg -save-temps -print-file-name =library -print-libgcc-file-name -print-prog-name=program )
OPTIMIZATION OPTION: (-O0 is not optimized, -O or -O1 optimizes generated code - O2 is further optimized - O3 is further optimized than -O2, including inline function)
Target option (TARGET OPTION): (-b machine -V version )
Machine-related options (MACHINE DEPENDENT OPTION): (-m486 for code optimization for 486)
Code generation option (CODE GENERATION OPTION) : (-fpic -fPIC)
1, the compiler for programming under linux system
Overview
GCC (GNU Compiler Collection, GNU Compiler Suite) is a programming language compiler developed by GNU. It is a set of GNU compilers
Free software released under the GPL and LGPL licenses is also a key part of the GNU project and is the standard compiler for free Unix-like and Mac OS X operating systems.
GCC was originally called the GNU C language compiler because it could only handle C language. GCC quickly expanded to become C++. It has also become available for Fortran, Pascal, Objective-C, Java, and Ada and other languages.
history
GCC was started in 1985 by Richard Matthew Stallman. He first augmented an old compiler so that it could compile C. The compiler was originally written in the Pastel language. Pastel is a non-portable Pascal language special edition, this compiler can only compile the Pastel language. In order to have a compiler for free software, the compiler was later rewritten by Stallman and Len Tower in C language in 1987 and became the compiler for the GNU project. The founders of GCC are directly managed by the Free Software Foundation.
In 1997, a group of people who were dissatisfied with GCC's slow and closed creative environment organized a project called EGCS (Experimental/Enhanced GNU Compiler System), which brought together several experimental branches into a branch of a GCC project. in. EGCS is more dynamic than GCC's construction environment, and EGCS eventually became the official version of GCC in April 1999.
GCC is currently maintained by several different teams of programmers from around the world. It is the compiler that is ported to the central processor architecture and the most operating system.
Since GCC has become the official compiler for the GNU system (including the GNU/Linux family), it has also become the main compiler for compiling and building other operating systems, including the BSD family, Mac OS X, NeXTSTEP, and BeOS.
GCC is usually the compiler of choice for cross-platform software. Unlike the compilers that are generally limited to specific systems and execution environments, GCC uses the same front-end handler on all platforms, producing the same mediation code, so this mediation code is compiled using GCC on various other platforms. The opportunity to get the correct output program.
structure
The external interface of GCC looks like a standard Unix compiler. The user types gcc's program name in the command line, along with some command parameters, to determine the individual language compiler used for each input file, and uses a combined language compiler suitable for this hardware platform for the output program code, and optionally The linker is executed to make an executable program.
Each language compiler is a stand-alone program that processes the input source code and outputs the combined language code. All language compilers have a common intermediary architecture: a front-end parsing source code that conforms to the language, and an abstract syntax tree, and a translation of the syntax tree into the back end of GCC's register translation language (RTL). Compiler optimization and static code resolution techniques (such as FORTIFY_SOURCE, a compiler that attempts to find buffer overflows) are applied to the program code at this stage. Finally, the combined language code for this hardware architecture is produced by the algorithm invented by Jack Davidson and Chris Fraser.
Almost all GCCs are written in C, except that most of the Ada front ends are written in Ada.
Front end interface
The function of the front end is to generate a syntax tree that can be processed by the back end. This parser is a handwritten recursive parser.
Until recently, the syntax tree structure of the program was not decoupled from the processor architecture to be produced. The rules of the syntax tree are sometimes different in different language front ends, and some front ends provide their special syntax tree rules.
In 2005, two new state-level syntax trees decoupled from language were included in GCC. They are called GENERIC and GIMPLE. Grammar parsing becomes a temporary grammar tree that is related to the language and then turns them into GENERIC. Then use the "gimplifier" technique to reduce the complex structure of GENERIC and become a simple GIMPLE form based on the Static Single Assignment Form (SSA). This form is a globally optimized common language that is decoupled from language and processor architecture and is suitable for most modern programming languages.
Mediation interface
The general compiler author will put the optimization of the syntax tree on the front end, but in fact this step does not look at the language type and does not require a parser. Therefore, GCC authors have included this step in the section known as the intermediary stage. Optimizations of this class include digestion of dead codes, deduplication operations, and global numerical re-encoding. Many optimization techniques are also being implemented.
Backend interface
The behavior of the GCC backend differs depending on the functionality of the different preprocessor macros and the specific architecture, such as different character sizes, call modes, and size tails. The first half of the back-end interface uses these messages to determine the form of its RTL generation, so although the RTC of GCC is theoretically unaffected by the processor, its abstract instructions have been converted to the format of the target architecture at this stage.
GCC optimization techniques vary greatly depending on the release version, but include standard optimization algorithms such as loop optimization, thread jump, common program clause reduction, instruction scheduling, and more. RTL optimization is less important because it is less available and lacks higher-order information. Therefore, it is less important to compare the recently added GIMPLE syntax tree form [2].
After the back end, through a re-reading step, the abstract register is replaced with the real register of the processor by using the information obtained when describing the instruction set of the target processor. This phase is very complicated because it must take into account the specifications and technical details of the processor instruction set of all GCC portable platforms.
The final step of the backend is quite formulated, and only the combined language code obtained in the previous stage is converted into a mechanical code corresponding to the memory location by the simple side function.
basic rules
Part of the conventions followed by gcc:
.c is the suffix file, C language source code file;
.a is a suffix file, which is an archive file composed of object files;
A file with a .C, .cc or .cxx suffix is ​​a C++ source code file;
The .h file is a suffix and is the header file included in the program.
The file with the .i suffix is ​​the C source code file that has been preprocessed;
The .ii file with the suffix is ​​the C++ source code file that has been preprocessed;
The .m is the suffix file, which is the Objective-C source code file;
.o is the file with the suffix, which is the compiled target file;
The .s file with the suffix is ​​the assembly language source code file;
The .S suffix file is a pre-compiled assembly language source code file.
Implementation process
Although we call Gcc a C compiler, the process of generating an executable from a C source code file using gcc is not just a compilation process, but a four-interrelated step: preprocessing (also known as pre-processing) Compilation, Compilation, Assembly, and Linking.
The command gcc first calls cpp for preprocessing. In the preprocessing process, the file include (include) and precompiled statements (such as macro definitions, etc.) in the source code file are analyzed. Then call cc1 to compile. At this stage, the target file with the .o suffix is ​​generated according to the input file. The assembly process is a step for the assembly language, calling as to work. Generally speaking, the assembly language source code file and the assembly of the .s suffix and the assembly language file with the .s suffix are pre-compiled and compiled to generate .o. The target file for the suffix. When all the target files are generated, gcc is called.
We are professional audio manufacturing company that makes a variety of speaker with bluetooth, including bluetooth portable speaker, bluetooth speakers outdoor, small speaker bluetooth, light bluetooth speakers, waterproof speakers etc.
With full turnkey service from product design to delivery, and every step in between.
From sophisticated custom audio systems to 'off-the-shelf' speaker drivers, iTopnoo has been saving our customers time, effort, and money.
To constantly offer clients more innovative products and better services is our consistent pursuit.
Best Portable Speakers,customizable bluetooth speaker, Custom jbl speakers, speaker wholesalers
TOPNOTCH INTERNATIONAL GROUP LIMITED , https://www.micbluetooth.com