weird g++ error

Guillem Jover guillem@debian.org
Sat, 11 Sep 2004 09:01:16 +0200


Hi,

On Fri, Sep 10, 2004 at 12:39:12AM +0200, Robert Millan wrote:
> I noticed this weird g++ error.  Sounds like something is fucked in our g++..
> 
> rmh@aragorn:/tmp$ cat test.cpp 
> #include <iostream>
> rmh@aragorn:/tmp$ g++ test.cpp
> In file included from /usr/include/c++/3.3/bits/stl_algobase.h:67,
>                  from /usr/include/c++/3.3/memory:54,
>                  from /usr/include/c++/3.3/string:48,
>                  from /usr/include/c++/3.3/bits/locale_classes.h:47,
>                  from /usr/include/c++/3.3/bits/ios_base.h:47,
>                  from /usr/include/c++/3.3/ios:49,
>                  from /usr/include/c++/3.3/ostream:45,
>                  from /usr/include/c++/3.3/iostream:45,
>                  from test.cpp:1:
> /usr/include/c++/3.3/cstdlib:114: error: `system' not declared

I've tracked that down to:

[-- test-bad.cc --------]
#include <pthreads.h>
#include <cstdlib>
int main() { return 0; }
[-- test-bad.cc --------]

In <cstdlib> we have:

[-- <cstdlib> ----------]
...
#include <bits/c++config.h>
#include <cstddef>
#include <stdlib.h>
...
#undef system
...
namespace std {
  ...
  using ::system;
  ...
}
...
[-- <cstdlib> ----------]

And in <pthreads.h> from (GNU pth) we have:

[-- <pthread.h> --------]
#define system __pthread_system
[-- <pthread.h> --------]

So <bits/c++config.h> is indirectly #including <pthreads.h>, and
that #defines the new function, changes all occurrences from there,
and thus the ones on <stdlib.h>, and then <cstdlib> undefs it, so
when the it tries to reference ::system in the using sentence it
cannot find it because it hase been previously remmaped to
__pthread_system.

A test case to show that this is the problem could be:

[-- test-ok.cc ---------]
#include <pthreads.h>
#undef system
#include <cstdlib>
int main() { return 0; }
[-- test-ok.cc ---------]

I'm not sure what's the proper fix, on <cstdlib> we can read:

// Get rid of those macros defined in <stdlib.h> in lieu of real functions.

There are multiple ways to fix this:

  * Move the <stdlib.h> and #undefs before #including <bits/c++config.h>
    and <cstddef>, as it's supposedly intended to only get rid of macros
    from <stdlib.h>.
  * Do no undef those macros on <cstdlib>.
  * Do not define those macros in <pthread.h> as they are not defined
    for LinuxThreads for example.

regards,
guillem