C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old January 9th, 2003, 04:29 AM
bsuribabu bsuribabu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Hyderabad
Posts: 17 bsuribabu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to bsuribabu
Linakge of Standard Library Functions

Hi,

In c how the linkage of Standard Library Functions happens?



Please explain me.


Suri

Reply With Quote
  #2  
Old January 9th, 2003, 07:03 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
well you know it's called by an include:

#include <stdio.h>

which go at the top of the source file. that makes the compiler look where it knows libraries are stored. when you give it an include to look for and if the file name is in < > brackets it'll try and find it in the standard place. any functions that are in stdio.h are then useable by the code that follow beneath the include.

if the file name is enclosed in quotes like:

#include "header.h"

it'll look first in the same directory as where your source code is for that file, if it doesn't find it there it'll then look for it in the standard place.

that's how i understand it anyway. hth.

Reply With Quote
  #3  
Old January 9th, 2003, 11:15 AM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 6
Send a message via AIM to pschmerg
Quote:
Originally posted by balance
well you know it's called by an include:

#include <stdio.h>

which go at the top of the source file. that makes the compiler look where it knows libraries are stored. when you give it an include to look for and if the file name is in < > brackets it'll try and find it in the standard place. any functions that are in stdio.h are then useable by the code that follow beneath the include.

if the file name is enclosed in quotes like:

#include "header.h"

it'll look first in the same directory as where your source code is for that file, if it doesn't find it there it'll then look for it in the standard place.

that's how i understand it anyway. hth.

yep that's pretty much how it works. once the function you are calling is found it pretty much inserts the code from the called library function into your code.

Reply With Quote
  #4  
Old January 9th, 2003, 01:25 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
Nope. Nice try, but that isn't quite it. Try compiling the following:
Code:
#include <stdio.h>
int main(void) {
   printf("Hello world\n");
   return 0;
}

Now remove the #include <stdio.h> and try compiling again. Notice that the program compiles JUST fine and runs the SAME way.

So, what's the deal then and what exactly is in this header file <stdio.h>. Well, if you open it, you'll notice the only thing that it contains is a few #define and #include lines (defining things like TRUE, FALSE, NULL etc. and possibly including other .h files ), and function prototypes. So where's the actual function code then?? The answer is very simple. All C compilers come with a bunch of libraries and the linker looks at those libraries by default, unless you specify alternative libraries to the linker. The standard library is usually called libc.a (for *NIX), libc.lib (for Visual c++), CS.LIB or CL.LIB (for Turbo C) etc. The linker that comes with the C compiler will automatically look at the standard libraries BY DEFAULT unless told otherwise specifically. All that the header files have are function prototypes, so that the compiler may know ahead of time, to typecast arguments to the correct types, if needed. If a C compiler can't find the function prototype, it'll assume that you've called it with the correct number/type of arguments and leave it to the linker to link them together! This explains why our sample hello world program works whether you include stdio.h or not.

So how are these libraries created. Basically, when you compile C programs, this is how the process works:
{ C Source Files } ==> Compiler ==> {Object Files}
{Object Files} + { Third Party Object Files } + { Libraries} ==> Linker ==> {Executable}
The C compiler takes C source files and compiles them into Object files. An object file contains machine code + stub information to enable the linker to join the functions together to form the final executable. Hence, third party people can distribute only the object files and the header (.h files), and you can still create an executable using their object files. However, if you have a large number of object files, then they become a little unmanagable, especially, if you implement one or two functions per file. So, you can combine a bunch of object files into a library using a library tool. Depending on the compiler, the name of the tool varies. For example, Turbo C has TLIB.EXE, Visual C++ calls it LIB.EXE, C++ Builder has a wizard etc. You can use the library tool to take a bunch of Object files and organize them into a library. The standard library is built essentially the same way.

Hope this helps!

Reply With Quote
  #5  
Old January 9th, 2003, 04:30 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
that's great information. certainly helps me.

further question:

how could i find out all the connected library files? find out what/where they all are?

the reason i ask is because i am attempting to make an all in one easy to install version of gnu's gcc (for mac os x). i'm pretty naive on the whole thing, and after finally managing to build and install gcc (using apple's gcc that comes with apple's dev tools) i then copied over the freshly installed gcc to another machine (that didn't have dev tools or gcc) and found much to my surprise, but probably not to yours, that compilation needs more than just a compiler.

so i need to find out what else is needed, that a development environment has, that a standard environment doesn't have (apart from the compiler obviously)

i've found out i need an assembler and linker apparently. also the standard c libraries as they're also not included in a gcc install. how can i find out which libraries are needed by gcc? is that possible?

how did you make the connection between stdio.h and libc.a (or different name for different system)? or is it just something you picked up rather than found out via the code/files?

Reply With Quote
  #6  
Old January 10th, 2003, 12:37 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
>> how could i find out all the connected library files? find out what/where they all are?
Hopefully this page contains what you need. You also need all the header files under /usr/include, or whatever the equivalent is, under Mac OS X. Usually the library files are in /usr/lib (or /usr/local/lib) and the binaries are in /usr/bin (or /usr/local/bin) on a *nix system, and since OSX has a FreeBSD heritage, it might be in the same place (Don't take my word for it though! ).

>> how did you make the connection between stdio.h and libc.a (or different name for different system)? or is it just something you picked up rather than found out via the code/files?
Comes with experience and writing apps that needed to be linked to libraries other than the standard libraries (this happens a lot, if you're writing code for handheld scanners or other such embedded systems). It's precisely when I was deprived of the standard libraries when I became aware of them

Reply With Quote
  #7  
Old January 10th, 2003, 10:44 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,834 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 23 h 30 m 30 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Quote:
(this happens a lot, if you're writing code for handheld scanners or other such embedded systems)

I do the same thing but got past this problem. We just use a telnet client on the scanner that telnets to the application server using 802.11b. Love writting apps that run on our scanners.

Reply With Quote
  #8  
Old January 10th, 2003, 02:38 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Hopefully this page contains what you need.

yes that look very useful. it's got 'dependancies' listed which is very usefull. thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Linakge of Standard Library Functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway