ts-7000
[Top] [All Lists]

Re: [ts-7000] Segmentation Fault - I blame it on gcc's linker

To:
Subject: Re: [ts-7000] Segmentation Fault - I blame it on gcc's linker
From:
Date: Fri, 03 Aug 2007 00:52:11 -0400
>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/
 

<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU