Play tinycc (aka TCC)

Source: Internet
Author: User

TCC is a lightweight C language compiler. According to the official introduction, TCC fully supports ansi c and can even be used to compile the Linux kernel. The most notable feature of TCC is its fast Compilation speed, small executable programs, and the ability to execute C code like a script. Unfortunately, the author does not seem to have developed TCC any more. The latest version is 0.9.25.

 

The usage of TCC is basically the same as that of GCC, and the default executable program name is A. Out. Write a hello World command to compare the executable program size generated by GCC and TCC.

Hello. c

#include <stdio.h>int main(void){    printf("hello world.\n");    return 0;}

GCC hello. C-o G

TCC hello. C-o t

Result:

We can see that the size of the executable program generated by GCC is nearly three times that of TCC. Does the executable program generated by GCC contain a large amount of debugging symbols? Then we use strip to slim down the G and T respectively, and then look at the size.

The Strip cannot slim down the T files compiled by TCC and reports a bad value error. In any case, even if the size of the executable program compiled by TCC is more than twice the size of T, it can be seen that the size of the executable program compiled by TCC does have some advantages, this advantage is more evident in embedded devices. The less binary code the execution speed may be faster, but this requires actual testing.


Try to use TCC to script the C code and use the above hello. C program. TCC can directly run the C code without compiling the source code as an executable program. The method is as follows:

TCC-run Hello. c

The "Hello World" text is also output, but no intermediate files are generated, such as hello. O and A. Out. It is running like a script. You may think that it would be nice to execute the hello. c file directly, just like the shell script. I'm glad to tell you that TCC can do this.

#!/usr/bin/tcc -run#include <stdio.h>int main(void){    printf("hello world.\n");    return 0;}

You only need to add "#! /Usr/bin/TCC-run ", add executable permissions to the source file, and run./Hello. C like a common command. The result is Hello world.

Maybe you still think that although the hello. c file can be run directly, there is a. c suffix after all. In fact, you can remove the Suffix from MV hello. c hello and run the same job. It looks more like an executable file in ELF format. At this time, TCC has helped you complete the dream of scripting in C language.

It is absolutely suspected that TCC is used to compile C code in script mode. However, if you want to write a small program and need to modify and debug it repeatedly, it is much easier to directly execute it using TCC, so you do not need to compile it repeatedly and link the process.

However, the advantage of TCC is mainly reflected in the rapid compilation. After all, for a slightly larger project, GCC needs to be compiled for a while. A joke is that "Why are programmers so idle? Because it is being compiled ". As for the efficiency of executable programs generated by TCC, you still need to use a large project to test it.


TCC details: http://bellard.org/tcc/tcc-doc.html


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.