Hello, World

Discuss Programming
Post Reply
X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Hello, World

Post by X11 »

I want to see a Hello, World program in c that has no includes, that somehow writes to stdin without libarys?

anyone know where one is?

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

why?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

That writes to STDIN? Or *reads* from STDIN? I have written several that read from STDIN but I can't think of a reason for writing to it or if it is even possible (if it is I would imagine it's just putting data in a buffer).

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

he probably wants to write to stdout since that's what a hello world program does, i just don't see why it has to have no libraries, since it can simply be statically compiled if it needs to be shifted to a system with no libraries installed, but then i am terminally confused anyway when it comes to this sort of thing...

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

Actually it would be statically "linked" to make a program not depend on external libraries. I know you are not a programmer but I get the feeling you have some interest in it. For most/all compiled/assembled languages there is a two step process to create an executable program, the "compile" process and the "link" process. GCC without a special parameter will do the compile part to turn your source code into object code and then call the linker program to turn your object code into code that is executable by the OS.

You can tell gcc not to call the linker by adding the "-c" param:

$ gcc -c -o myprog.o myprog.c

Now to see what gcc would do to link the resulting object file you can add a "-v" parameter and use gcc to link it:

$ gcc -v -o myprog myprog.o

On my system (with GCC v3.2) it uses a program called "collect2" that I suspect in turn calls the "ld" program with lots of options. gcc used to call "ld" directly.

At any rate you should see that there are a few other object files included even if you don't specify any on the command line. These are needed to make your program loadable by the OS. You are correct though in that statically linking them makes them not require any libraries at run time.

Now as far as X11's original question I assume he wants to be able to directly "read" STDIN as writing to STDOUT is something he already knows how to do and any simple printf statement does automatically. Making your program read from STDIN gives it "coolness" because you can then use your command in a pipeline using the pipe "|" symbol. If that is what you are asking X11 I can whip up an example. It might be just as easy to grab the source code for a couple of the simple existing UNIX utils like "cat" or "tr" though as they would be small good examples.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

i don't think i knew any of that!
it's always good to learn though!

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

Sorry, I ment stdout.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

Oh and there is no reason Calum, its called "Hacking"

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

mr sarcasm strikes back.
i only wondered.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

mr sarcasm strikes back,
i was only telling you

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

well... thanks! that's one more thing i now know, how long will it be before my brain is full? :D

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

when you are about 40 and you go by the name "Void Main" and have an answer to almost every linux related question asked of you.

Post Reply