The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: extern for functions
Gnet
Newbie
Rank: 1



UID 159
Digest Posts 0
Credits 0
Posts 44
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:27 AM  Profile | P.M. 
extern for functions



extern for functions



hi,

please let me know if the extern keyword is necessary for using functions which is defined and declared in another file and and used in a different file where both these files are linked together.


Top
Dubz
Newbie
Rank: 1



UID 126
Digest Posts 0
Credits 0
Posts 26
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:27 AM  Profile | P.M. 
no. you don't have to do this.

code:
# cat test.c
#include<stdio.h>

int main(int argc, char *argv[]){
        adder(5,6);
        exit(0);
}
# cat test1.c
#include<stdio.h>
void adder(int a,int b) {
        fprintf(stdout,"%d\n",a+b);
}
# cc test.c test1.c
# ./a.out
11
Top
kapengbarako
Newbie
Rank: 1



UID 212
Digest Posts 0
Credits 0
Posts 44
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:27 AM  Profile | P.M. 
oh god ! i always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c

file1.c

code:
#include<stdio.h>
#include "file2.h"
main()
{
   func2();
}
file2.c

code:
#include<stdio.h>
func2()
{

}
file2.h
Top
darzuaga
Newbie
Rank: 1



UID 106
Digest Posts 0
Credits 0
Posts 50
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:27 AM  Profile | P.M. 
quote:
originally posted by the_learner
oh god ! i always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c



it is good practice to do so.

also, file2.c should include file2.h to confirm the actual function agrees with your public interface.

i also recommend compiling with maximum warnings on to help catch errors, with gcc this would be "-wall -werror". while some may claim you don't technically need to in many cases, the errors that it catches outweigh any time saved not using prototypes.
Top
Canadianbacon
Newbie
Rank: 1



UID 82
Digest Posts 0
Credits 0
Posts 32
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:27 AM  Profile | P.M. 
quote:
originally posted by the_learner
oh god ! i always thought that if a function in a file file1.c calls another function func2 in file file2.c, then we must include prototype of func2 in file2.h and include it in file1.c

file1.c

code:
#include<stdio.h>
#include "file2.h"
main()
{
   func2();
}
file2.c

code:
#include<stdio.h>
func2()
{

}
file2.h

code:
void func2();


also, don't put #include <stdio.h> in file1. and file2.c. just make it so they both include file2.h, and #include <stdio.h> in file2.h.
Top
mantero
Newbie
Rank: 1



UID 248
Digest Posts 0
Credits 0
Posts 18
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 10:28 AM  Profile | P.M. 
quote:
originally posted by octal
just make it so they both include file2.h, and #include <stdio.h> in file2.h.



you could do, but you don't need to put every include file under the sun in file2.h, only put include files required to support the types used in file2.h.

typically you have an include file common to the project which includes all the include files required by most files and common types.

typically with unix you try and include the minimum number of include files you need to support your project, where as on windows, windows.h includes almost everything you could think of.
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:33 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005688 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP