>I just spent two days trying to figure out why I got a "Segmentation
>Fault" every time my program tried to make a system call to the "stat"
>function. If I put all my source code into one big module it worked
>fine.
The thing that seems odd to me is that it "worked fine". If you
compile this program:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat;
main()
{
struct stat b;
stat("file",&b);
}
don't you get errors like
c.c:5: `stat' redeclared as different kind of symbol
/usr/include/sys/stat.h:358: previous declaration of `stat'
c.c: In function `main':
c.c:10: called object is not a function
?
>Maybe there is some compiler or linker option I should have been using
>that would have caught this ambiguity and prevented this from
>happening. If anyone knows the answer to this, please share you
>knowledge with me.
I can't think of any. As far as I know, the linker does not have any
knowledge of data types.
The usual way to prevent this problem is to use a single include file
to declare all your globals. Then you include just that one include
file at the top of every source file.
#include "project_includes.h"
main()
{
...
}
That include file would contain includes for all the system include files
you need too. For example:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
extern int my_function(int a, int b, double c);
extern void *pointer;
...etc...
Every time any one of your source files needs another system include file,
you add it to project_includes.h. It doesn't matter that it gets included
in a hundred other source files that don't need it. It prevents any of
those other files from having the problem you ran in to. It also has
other benefits, like you end up spending a lot less time adding include
directives for system include files. (Never have to type "#include
<stdio.h>" again, for example...)
Mark S.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|