Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

TCC : Tiny C Compiler


  • Please log in to reply
19 replies to this topic
PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
TCC : Tiny C Compiler
Downloaded file size (Windows version): 418KB
Unzipped size: 1.12MB
Example after unzipping:
C:\PrgCmdLine\tcc-0.9.23
> tcc\tcc -Iinclude -Llib examples/fib.c

C:\PrgCmdLine\tcc-0.9.23
> fib 11
fib(11) = 89

C:\PrgCmdLine\tcc-0.9.23
> tcc\tcc -Iinclude -Iinclude/winapi -Llib examples/hello_win.c

C:\PrgCmdLine\tcc-0.9.23
> hello_win
(shows a fully functional (but simple) centered window displaying a centered string)
Perfect for those wanting to learn C, and even the Windows C API.
It can even run as scripting language!

Otherwise, there is the reputed Ch language (Ch -- an embeddable C/C++ interpreter), weighting 35MB in its Windows version... Presumably which much more libraries and documentation! :-) It is freeware (but has a Pro version).

There are other C scripting languages, like Small, CSL or some others.

But unlike TCC, they can't be (AFAIK) used to make .exe or .dll files...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Sounds interesting. I knew about Ch and wanted it (before I learned AutoHotkey) as a scripting language for my USB Stick. But it was too heavy.

The ability to create standalone exes make it so interesting to me. Thx.

No signature.


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
It works wonderfully as a compiler.

It can even run as scripting language!

Could you give a pointer on how to do it, or your experiences? Also, do you know where can I download libtcc.dll or if I have to compile it myself?

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
I am using tcc.exe with this batch file: tcc.bat
@Echo Off
Set tccroot=%~dp0
"%tccroot%tcc\tcc.exe" "-I%tccroot%include" "-I%tccroot%include\winapi" "-I%tccroot%include\sys" "-L%tccroot%lib" %*

Put it to the root path of tcc.

Using tcc as a scripting language is as easy as using it as a compiler. Just add following option to the command: -run

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
It is fast enough, but you have to save your C file to disk. It was really nice if we could pass a string, containing a C program to a tcc dll. This string would be created by an AHK script, and we don't mess with the user's disk by storing temporary C files.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Why are you against a temporary file? After work it could be deleted.

Also it is very amazing that a "hello world" program compiled with tcc is only 1.5 KB small!!

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

Why are you against a temporary file?

I just don't like to worry about conflicts between file names, about non-existent temporary directories, environment variables, write permissions, disk quotas, free space, etc. In real life not very common scenarios, but why not play safe?

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Using the Windows standard temporary folder and naming the file with current timestamp should help for now. (%A_Temp%\%A_Now%_tcc.tmp)

I don`t think in that case a write or read permission error wouldn`t occur. Perfomance would be a better argument. :)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

Using the Windows standard temporary folder and naming the file with current timestamp should help for now. (%A_Temp%\%A_Now%_tcc.tmp)

It is not absolutely failsafe, either. Other applications following the same naming conventions would see their temporary files overwritten by our C files, or the other way around. When the PC clock is updated, you could get repeated and locked filenames. The Windows temporary files directory can be changed by another application, when we will not find our C file written a moment earlier. And so on. These are not common cases, but, if you distribute your code and many people start using it, there will be a few hit by these problems and you have to respond to their bug reports.

Perfomance would be a better argument.

Surprisingly, not always. Small temporary files are not actually written to disk. They remain in the Windows disk buffer, where you can access them in the speed of memory access. The root directory of the boot drive proved to be the fastest.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
2Laszlo

It is not absolutely failsafe, either

That is not, but TEMP\GUID\MyFile is.
Although, you can always construct something more natural so you can revisit TEMP folder and know when you see your subfolder.

BTW, scripting is supported in Lin:

C script supported : just add '#!/usr/local/bin/tcc -run' at the first line of your C source, and execute it directly from the command line.


I guess some of you may want to contact the win binary author and ask him to add stdin option, like:

type helloworld.c | tcc


You may also want to use variation of what Philho or Tuncay did as scriptrunner, which can be seen as workaround for missing Lin feature in Win.
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

TEMP\GUID\MyFile is [safe].

I agree. I often use GUID for naming temp files (%GUID%.tmp), The chance of a conflict is small enough. The other (not so common) problems remain, however: disk quotas, free space, cleaning up after a crash, etc.

scripting is supported in Lin

It works with tcc, too, but it uses a file.c, what I wanted to avoid with AHK generated C programs.

type helloworld.c | tcc

It would be nice, like tcc < helloworld.c, but not the same as a dll, which could remain memory resident. There are instructions on the tcc website about making such a dll, but it needs editing the source files, setting up project files, installing MinGW, etc. It would be nice if someone posted a link to an already compiled libtcc.dll.

Grumpy
  • Guests
  • Last active:
  • Joined: --

installing MinGW

tcc can compile itself...

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

tcc can compile itself...

but for a dll it needs to link runtime support. On their mailing list it was said that either MinGW or MS Visual Studio is needed. I have not tried, though...

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I visited MinGW but then.. as it is collection of various things I just couldn't reserve time to see what I need out of them. I don't know if they released some pack that can be used without hessle.


Cleaning up after a crash,

Its the best if you leave those files alone in TEMP, and clean TEMP once in a while (like 3 times a year), automaticaly or manuely. ITs not like .c files are going to eat much of your 120GB drive. The same with other "problems".

It works with tcc, too, but it uses a file.c

I think its easier to add stdin handling then to create dll.

which could remain memory resident.

Maybe exe can be resident too, apart from using RAM drive.
I didn't investigate this much, but this is an idea:
You create small launcher ~ 5K, probably in ASM. Launcher loads tcc into memory using "memory maped files" (see MSDN) and put it into shared memory and then goes to idle. On next execution new instance of launcher will send command to already active to execute script X and exit imediately. First luancher instance then executes c file using tcc in memory. The only thing I don't know for sure if exe can be run from memory, but I think it can, I remember a story about that...

So, later you can even put launcher as a service using some of the "app to service" utilties and you will have tcc preloaded before any user enters the system. No need for this thought, as tcc will be loaded with first script execution, so its best not to be in mem if you never use it in your win session.

All together, this is far easier then building tcc dll in mingw, and will have the same effect when speed is in question.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I am using tcc.exe with this batch file: tcc.bat

@Echo Off
Set tccroot=%~dp0
"%tccroot%tcc\tcc.exe" "-I%tccroot%include" "-I%tccroot%include\winapi" "-I%tccroot%include\sys" "-L%tccroot%lib" %*

I am using tcc with this part of a doskeyrc file:
tcc=C:\PrgCmdLine\tcc-0.9.23\tcc\tcc.exe -IC:\PrgCmdLine\tcc-0.9.23\include -IC:\PrgCmdLine\tcc-0.9.23\include\sys -IC:\PrgCmdLine\tcc-0.9.23\include\winapi -LC:\PrgCmdLine\tcc-0.9.23\lib $*
Of course, path must be adjusted.
The doskeyrc is in C:\, along with cmd.bat which contains:
@doskey /macrofile=c:\doskeyrc
It provides a number of handy abbreviations, like:
dkm=doskey/macros
?=doskey/macros
vi="C:\Program Files\UText\SciTE\SciTE.exe" $*
write="C:\Program Files\UText\SciTE\SciTE.exe" $*
The later is because when fellow programmers use my computer, they type vi file or write file, but thus I still open the file in a good available editor...

I have managed to compile a test DLL with TCC, but it wants a WinMain, it doesn't accept a DllMain...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")