Tiny CC plays one of the power of C language: Introduction
content:
TCC introduces TCC greetings TCC, TCC, TCC uses TCC Summary in Shell Pipeline Thank A Reference Information About Authors
In the Linux area:
Tutorial Tools & Product Codes & Component Articles
This article describes the smallest C language compiler Tiny C compiler on the GNU / Linux system. The Tiny C compiler is more than just a conventional sense of C language compiler, which also makes the user can use the C language to make a shortcut program programming like the scripting language. We focus on the charm of scripting developments with C language. This series will consist of three articles, this is the first, introduced; in the second article, we will explain how to use standard C language to complete the work that is typically completed with SED and AWK; in the third one, We will explain how to use TCC as a machine code generator in its own compiler project. TCC introduces here, we say that Tiny C compiler, tiny cc, or TCC refers to the smallest ANSI C language compiler in the GNU / Linux environment invented by this Fabrice Bellar. The TCC's home page is listed in the reference. In the Debian GNU / Linux system, it is convenient to install the TCC compiler from the network with APT-GET Install TCC. The RPM package provided on the Red Hat system is available on the home page of the TCC. In a Microsoft Windows environment, you can compile and use the Tiny C compiler using CygWin's analog UNIX development environment. TCC is free software, software license is GNU LGPL, notice that it is not GPL. The most interesting feature of TCC is the source program that can perform the ANSI C language written in the Unix system, and omitted the steps to compile and link on the command line, but Run the source program that is written directly in the C language. This will achieve significant speed development steps like any other scripting language such as Perl or Python. You can use the C language like writing a shell script, just think about it, I think it is a wonderful thing. But TCC has some other features!
The TCC is very small, and all source code packages less than 200 k bytes after compression, and the compiled TCC executor is only 80 k bytes. This means that we can almost use TCC to use TCC to provide our ability to write C language scripts in any occasion. This of course, includes a highly tight environment, such as an embedded system and a boot floppy disk, and the like. Any dynamic link library that can be used in the source program to GCC can be used in the source program to TCC. TCC also supports standard ANSI C languages, but also supports ISO C99 standards and part from GCC's expansion of C language. TCC directly generates partially optimized X86 machine code. There is no need to generate a binary code for any virtual machine. According to the data provided by TCC, the compilation speed of TCC is faster than the GNU C compiler does not do any code optimization (GCC -O0). Of course, if you let GCC do code optimization, then compile speed is more than TCC. Below we use several examples to explain the convenience of TCC. TCC greetings here is a simple C language Hello, World! Program. We use this classic greetings to make a simple comparison for GCC and TCC.
-------- 8 <------- Here is Hello.c
#include
INT main (int Argc, char ** argv)
{
Printf ("Hello, World! / N");
Return 0;
}
--------> 8 -------- or more is Hello.c to compile with gcc -wall -o2 -o hello hello.c on the command line, then strip hello put the executable The debugging information in the middle is deleted. We get a 2592-byte size executable on Debian GNU / Linux. As a comparison, we can run this classic greeting program directly with Tcc -Run Hello.c on the command line:
-------- 8 <------- The following is the console information
$ tcc -run hello.c
Hello, World!
$
--------> 8 -------- or more is the console information
The program reaches the same operational effect, but the corresponding hard disk space required is only 95 bytes occupied by Hello.c. On the other hand, we can also use Tcc -o Hello Hello.c like a Tcc -o Hello Hello.c like a GCC, and then run. After using Strip Hello on the command line, we deleted the debug information. We got a 1724-byte size executable file in the same debian GNU / Linux system, and 2592 bytes received by GCC were a lot. . In order to give full play to the ability of the TCC's writing C language script, we can also add the traditional joose of the UNIX scripter on the first line of the source file of this file:
#! / usr / bin / tcc -run
The above / usr / bin / tcc is the installation path of the TCC on the Debian GNU / Linux system. If you are the TCC source code you downloaded, the TCC installation path on your system will definitely be different, then Of course, it is necessary to make a corresponding change here. After joining this line, we at the command line CHMOD A X Hello.c makes hello.c into an executable. This way we can run the Hello.c. As we can run the shell script directly on the command line or use the script with Perl and Python:
-------- 8 <------- The following is the console information
$ CAT Hello.c
#! / usr / bin / tcc -run
#include
INT main (int Argc, char ** argv)
{
Printf ("Hello, World! / N");
Return 0;
}
$ ./hello.c
Hello, World!
$
--------> 8 -------- or more is the console information
As so far here, TCC allows us to omit the steps of compiler and links over and over again when using C language programming, which is really very convenient. TCC, TCC, TCC, in this section, let us play a small multi-nest TCC game. First, how the TCC is passed to the application to the application. Enter tcc -run program.c on the command line Shell to run the Program.c with the pro.c. If we need to pass the main (int Argc, char ** argv) function inside the command line, we need to enter the command line to enter the command line to enter a command to enter the command line; Thus, Arg1 and Arg2 are passed by the TCC to the main () function in the Program.c program. After learning this, let's start our small game. The first is to run TCC -V directly on the command line shell to output TCC version information. And do the test assessment of the system run time. The TCC command here is a TCC command installed under the / usr / bin directory on the Debian GNU / Linux system. For details on the Time command, readers can refer to the corresponding Manual Page page. -------- 8 <------- The following is the command line running record
$ TIME TCC -V
TCC Version 0.9.19
Real 0m0.019s
User 0m0.001s
SYS 0M0.017S
--------> 8 -------- or more is the command line operation record
Next, let the TCC running the TCC.C. This is the primary file of the source program of the TCC's C language written, where there is a main () function This C language entry function. The file TCC.c is located in the TCC compression package downloaded from the TCC home page. Related links are listed in the article. Pack the downloaded TCC compression software to open it under the corresponding directory, enter the directory, you can continue our small game as follows. Obviously, the time required for the program runs long. But let TCC run yourself, readers don't think it is interesting? Below we can also see more interesting things.
-------- 8 <------- The following is the command line running record
$ TIME TCC -Run Tcc.c -V
TCC Version 0.9.19
Real 0m0.385s
User 0m0.147s
SYS 0M0.178S
--------> 8 -------- or more is the command line operation record
It seems that it is not fun enough to let TCC yourself, we have to let such nesting deeper. First, explain the TCC -b option setting TCC to find the path to the library file; -i option Sets the path to the C language header file. In the Debian GNU / Linux system, the related library files and C language head files are installed in the path location shown in the following command. The system installed TCC can naturally find these positions. But TCC.c running from the source file requires us to tell where these files are.
-------- 8 <------- The following is the command line running record
$ TIME TCC -RUN /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -v
TCC Version 0.9.19
Real 0m0.793s
User 0m0.463s
SYS 0M0.249S
--------> 8 -------- or more is the command line operation record
Now we can see that such nesters can almost do nothing to do. Let us go deep into two floors, end our small game.
-------- 8 <-------- The following is the command line running record $ TIME TCC -RUN /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -v
TCC Version 0.9.19
Real 0m1.427s
User 0m0.844s
SYS 0M0.406S
--------> 8 -------- or more is the command line operation record
Let's take a layer of nested:
-------- 8 <------- The following is the command line running record
$ TIME TCC -RUN /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -i / usr / lib / tcc / include -b / usr / lib / tcc -run /
> tcc.c -v
TCC Version 0.9.19
Real 0m2.381s
User 0m1.167s
SYS 0M0.664S
--------> 8 -------- or more is the command line operation record
I want some "diligent" readers who don't help but ask, what is the meaning of this small game? My answer is this: The above little game is intended to show that the small thing is very flexible. On the one hand, you can explain the code quality of TCC; on the other hand, I believe that you can convince readers to agree that small and flexible things can often have unexpected wonderful performances. Using TCC TCC in the shell pipe to provide us with C language to program the ability to program, but to maximize the potential of scripting programming, we need to make TCC's script and other in the environment of the command line shell. The command line shell tool can work closely. The way to cooperate with several tools in the Unix command line is the pipeline mechanism of the shell we are well known. Let's take a look at how TCC and Shell's pipeline mechanism cooperate. There are two aspects: one of the TCC and Shell pipelines: First, how the TCC compiler itself uses the pipe; second, the C language scriptor written in TCC uses pipes. Let's first look at how the TCC compiler itself uses the shell pipe. The common way to handle the pipe input on the GNU / Linux system is to allow the command line program to handle special minus (-) as a command line parameter. Originally, you need to read the command line program for input data from a file. After receiving this minus, it is changed to read data from the standard input (stdin). This allows you to cooperate with the pipe mechanism of the shell. However, this minus sign is not processed in the current TCC version 0.9.19 version as the command line parameters. However, we can have an alternative way to use the / dev / stdin device file on the GNU / Linux system. The following test is made on the Debian GNU / Linux system. On the Debian GNU / Linux system / dev / stdin is actually a symbolic link to / dev / fd / 0; and the latter is a symbolic link to / dev / PTS / 0. If you don't / dev / stdin on your GNU / Linux system, you can use / proc / self / fd / 0 instead.
-------- 8 <------- The following is the command line running record
$ CAT Hello.c | TCC -Run / dev / stdin
Hello, World!
--------> 8 ------------------------------------------------------------------------------------------------ Here we see how TCC coordinated operations with the Pipe mechanism of the SHELL in GNU / Linux. Let's take another example of nested TCC.c. Document TCC.c has nearly 300 k bytes of byte, and only 50 k bytes will be left after compression with BZIP2. We now know how to use the shell pipeline, you can run TCC like this. This will save a lot of space in the case where the storage space is more tense.
-------- 8 <------- The following is the command line running record
$ bzcat tcc.c.bz2 | tcc -i. -b / usr / lib / tcc -run / dev / stdin -v
TCC Version 0.9.19
--------> 8 -------- or more is the command line operation record
Know how the TCC compiler supports the pipe function of Shell, we can easily make a variety of preprocessors required for our C language script. This convenient support for pre-processing of program source files, which is extremely convenient for our C language software. Tell the TCC compiler itself how to support the shell pipe, how to use the C language script written in TCC how to support the shell pipe. This is actually very simple, just use standard input (stdin) and standard output (stdout) in your C language scripter. Let's take a simple example.
-------- 8 <------- Here is dup.c
#include
INT main (int Argc, char ** argv)
{
INT C;
While ((c = fgetc (stdin))! = EOF) {
FPUTC (C, STDOUT);
FPUTC (C, STDOUT);
}
Return 0;
}
--------> 8 -------- is dup.c
This program is simple, it repeats each character passed from standard input, and then passes to the standard output. Let's take a look at its operation:
-------- 8 <------- The following is the command line running record
$ Echo "a" | TCC -Run Dup.c
AA
$ ECHO "a" | TCC -Run Dup.c | SED -E 'S / A / B /'
BA
$ ECHO "a" | TCC -Run Dup.c | SED -E 'S / A / B / G'
BB
--------> 8 -------- or more is the command line operation record
Although this example is simple, we can use TCC to do some simple string processing. The work of these string processing is usually done with tools such as awk and SED. Now with TCC tools, we can also use C language to complete. In this series of next article, let's take a look at this example. Small junction TCC's small and flexible characteristics allow we can program a script programming in such a C language such as installation floppy disk, first aid floppy disk, and micro GNU / Linux system. In small and medium-sized C language projects, the TCC is used in the development phase, and the steps of compilation and linking can be exempted to speed up the speed of the test. These two points are the main benefits of TCC bring us. In addition, I think readers will also agree to use C language to conduct script programming, which is really a very interesting thing. Thank you for your Fabrice Bellard brings us a wonderful tool such as TCC. In addition, the author is also grateful to Fabrice Bellard and Damian M Gryski to provide the author's help on the Tinycc-Devel mailing list. Reference TINY CC's home http:/fabrice.bellard.free.fr/TCC/ Debian TCC package home page http://packages.debian.org/unstable/devel/tcc.html Cygwin is a Microsoft Windows environment Most of the simulated UNIX development and use environments, most of the major GNU kits have version under CygWin. You can use TCC in a Cygwin environment. Cygwin's homepage at http://www.cygwin.com