Bug 32474 - struct timeval collision in include files for MinGW cross compile
Summary: struct timeval collision in include files for MinGW cross compile
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: bootstrap (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-23 19:09 UTC by Rob
Modified: 2007-06-23 21:04 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-mingw32
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rob 2007-06-23 19:09:41 UTC
When building for target i686-pc-mingw32 on host (Debian) i686-pc-linux-gnu
I find the file /opt/build/gcc-4.1.2/libiberty/pex-win32.c uses both
#include "pex-common.h" and "#include <windows.h>" thus we get this:

In file included from /opt/mingw32/i686-pc-mingw32/sys-include/bits/resource.h:151,
                 from /opt/mingw32/i686-pc-mingw32/sys-include/sys/resource.h:26,
                 from /opt/mingw32/i686-pc-mingw32/sys-include/sys/wait.h:32,
                 from /opt/build/gcc-4.1.2/libiberty/pex-win32.c:37:
/opt/mingw32/i686-pc-mingw32/sys-include/bits/time.h:70: error: redefinition of 'struct timeval'


Creating an ".i" file I see this:

# 181 "/opt/build/gcc-4.1.2/libiberty/../include/libiberty.h"
/* Get the current time.  */
/* Prototypes vary from system to system, so we only provide a
   prototype on systems where we know that we need it.  */
#ifdef __MINGW32__
/* Forward declaration to avoid #include <sys/time.h>.   */
struct timeval;
extern int gettimeofday (struct timeval *, void *);
#endif

# 109 "/opt/mingw32/i686-pc-mingw32/include/winsock2.h" 3
struct timeval {
 long tv_sec;
 long tv_usec;
};

# 69 "/opt/mingw32/i686-pc-mingw32/sys-include/bits/time.h"
struct timeval
  {
    __time_t tv_sec;
    __suseconds_t tv_usec;
  };



The file "sys/resource.h" includes "bits/resource.h", which then includes "bits/time.h" _directly_.


The file "sys/time.h" has an ifdef system like this:

#ifndef _TIMEVAL_DEFINED /* also in winsock[2].h */
#define _TIMEVAL_DEFINED
struct timeval {
  long tv_sec;
  long tv_usec;
};
...
#endif /* _TIMEVAL_DEFINED */


The file "bits/time.h" has an ifdef system like this:

/* Never include this file directly; use <time.h> instead. */
#ifdef __need_timeval
# undef __need_timeval
# ifndef _STRUCT_TIMEVAL
#  define _STRUCT_TIMEVAL
#  include <bits/types.h>

/* A time value that is accurate to the nearest
   microsecond but also has a range of years.  */
struct timeval
  {
    __time_t tv_sec;            /* Seconds.  */
    __suseconds_t tv_usec;      /* Microseconds.  */
  };
# endif /* struct timeval */
#endif  /* need timeval */


Since we are compiling for --target=i686-pc-mingw32 I imagine we prefer the winsock2.h version of struct timeval.


To achieve that do this:


/opt/build/gcc-4.1.2/libiberty/pex-win32.c
----- OLD
#include "pex-common.h"

#include <windows.h>

#ifdef HAVE_STDLIB_H
-----

----- NEW
#include "pex-common.h"

#include <windows.h>
#ifdef __MINGW32__      /* avoid redefinition of 'struct timeval' */
#define _STRUCT_TIMEVAL
#endif

#ifdef HAVE_STDLIB_H
-----



Alternately we could do this:

/opt/build/gcc-4.1.2/include/libiberty.h

----- OLD
/* Get the current time.  */
/* Prototypes vary from system to system, so we only provide a
   prototype on systems where we know that we need it.  */
#ifdef __MINGW32__
/* Forward declaration to avoid #include <sys/time.h>.   */
struct timeval;
extern int gettimeofday (struct timeval *, void *);
#endif
-----

----- NEW
/* Get the current time.  */
/* Prototypes vary from system to system, so we only provide a
   prototype on systems where we know that we need it.  */
#ifdef __MINGW32__
/* Forward declaration to avoid #include <sys/time.h>.   */
struct timeval;
extern int gettimeofday (struct timeval *, void *);
#define _STRUCT_TIMEVAL         /* use winsock2.h timeval structure instead of bits/time.h version */
#endif
-----


Which do the maintainers prefer ? Either way pex-win32.c compiles without error.

I chose to modify /opt/build/gcc-4.1.2/include/libiberty.h for _my_ build since
there was already a "#ifdef __MINGW32__" in that file.


I don't know that "sys/resource.h" which includes "bits/resource.h" should
include "bits/time.h" _directly_. 


I don't know why the "bits/time.h" says this: "/* Never include this file 
directly; use <time.h> instead. */" since "time.h" does not include "bits/time.h" - so "bits/resource.h" needs to if it wants bits/time.h.

The files "bits/resource.h" and "sys/select.h" are the only ones that include
"bits/time.h", that may not be GCC's fault but I am using GNU/Linux.

# grep -r bits\/time.h /opt/mingw32/i686-pc-mingw32/sys-include/*          
/opt/mingw32/i686-pc-mingw32/sys-include/bits/resource.h:#include <bits/time.h>         /* For `struct timeval'.  */
Binary file /opt/mingw32/i686-pc-mingw32/sys-include/c++/4.2/bits/stdc++.h.gch/O0g.gch matches
Binary file /opt/mingw32/i686-pc-mingw32/sys-include/c++/4.2/bits/stdc++.h.gch/O2g.gch matches
Binary file /opt/mingw32/i686-pc-mingw32/sys-include/c++/4.2/bits/stdtr1c++.h.gch/O2g.gch matches
/opt/mingw32/i686-pc-mingw32/sys-include/sys/select.h:#include <bits/time.h>


If it is agreed that this is a bug then you will want to update
http://ftp.gnu.org/gnu/gcc/gcc-4.1.2/gcc-core-4.1.2.tar.bz2
Comment 1 Richard Biener 2007-06-23 19:26:36 UTC
This looks more like a mingw header problem.
Comment 2 Andrew Pinski 2007-06-23 19:48:59 UTC
# 109 "/opt/mingw32/i686-pc-mingw32/include/winsock2.h" 3
# 69 "/opt/mingw32/i686-pc-mingw32/sys-include/bits/time.h"


These two headers don't come from GCC at all so closing as invalid.  Please report this bug to mingw instead.
Comment 3 Rob 2007-06-23 21:04:55 UTC
OK. I thought since GCC had a number of "#ifdef __MINGW32__" lines in it's source that the fixes were done on GCC to accommodate the OS (especially on a cross-compile) in preference to altering system header files to work for GCC.