ts-7000
[Top] [All Lists]

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

To:
Subject: [ts-7000] Re: Segmentation Fault - I blame it on gcc's linker
From: "Ed" <>
Date: Fri, 03 Aug 2007 14:51:08 -0000
--- In   wrote:
> >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
> ?
> 

You're absolutely right, Mark.  I mis-spoke when I said it "worked
just fine."  It only compiled AFTER I got the above error and changed
the name of my variable "stat" to something else.  Where it failed to
catch the error was when I had declared the variable "stat" in a
different module as shown below.  The compiler/linker doesn't flag an
error in this case and the program crashes.  It was sloppy
programming on my part and I should have seen what was going on much
sooner (right now I'm hitting myself on the head and saying "Stupid,
stupid, stupid!").  I plan to take all the advice that has been given
here.  I've already implemented the global include file idea.  

Thanks very much to everyone who responded.  You've all been very helpful.

Ed


THIS CODE COMPILES AND LINKS BUT GIVES A SEGMENTATION FAULT WHEN RUN:

// ---------------------
// stat_a.c
// ---------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void some_function (void);
main()
{
  struct stat b;
  stat("file",&b);

  some_function();
}

// ---------------------
// stat_b.c
// ---------------------
#include <unistd.h>
#include <stdio.h>
int stat;
void some_function (void)
{
   printf("Do something\n");
}

Compiled with:
$> gcc -g -Wall -o /bin/mystat stat_a.c stat_b.c





 
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