Compile the c program under the command line in linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. C is the system language of all versions of UNIX.
C has made great development in the past two decades. at the end of 1980s, the American NationalStandards Institute released a C language standard called ansi c. this ensures the consistency of C on different platforms in the future. in 1980s, a C-oriented extension called C ++ emerged.
The available C compiler on Linux is the gnu c compiler. It is based on the programming license of the Free Software Foundation, so it can be freely released. You can find it on the Linux release CD.
The gnu c compiler (GCC) released with Slackware Linux is a fully functional ansi c compatible compiler. if you are familiar with a C compiler on other operating systems or hardware platforms, you will be able to quickly master GCC. this is similar to java in the command line.
Compilation is similar. We will introduce how to use GCC and some of the most common options of GCC compiler below.
Format: gcc [option] [sourcefilename]
There are a wide variety of gcc options, about, but not many are commonly used at ordinary times. We will introduce some frequently used options:
The simplest is: gcc hello. c
The executable file of a. out is generated by default. You only need to enter./a. out on the terminal to view the execution result.
If you want to specify the name of the target file to be generated, add the-o option and run the following command:
Gcc-o hello. c
Gcc also allows you to compile only the source program. In this way, you can only check the errors at the time of compilation and debug the program. You can use the-c option to achieve this purpose.
The command is as follows:
Gcc-c hello. c
An intermediate code hello. o file is generated.
The compilation option tells GCC to stop compiling after an assembly language file is generated for the C code. the default extension of assembly language files generated by GCC is. s. -E indicates that the compiler only processes the input file. when this option is used, the pre-processor output is sent to the standard output instead of stored in the file.
Then you can link the above intermediate code and run the command:
Gcc-o hello. out hello. c
Optimized items
When you use GCC to compile C code, it will try to complete the compilation at least and make the compiled code easy to debug. easy debugging means that the compiled code has the same execution order as the source code, and the compiled code has not been optimized. there are many options that can be used to tell GCC to generate smaller and faster executable files at the expense of more Compilation Time and easier debugging. the most typical options are-O and-O2.
The-O option tells GCC to optimize the source code. in most cases, these optimizations make program execution faster. -The O2 option tells GCC to generate code as small as possible and as fast as possible. the-O2 option will make compilation faster than-O. however, code execution is usually faster.
GCC supports several debugging and profiling options. Among these options,-g and-pg options are most commonly used.
Option tells GCC to generate debugging information that can be used by the GNU Debugger to debug your program. GCC provides a feature that is not available in many other C compilers. in GCC, you can combine-g with-O (to produce optimization code. this is useful because you can debug your code as close as possible to the final product. when you use these two options at the same time, you must be aware that some of your code has been modified by GCC during optimization. for more information about debugging C Programs, see the next section "Debug C Programs with gdb ".
Command gcc-c-g hello. c
The pg option tells GCC to add additional code to your program. During execution, gprof profiling information is generated to show the time consumption of your program.
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