Hello, World

Discuss Programming

Hello, World

Postby X11 » Thu May 15, 2003 3:58 am

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?
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Calum » Thu May 15, 2003 4:45 am

why?
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Thu May 15, 2003 7:47 am

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
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Thu May 15, 2003 9:34 am

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
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Thu May 15, 2003 10:19 am

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
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Thu May 15, 2003 10:27 am

i don't think i knew any of that!
it's always good to learn though!
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby X11 » Fri May 16, 2003 3:34 am

Sorry, I ment stdout.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Fri May 16, 2003 3:40 am

Oh and there is no reason Calum, its called "Hacking"
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Calum » Fri May 16, 2003 5:37 am

mr sarcasm strikes back.
i only wondered.
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby X11 » Fri May 16, 2003 10:34 am

mr sarcasm strikes back,
i was only telling you
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Calum » Sat May 17, 2003 3:24 am

well... thanks! that's one more thing i now know, how long will it be before my brain is full? :D
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby X11 » Sat May 17, 2003 9:40 am

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.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron