egcs C++ compiler with STL library, how on redhat 5.0?

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Dan Kell » Sun, 19 Jul 1998 04:00:00



I've installed the latest egcs C++ compiler on my redhat-linux box,
but have found that it cannot compiler simple programs using the
standard-template-library (STL).  On the egcs site I saw that egcs is
said to work with the SGI version of STL, so I downloaded that, and I
got somewhat better results.  I can compile the first of the
following, but not the second.

Any hints??

PS: ideally, I'd love to get a RPM version of the STL library, that
will work with egcs.  Is such a thing out there, somewhere?

... STL1.cpp -- compiles ...

  #include <stdio.h>
  #include <vector.h>
  int main(int argc, char *argv[]) {
    vector<double> a;
    for (int i = 0; i < 10; i++)
      a.push_back(i/10.0);
    printf("a is:\n");
    for (int i = 0; i < 10; i++)
      printf("  %f\n", a[i]);
  }

... STL2.cpp -- will not compile ...

    #include <vector>
    #include <string>
    #include <stdio.h>
    int main(int argc, char *argv[]) {
      vector<string> a;
      for (int i = 0; i < 10; i++) {
        char msg[100];
        sprintf(msg, "%d", i);
        a.push_back(msg);
      }
      printf("a is:\n");
      for (int i = 0; i < 10; i++)
        printf("  '%s'\n", a[i]);
    }

--

Oceanography Department         phone:                 (902)494-1694
Dalhousie University            fax:                   (902)494-2885
Halifax, NS, CANADA, B3H 4J1    http://www.phys.ocean.dal.ca/~kelley

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by a.. » Sun, 19 Jul 1998 04:00:00



> PS: ideally, I'd love to get a RPM version of the STL library, that
> will work with egcs.  Is such a thing out there, somewhere?

RH 5.1 comes with egcs 1.0.3a, although with the old STL. Probably
the upcoming egcs 1.1 will still contain the old STL.

Several bugs here.
First your programm will not compile with any strictly conforming compiler
anyways, because it doesn't care about namespaces (egcs 1.0 doesn't have
working namespaces, so you have luck here, but it is nonetheless incorrect
C++)

Quote:>     #include <vector>
>     #include <string>
>     #include <stdio.h>

                using namespace std;

