C - TCC - example / small tutorial


Forum: Other Help Topics
Topic: C - TCC - example / small tutorial
started by: enthusi

Posted by enthusi on Jan. 14 2004,02:10
Hi there :)
I just uploaded a small C/TCC - Tutorial for DSL.
I wrote a small program during christmas when I had nothing with me but my beloved DSL-CD  :)  Since I had no C-books or internetresources available I coded along and commented every line. I soon came to the thought that it might be quite useful for others to take a look at this easy program without the need of heavy c-background. I kept things very simple and commented almost every line in a textfile.
I was quite sceptical about TCC and wanted the good "old" GCC...
TCC may not suit for general source-compilation but it is astonishing powerful for own little projects.
Maybe someone's interessted - please feel free to comment on it. I will start a thread in HOW-TOs. And hopefully you can help me with
comments on how to improve it.
I think it would be nice to add stuff like this (ok, to be honest: please add THIS one :-) to DSL... It takes up almost no space and can add many many fun hours for the DSL-user...

Get it here:
< http://allthegoodthings.tripod.com/damnsmalllinux >
< TCC - tut for DSL >
or type:
Code Sample

wget http://allthegoodthings.tripod.com/damnsmalllinux/tcc_tuturial_v0.1.zip

and unzip it...

Good night now,
enthusi

Posted by DrWatt on Jan. 14 2004,14:37
Thanks enthusi. I had tried to run tcc also and got the same sorts of errors as reported. At the time, I didn't have the time to figure it out, but the secret was the missing line

#include "/usr/lib/tcc/include/tcclib.h"

For those who know a smattering of C this line replaces the usual #include statements, such as #include <stdio.h>. Apparently a lot of the usual header files in tcc are inluded in the tcclib.h file. There are several other header files to explore in the tcc library files.


The other secret to getting immediate output is the -run opion in the command. For example, in a "c" file called test.c you would type

tcc -run test.c

This assumes you don't want to use the run script you provided.

Here is my test.c traditional first "c" file:

#include "/usr/lib/tcc/include/tcclib.h"
main()
{
 printf("Hello DSL world");
 return 0;
}

A good thing to do is to backup the old "/usr/share/scite/cpp.properties" file (in case you want to use it to compile gcc files in the future) and modify it so that you can compile link and run in scite. The output will appear in a separate pane on the right. Very handy. Here are the mods to the file:

1. After the line that reads "cc=g++ -pedantic -Os -fno-exceptions -fvtable-thunks -c $(FileNameExt) -o $(FileName).o" add a line that reads:

"tcc=tcc -run $(FileNameExt)"

2. Change all the lines that read "command.compile.*.c=$(cc)" to

"command.compile.*.c=$(tcc)"

there are 4 such instances. Save it. You must make the changes to this library file as root or it won't let you save them.

Now, you can edit in scite and use the "Complie command under tools to test your programs. Very nifty indeed. Standard c files should work.

Posted by DrWatt on Jan. 14 2004,14:43
A bit more explanation on the mods to scite. If you do them, any file you open with a ".c" extension will be assumed to be a tcc file. Scite will format and color code it as such and when you use the compile command in scite it will use scite to run and compile it.

There is some online documentaion for tcc at

< http://fabrice.bellard.free.fr/tcc/ >

enjoy.

Posted by DrWatt on Jan. 14 2004,16:41
enthusi -- my scite mod works well for simple c programs that need no input. I suspect that it could also be made to make and build, since there are scripts in scite for that as well.

I do not understand why the following will not compile and create an executable:

tcc -o test2 test.c

When i run that, I get the following errors:

tcc: undefined symbol '__libc_start.main'
tcc: undefined symbol 'printf'
tcc: undefined symbol 'scanf'

Obviously the libraries arent being linked.

Your command line in tccrun:

"tcc -o $1.run -lstdc++3-libc6.1-2-2.10.0 $1.c"

does work, and creates an executable. Could you please explain what each part of that line does?

Thanks.

Posted by enthusi on Jan. 14 2004,16:53
Hi!
I can give it a try.

when yoiu include the tcclib.h the functions you need are only declared.
Take a look at tcclib.h and you see what I mean (if you didnt already know).
So all they give is te name of a function and its parameters and return-values.
NOT the code itself!
The code for several functions is stored in different LIBS.
if you use te root-function SQRT() for example you have to declare it with math.h AND you have to link the mathlib to your program.
TCC (or any other compiler) will take the actual code of SQRT out of that lib.
In this case it is called libm..blabla. and can be linked to your program with the -l switch and the name of the lib without the leading word "lib".
So:
'tcc mathprogram.c -lm -o mathprogram'.
Same with the standardlibrary!
It is called libstdc++3-libc6.1-2-2.10.0 (you will find it in /usr/lib I think)
you have to link it to your code with -l'namewithout leading 'lib''.
So it is:
"tcc -o $1.run -lstdc++3-libc6.1-2-2.10.0 $1.c"
the $1 just stands just for the first parameter gicen to the bash-script
"tccrun test" will then execute
"tcc -o test.run -lstdc++3-libc6.1-2-2.10.0 test.c"
So your test.c IS then linked to the stdc-lib.
I hope this helps a bit...
Its not easy to know that you have to link wit THAT lib I admit.
Strange enough it is hardly mentioned anywhere...

have fun
enthusi

Posted by DrWatt on Jan. 14 2004,17:07
Thanks for the quick reply enthusi. That explanation makes sense to me. If I decide to modify scite to make and build with tcc I will have to include that library link.

Interesting that the run command works fine, but not if you want to create an executable. Tcc must already know where that library is.

I like tcc. Its fast and strong and a lot easier to use than gcc IMO. If I can get the whole thing working right in scite I will have a complete development package.

Happy C'ng.

Posted by enthusi on Jan. 14 2004,17:24
Thanks :)
GCC is quite nice too but its HUGE and most of the extraoptions are not needed for own projects....
Your scite-ev-kit sounds nice! Maybe I'll give it a try too some day. I still prefer to do things by hand but its quite old fashioned...
On my main box I usually code in kwrite or even midnightcommanders edit in the shell... :o)
Till next time,
enthusi

Posted by enthusi on Jan. 16 2004,14:52
Hi folks.
There was one little line to change - so here comes the new version.
Nothing bad but I want to keep up to date.
So better forget the direkt link and just see whats up on this site :
< TCC - Tutorial 16.01.04 v0.1.1 >

Have fun with it and please post wishes of any kidn to me :)
enthusi

Posted by hasty on Jan. 16 2004,20:00
Great stuff guys.

I can't add to your work, but I'm following (far behind) with interest.

Posted by enthusi on Jan. 17 2004,13:41
@hasty
if questions arise just shoot :)

Posted by enthusi on Jan. 20 2004,00:34
Hi!
Again another new tcc-tutorial-version!
Only minor changes in the documentary - none in the code.
BIG change concernig the license. Actually no change for you.
I never cared for such things but everybody should. Especially when "people" get funny (or rather sad) ideas about copyrights.
Luckyly people already came up with a solutions that fits pretty perfect for stuff like mine here, so from now on tcc_tutorial  is published under the GNU-GPL / GNU-FDL. (in short terms: do as you like)
Dont be concerned reading about licenses here - Dont Panic!
This is a gooood thing :D
Another good thing might be that I decided to dedicate myself to this tutorial in the future so expect frequent updates, changes and so on...
(talking is cheap, er?)

The new version is again available on the little extrasite (until told otherwise)
< NEW TCC_TUTORIAL_VERSION >

Have fun with it!

Bye, enthusi
Oh and here something I found very impressive:

Posted by wrayal on Jan. 23 2004,20:09
Is there any way anybody could post some of this in an uncompressed format? I am on a school network and cannot download compressed files and would be very interested to learn about tcc. Any help would be very useful!

Wrayal

Posted by enthusi on Jan. 24 2004,02:09
HI, sure.
I can uplaod the files uncompressed (but that would take me a while)
It might be faster if you give me a mailadress I could send them to?
You could send it to enthusi@gmx.net
There is one picturefile included that ist about 900kb big but can be compressed to 1kb :o) It would make sense to recreate it on your own. This is nor problem from within DSL and is described in the tutorial itself, also...
I will upload it uncompresses when I find the time...
I could mail it to you at once as soon as I got an emailadress...
Ciao,
enthusi

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.