Need help understanding external functions

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 49
Reputation: DeathEvil is an unknown quantity at this point 
Solved Threads: 0
DeathEvil DeathEvil is offline Offline
Light Poster

Need help understanding external functions

 
0
  #1
Jul 9th, 2007
Can someone please explain to me and show a sample of using external function?

Let say I have my main file: main.c
and other file only with function: print.c

I know I should prototype print function in my main.c, define it in print.c and call it from main.c. (at least I think that's how it is)

when I do it, the compiler refuses to work, that's why I need some sample which I could not find anywhere (ebooks, google, textbook) - really weird.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help understanding external functions

 
0
  #2
Jul 9th, 2007
  1. /* main.c */
  2. extern void foo ( void );
  3.  
  4. int main ( void )
  5. {
  6. foo();
  7. return 0;
  8. }
  1. /* print.c */
  2. #include <stdio.h>
  3.  
  4. void foo ( void )
  5. {
  6. puts ( "foo!" );
  7. }
That's all it takes. In fact, you don't even need the extern keyword for the prototype (but it aids clarity). If that's what you're doing, can you explain how the compiler "refuses to work"?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 49
Reputation: DeathEvil is an unknown quantity at this point 
Solved Threads: 0
DeathEvil DeathEvil is offline Offline
Light Poster

Re: Need help understanding external functions

 
0
  #3
Jul 9th, 2007
thanks for quick reply, yes this is what I do and it fails. when I compile it is says linker erros undefined reference to'my()'

where my() is the function in print.c file
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help understanding external functions

 
0
  #4
Jul 9th, 2007
How are you compiling and linking your files?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 49
Reputation: DeathEvil is an unknown quantity at this point 
Solved Threads: 0
DeathEvil DeathEvil is offline Offline
Light Poster

more detailed example

 
0
  #5
Jul 9th, 2007
// main.c

extern void my(void); //prototype

........

my(); //function call

// my.c

#include <stdio.h>
my(void)
{
printf"external function works!!!!!");
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help understanding external functions

 
0
  #6
Jul 9th, 2007
Well first, your prototype and definition don't match. If you omit the return type, that's call implicit int and an int return type will be assumed, not void. Second, I asked how you're compiling and linking, not to post your code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 49
Reputation: DeathEvil is an unknown quantity at this point 
Solved Threads: 0
DeathEvil DeathEvil is offline Offline
Light Poster

Re: Need help understanding external functions

 
0
  #7
Jul 9th, 2007
I'm using visual studio 2005, but I just tried gcc, devc++, and they fail too
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help understanding external functions

 
0
  #8
Jul 9th, 2007
Okay, then post your exact code so that I can try it out.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: Need help understanding external functions

 
0
  #9
Jul 9th, 2007
You likely don't link your my.c anyhow. It depends on your compiler, like gcc main.c my.c -o main both compiles the modules, and links them, producing the executable main. This is similar for all compilers, but the problem is when you use some graphical ide, which is bad anyhow, but if you do, you must add your my.c in project, so that the ide knows what files need to be linked. Usually, the function prototypes and global variables defined in one file,would be declared not in that file, but in include file, like my.h, which would be included to both files, with #include <my.h>. It works also otherwise, but this is a good habbit, so that you don't always have to write your declarations separately in every file. If you are very lazy, your program would certainly compile if you would include your c file to the main file, with #include <my.c>, but it's not good to be that lazy.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help understanding external functions

 
0
  #10
Jul 9th, 2007
It works fine for me in Visual Studio 2005 after correcting the syntax errors, setting the correct return type for your external function, and compiling as C++ because that's clearly what you're doing. Post the errors you get when you build.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC