TCC Research (2) the C language as a script, interpreting execution, and embedding various programs

Source: Internet
Author: User
Tags function prototype

Tiny c Compiler (TCC, or Tiny CC) is the smallest C language compiler in the world.

TCC has a prominent feature: it is possible to use the C language as a script. The trial record is as follows:


First of all, install TCC.

Under Windows, download the Execute program: tcc-0.9.26-win32-bin.zip. Unzip to C:\TCC and add C:\TCC to the path directory.

Test if the installation is successful, enter command tcc-v in the command Line window and see the TCC version number is successful


Mode one: Execute C language text with TCC interpretation

Explain the execution, that is, do not compile, run directly.

Write a C program and save it for HELLO.C

#include <stdio.h>int main (int argc, char *argv[]) {   int i;    printf ("Hello, world\n");   for (I=1; i<argc; i++)     printf ("argv[%d]=%s\n", I, Argv[i]);}
In the Command Line window, enter command Tcc-run hello.c

-run means immediate execution.

Operation Result:

Hello, World


You can also pass in parameters to the program via the command line

In the Command Line window, enter command tcc-run hello.c param1 param2

Run result (with two incoming parameters shown):

Hello, World
Argv[1]=param1
Argv[2]=param2


Mode two: Under Linux, the C language program as a script to run, like the SH script

First of all, installing TCC under Linux, the version I used was Ubuntu 14.04

Download TCC source code, unzip

CD ~

wget http://download.savannah.gnu.org/releases/tinycc/tcc-0.9.26.tar.bz2

Tar xvf tcc-0.9.26.tar.bz2

Create a subdirectory tcc-0.9.26, enter the directory, compile it

CD tcc-0.9.26

./configure

Make

Make install

When you are finished, enter the command tcc-v. If the TCC version number is displayed, it indicates success.

Use the Find command to find out where TCC is. Find/-name TCC

Found TCC executable file installed in/usr/local/bin

Write a C program and save it for HELLO.C

#!/usr/local/bin/tcc-run#include <stdio.h>int Main (int argc, char *argv[]) {   printf ("Hello, world\n");}
The first line of #!/usr/local/bin/tcc-run is to tell the operating system that this is a script that the interpreter is/USR/LOCAL/BIN/TCC

Modify permissions to convert hello.c to executable file

chmod +x hello.c

Run HELLO.C directly

./hello.c

Operation Result:

Hello,world

Feel good,. c file directly as script


Compare, compile and then execute the case, the HELLO.C compiled: TCC hello.c, will generate a.out

Run A.out:./a.out

The result is the same as the script execution

Because of the first line of #!/usr/local/bin/tcc-run, compiling hello.c with GCC will make an error, compiling with TCC is no problem


Mode three: Embed the script function in the program, call the C language script dynamically (this is the essence of TCC)

The embed script is to let your program have the script function, and this script is still C language.

The examples\libtcc_test.c under the TCC directory is a demonstration program.

I think the demonstration program is not universal, so I made up a generic module, two files: cscript.c, cscript.h

First look at cscript.c, define a function run_script ()

Note: libtcc.h is a header file provided by TCC, copied over in the TCC directory

#include <stdlib.h> #include "libtcc.h"//TCC provided header file/* Run a script, start the function specified in the script, return the function's run result value * The Startup function prototype in the script must be: int func (int PARAM) * Program is the full content of the script, Function_name is the start function name, param is the parameter passed to the function * Tcc_path used to specify the directory where TCC is located, Tcc_path set NULL indicates that the TCC directory is not specified */int Run_script (char *program, char *function_name, int param, char *tcc_path) {tccstate *s;//TCC compilation engine int (*FUNC) (int);//A  function pointer, function prototype: int func (int param) int result; Running result s = tcc_new (); Initialize the TCC Build engine if (!s) return-1;    Initialization failed//tcc_path Specifies the directory where the TCC is located if (tcc_path!=null) Tcc_set_lib_path (S, Tcc_path); Indicates that the compilation result is written to memory, not to file Tcc_set_output_type (s, tcc_output_memory);//If compilation fails, exit if (tcc_compile_string (s, program) = =-1    ) Return-2;    If the program relocation fails, exit if (Tcc_relocate (S, Tcc_relocate_auto) < 0) return-3;    Look for the entry function named function_name func = Tcc_get_symbol (s, function_name); if (!func) return-4;    Entry function not found, exit//Run entry function, get run result = func (param);    End TCC Compiler engine Tcc_delete (s); return result;}

Look at cscript.h, nothing more than declare a run_script () this function.

#ifndef __cscript_h__#define __cscript_h__#ifdef __cplusplusextern "C" {#endif/** run a script that  starts the function specified in the script and returns the running result value of the function  * The Startup function prototype in the script must be: int func (int param) * Programe is a script, function_name is a startup function, param is a parameter passed to the function * Tcc_path is used to specify the directory where TCC is located, tcc_ When path is set to NULL indicates that the TCC directory is not specified */extern int run_script (char *program, char *function_name, int param, char *tcc_path); #ifdef __cpl Usplus} #endif #endif

All right, make a main program, and use the Run_script () function in the common module to specify the script file, read it and run it, each time through the command line.

Assume that the main program is named Cscript.exe

The Run command is: cscript <script_file>

The main program is as follows, save as Main.c

 #include < stdio.h> #include <stdlib.h> #include "cscript.h"//General module header file int main (int argc, char *argv[]) {char *program = NULL; Script content Char *function_name = "Script_main"; The start function is named script_mainint param = 888; Incoming parameter char *tcc_path = NULL; int file_size;int result; Returns the result file *fp;if (argc<=1) {printf ("Usage:cscript <script_file>\n"); return-1;} The first command-line argument is the script file name, open it if ((Fp=fopen (argv[1], "RB")) ==null) {printf ("Error Open File%s\n", argv[1]); return-1;} Measured file length fseek (FP, 0L, seek_end); file_size = Ftell (FP);//Request a memory for storing file contents program = (char *) malloc (file_size+1); Program==null) return-1;//Read all the contents of the file into Progarme fseek (FP, 0L, Seek_set); if (Fread (program, File_size, 1, FP) >0) { Program[file_size]=0;result = Run_script (program, function_name, Param, tcc_path);//Run printf ("result =%d\n", result);} Free (program); fclose (FP);} 
This program is simple, just read the content from the file specified in the first parameter of the command line, and execute it as a script.

In the script, you need to specify a startup function that is not main (), but an int script_main (int param).

Compile the main program (MAIN.C and CSCRIPT.C), assuming the TCC is installed in the C:\TCC directory

TCC-LLIBTCC-LC:\TCC main.c Cscript.c-o cscript.exe

-LLIBTCC means link LIBTCC library (case not written incorrectly)

-LC:\TCC Specify the TCC directory (case-insensitive)

-O cscript.exe specifies that the EXE file is generated as Cscript.exe

The compilation was successful without any prompts. Generate cscript.exe under Directory


Well, write a script (the content is as follows) and save it as Test1.txt

#include <stdio.h>int script_main (int param) {   printf ("It ' s in script main, param =%d\n", param);   return param;}

Run the Test1.txt directly with the cscript.exe you just created

Enter Command cscript Test1.txt

Operation Result:

It ' s in script main, param =888
result = 888

Success!!

This time, instead of using TCC to run the script, we used the Cscript.exe program we just wrote to run the script. Let your program have scripting capabilities.

From the example provided by TCC, TCC can support standard C, Windows API calls, Linux Dynamic library calls, and so on, that is, scripts to write system programs, GUI, anything.

Equivalent to a remote distribution program.

How to apply, to see the imagination. For example: The program downloads a text from the website, runs, generates a GUI, produces a villain running around on the desktop ...

Another example: Big Data operations, 100 computer networking, a machine to distribute a script, the local operation of each machine, return the results to the host summary, is the big Data map-reduce algorithm.

How far the heart is, how far it is

TCC is strong!




TCC Research (2) the C language as a script, interpreting execution, and embedding various programs

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.