Quote:>     int main(int argc, char *argv[]) {
>       vector<string> a;
>       for (int i = 0; i < 10; i++) {
>    char msg[100];
>    sprintf(msg, "%d", i);
>    a.push_back(msg);
>       }
>       printf("a is:\n");
>       for (int i = 0; i < 10; i++)
>    printf("  '%s'\n", a[i]);

                ^^^^^^^^^^^^^^^^^^^^^
                printf %s expects a char *, not a string. Replace it with
        printf("  '%s'\n", a[i].c_str()), or use iostreams.

-Andi

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Will Maso » Tue, 21 Jul 1998 04:00:00


Quote:> ... STL2.cpp -- will not compile ...

>     #include <vector>
>     #include <string>
>     #include <stdio.h>
>     int main(int argc, char *argv[]) {
>       vector<string> a;
>       for (int i = 0; i < 10; i++) {
>         char msg[100];
>         sprintf(msg, "%d", i);
>         a.push_back(msg);

Why are you pushing a char* onto a vector<string>? It should be something
like:a.push_back(string(msg));

Quote:>       }
>       printf("a is:\n");
>       for (int i = 0; i < 10; i++)
>         printf("  '%s'\n", a[i]);

Here you have to give printf a pointer that it can handle, which means
you have to convert the string object to a char*, like:printf(" '%s'\n",
a[i].c_str());

Hope this helps,
Will

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Dan Kell » Tue, 21 Jul 1998 04:00:00


My thanks to the two previous posters, who are quite right -- I should
be pushing a string onto the stack.  The code should read as follows.
I can compile this fine in GNU/g++ but cannot compile it with egcs.
Question: can somebody out there who has egcs (e.g. from the redhat
5.1 release) give the following a try?  If it works then I'll know
that my problem lies in my installation of egcs on top of an existing
gnu compiler, as per the redhat 5.0 release.  Thanks so much for your
help!

#include <string>
#include <vector>
#include <stdio.h>
int
main(int argc, char *argv[]) {
  vector<string> a;
  for (int i = 0; i < 10; i++) {
    char msg[100];
    sprintf(msg, "%d", i);
    string *n = new string(msg);
    a.push_back(*n);
  }
  printf("a is:\n");
  for (int i = 0; i < 10; i++)
    printf("  '%s'\n", a[i].c_str());

Quote:}

--

Oceanography Department         phone:                 (902)494-1694
Dalhousie University            fax:                   (902)494-2885
Halifax, NS, CANADA, B3H 4J1    http://www.phys.ocean.dal.ca/~kelley
 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Torsten Pouli » Wed, 22 Jul 1998 04:00:00



Quote:> Question: can somebody out there who has egcs (e.g. from the redhat
> 5.1 release) give the following a try?  If it works then I'll know
> that my problem lies in my installation of egcs on top of an existing
> gnu compiler, as per the redhat 5.0 release.  Thanks so much for your
> help!

Your code copied verbatim to xx.C:

$ c++ xx.C
$ ./a.out
a is:
  '0'
  '1'
  '2'
  '3'
  '4'
  '5'
  '6'
  '7'
  '8'
  '9'

Seems to work ok. System is RH 5.1 with
$ rpm -qf /usr/bin/c++
egcs-c++-1.0.2-8

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Lars Hofhans » Wed, 22 Jul 1998 04:00:00



> #include <string>
> #include <vector>
> #include <stdio.h>
> int
> main(int argc, char *argv[]) {
>   vector<string> a;
>   for (int i = 0; i < 10; i++) {
>     char msg[100];
>     sprintf(msg, "%d", i);
>     string *n = new string(msg);
>     a.push_back(*n);
>   }
>   printf("a is:\n");
>   for (int i = 0; i < 10; i++)
>     printf("  '%s'\n", a[i].c_str());
> }



a is:
  '0'
  '1'
  '2'
  '3'
  '4'
  '5'
  '6'
  '7'
  '8'
  '9'

Seems to work here...


Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.90.27/specs
gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)

Cheers,

        Lars

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Klaus-Georg Adam » Wed, 22 Jul 1998 04:00:00



> My thanks to the two previous posters, who are quite right -- I should
> be pushing a string onto the stack.  The code should read as
> follows.

The two posters are right, but there is a conversion by constructor
from char* to std::string. The compiler automagically pushes the
string if you feed it a char*. The correct version of your program
(modulo namespaces) is:

#include <vector>
#include <string>
#include <cstdio> // Note the C++ version of stdio.h
int main() {      // argc and argv not needed.
        vector<string> a;
        for (int i = 0; i < 10; i++) {
                char msg[100];
                sprintf(msg, "%d", i);
                a.push_back(msg);
        }
        printf("a is:\n");
        for (int i = 0; i < 10; i++)
                printf("  '%s'\n", a[i].c_str());

Quote:}
> I can compile this fine in GNU/g++ but cannot compile it with egcs.
> Question: can somebody out there who has egcs (e.g. from the redhat
> 5.1 release) give the following a try?  If it works then I'll know
> that my problem lies in my installation of egcs on top of an existing
> gnu compiler, as per the redhat 5.0 release.  Thanks so much for your
> help!

> #include <string>
> #include <vector>
> #include <stdio.h>
> int
> main(int argc, char *argv[]) {
>   vector<string> a;
>   for (int i = 0; i < 10; i++) {
>     char msg[100];
>     sprintf(msg, "%d", i);
>     string *n = new string(msg);

Here you have a memory leak. a owns a copy of n, but n itself is never
deleted.

Quote:>     a.push_back(*n);
>   }
>   printf("a is:\n");
>   for (int i = 0; i < 10; i++)
>     printf("  '%s'\n", a[i].c_str());
> }

--
-------------------------------------------------------------------------

Institut f. Anorg. Chemie, Lehrstuhl II            Tel: 49(0)721 608 3485
Universit?t Karlsruhe, D-76128 Karlsruhe
-------------------------------------------------------------------------
 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Klaus-Georg Adam » Wed, 22 Jul 1998 04:00:00



> My thanks to the two previous posters, who are quite right -- I should
> be pushing a string onto the stack.  The code should read as
> follows.

The two posters are right, but there is a conversion by constructor
from char* to std::string. The compiler automagically pushes the
string if you feed it a char*. The correct version of your program
(modulo namespaces) is:

#include <vector>
#include <string>
#include <cstdio> // Note the C++ version of stdio.h
int main() {      // argc and argv not needed.
        vector<string> a;
        for (int i = 0; i < 10; i++) {
                char msg[100];
                sprintf(msg, "%d", i);
                a.push_back(msg);
        }
        printf("a is:\n");
        for (int i = 0; i < 10; i++)
                printf("  '%s'\n", a[i].c_str());

Quote:}
> I can compile this fine in GNU/g++ but cannot compile it with egcs.
> Question: can somebody out there who has egcs (e.g. from the redhat
> 5.1 release) give the following a try?  If it works then I'll know
> that my problem lies in my installation of egcs on top of an existing
> gnu compiler, as per the redhat 5.0 release.  Thanks so much for your
> help!

> #include <string>
> #include <vector>
> #include <stdio.h>
> int
> main(int argc, char *argv[]) {
>   vector<string> a;
>   for (int i = 0; i < 10; i++) {
>     char msg[100];
>     sprintf(msg, "%d", i);
>     string *n = new string(msg);

Here you have a memory leak. a owns a copy of n, but n itself is never
deleted.

Quote:>     a.push_back(*n);
>   }
>   printf("a is:\n");
>   for (int i = 0; i < 10; i++)
>     printf("  '%s'\n", a[i].c_str());
> }

--
-------------------------------------------------------------------------

Institut f. Anorg. Chemie, Lehrstuhl II            Tel: 49(0)721 608 3485
Universit?t Karlsruhe, D-76128 Karlsruhe
-------------------------------------------------------------------------
 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Will Maso » Wed, 22 Jul 1998 04:00:00


I know this is really not the point of your post, but just for the
record,
you are leaking 10 pointers to string objects. You should either:

string *n = new string(msg);
a.push_back(*n);
delete n;

Or, better yet, leave the heap out of it and just use one string object
on
the stack:

string n = msg;
a.push_back(n);

Or, less verbosely:

a.push_back(string(msg));

(Although some compilers will complain about the temporary object.)

The vector will copy the string that you push and the code will be
much cleaner and more efficient.

Just a thought,
Will

> #include <string>
> #include <vector>
> #include <stdio.h>
> int
> main(int argc, char *argv[]) {
>   vector<string> a;
>   for (int i = 0; i < 10; i++) {
>     char msg[100];
>     sprintf(msg, "%d", i);
>     string *n = new string(msg);
>     a.push_back(*n);
>   }
>   printf("a is:\n");
>   for (int i = 0; i < 10; i++)
>     printf("  '%s'\n", a[i].c_str());
> }

> --

> Oceanography Department         phone:                 (902)494-1694
> Dalhousie University            fax:                   (902)494-2885
> Halifax, NS, CANADA, B3H 4J1    http://www.phys.ocean.dal.ca/~kelley

--
What cracker is this same that deafs our ears
With this abundance of superfluous breath?
 - William Shakespeare, King John
 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Keith Nicho » Thu, 23 Jul 1998 04:00:00



> RH 5.1 comes with egcs 1.0.3a, although with the old STL. Probably
> the upcoming egcs 1.1 will still contain the old STL.

Now just hooooooold the bus!

Not wishing to side-track the thread into RH irrelevance, but *my* RH5.1 came
delivered with egcs-1.0.2-8 which frankly pissed me off since I'd previously
read that the "a" designation to 1.0.3a was specifically for RH5.1.  True?

In fact, I've not even been able to locate any egcs RPMS later than something
like 1.0.2-10 or-so.  Anyone give pointers?

Have I been duped?  Do I have a pre-release release?  Are we in Australia
being treated as if we're, well, "down-under"?  ;o)

And just to actually *contribute* something to the thread, didn't I read
somewhere that egcs ships with a vanilla release of the SGI STL?

Cheers

Keith

--
Keith Nichol                           | Address : Level 2, 175 Liverpool St,
Telstra Intelligent Network Platforms  |           Sydney 2000, Australia
                                       | Phone   : (+61) 2 9206 3429

 
 
 

egcs C++ compiler with STL library, how on redhat 5.0?

Post by Dan Kell » Thu, 23 Jul 1998 04:00:00


: I know this is really not the point of your post, but just for the
: record,
: you are leaking 10 pointers to string objects. You should either:

: string *n = new string(msg);
: a.push_back(*n);
: delete n;

: Or, better yet, leave the heap out of it and just use one string object
: on
: the stack:

: string n = msg;
: a.push_back(n);

: Or, less verbosely:

: a.push_back(string(msg));

Yes, you're quite right on that.  Thanks, to you and others who
pointed out my poor programming in setting up the example.

Another issue on the thread is whether RH5.1 comes with a functional
egcs with functional STL library.  I don't know the answer to this
question, and emphasize that I had a RH5.0 that I had messed with,
uninstalling gnu compiler and installing egcs and then a STL version
from the SGI site.  Therefore, my difficulties might not be shared by
folks who get RH5.1 and install the compilers out of the box.

As to whether the egcs that ships with RH5.1 is sufficiently robust, I
can't comment, but only hope (since I'm a new RH user and would like
to stay with them through future releases).


Oceanography Department         phone:                 (902)494-1694
Dalhousie University            fax:                   (902)494-2885
Halifax, NS, CANADA, B3H 4J1    http://www.phys.ocean.dal.ca/~kelley

 
 
 

1. GNU Standard C++ Library and the TenDRA C++ compiler?

    I was wondering if anybody has managed to get the GNU Standard C++
Library compiled with the TenDRA C/C++ compiler?  For those who don't
know, TenDRA is a public domain multi-language compiling system.  It is
available for many unix systems, including Linux.  And it comes stock
with C and C++ language "producers". From what I can tell, the C++
producer is very good and it follows very closely the ISO C++ draft
standard.  Much more so than egcs, as it even has namespace support.
Unfortunatly, TenDRA only comes with a _very_ minimal C++ library.  It
doesn't even have iostreams. I've been trying to get GNU's Standard C++
library compiled with TenDRA, but have been unsucessful so far.

    TenDRA is available at: http://alph.dera.gov.uk/TenDRA/

    Also, if anybody knows of another freely available Standard C++
library, I would appreciate it if you let me know.  Thanks!

--
=============================================================================
Arcadio A. Sincero Jr.
Undergraduate Computer Science Major/Linux Enthusiast/Competitive Bodybuilder
http://www.coming.to.a.web.site.near.you.com

"No one can hear you scream in /dev/null."
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

2. Linux 2.4.20 and 2.4.21-RC6 freezes during high writes from low free memory

3. Where is the HCL for Linux?

4. help with redhat

5. sun's compiler (v4.2), c++ and STL

6. Error Log analysis script/program?

7. Error in linknig C++ STL code using CC 5.0 on Solaris 2.6

8. HP's STL works with Sun's ProWorks C++ Compiler?

9. IBM C/C++ 3.6.4 compiler for AIX 4.2.1 and STL?

10. Q: Is the C++ STL now fully implemented in a Linux compiler?

11. Problem with STL set on Solaris 2.7 SC 5.0 compilers