I am trying to port my current C++ and C skills to Linux Programming. I have successfully created the standard Hello World starting project in C for linux using both files and standard output.

I am trying to do something simular using standard C++ libraries, I tried to use the iostream template class in the STL library, I don't whether it is supported in Linux however I can seem to use #include <iostream> without a linking error being generated. The linker throughs an error "Undefined reference" when I write the complete Hello World program:

Code:

#include <iostream>

using namespace std;

int main(void)
{
cout << "\n Hello World";
}

I am at the moment thinking the problem is that I am not linking the program with the required stl library, however I have no idea how to do this in Linux.

I have look through the standard included documentation but cannot find a reference of the libraries included in Linux (Red Hat 8.0), does any know of a good reference on the C++ libraries in Linux? Is this my problem or does Linux not support STL?

I am new to Linux programming so I am at the moment only really looking for a good reference site, thanks in advance.

KnNeeded

December 24th, 2003, 06:24 AM

cup

How are you building the system? Use g++ instead of gcc

December 24th, 2003, 06:35 AM

Andreas Masur

Quote:

Originally posted by cup
How are you building the system? Use g++ instead of gcc

Well...gcc is the GNU compiler suite which provides frontends for C, C++, Java, Fortran etc. as well as these libraries. g++ is the traditional name of the C++ compiler which is part of the gcc suite...

So, I do not see what you mean?

December 24th, 2003, 08:46 PM

Sam Hobbs

There is not a library for the STL. The STL is (usually) only source code and exists entirely in the headers. It is called a library but there is not a library for it. At least not for the most popular implementations of the STL. The STL is part of the C++ language, so it is supported by the Linux compiler.

I have not used gcc much but I am nearly certain that what was said by cup is what you need and you probably already have the problem solved. As I recall, the default language that gcc recognizes is C and the default language that g++ recognizes is C++. They are essentially the same program except for minor differences such as that. I recall having a similar problem; in a Windows environment, I assumed that a file with a cpp extension would be compiled as a C++ program, but it was not. I got a compile error using gcc (I think) that went a way when I compiled using g++ (I think).

December 24th, 2003, 09:00 PM

Sam Hobbs

Quote:

Originally posted by Andreas Masur
Well...gcc is the GNU compiler suite which provides frontends for C, C++, Java, Fortran etc. as well as these libraries. g++ is the traditional name of the C++ compiler which is part of the gcc suite...

I am nearly certain that the default language for gcc is C.

December 24th, 2003, 11:11 PM

KnNeeded

Thanks Guys I will check it out

December 24th, 2003, 11:34 PM

kasracer

Do not pass void to functions in C++.

Sorry, I know that doesn't fix your problem but it is SO annoying when people do that =/

I'm wierd

December 25th, 2003, 04:19 AM

Andreas Masur

Quote:

Originally posted by Sam Hobbs
There is not a library for the STL. The STL is (usually) only source code and exists entirely in the headers. It is called a library but there is not a library for it. At least not for the most popular implementations of the STL.

That is not true...one of the most popular STL implementation uses an library and/or a dll - STLport. I do not know much about others - Dinkumware, which is part of the IDE, might be different, however, I never used anything different than STLport...

December 25th, 2003, 04:24 AM

Andreas Masur

Quote:

Originally posted by Sam Hobbs
I am nearly certain that the default language for gcc is C.

Well...gcc stands for GNU Compiler Collection, which simply acts as a front end for the underlying compilers it contains. The former name was GNU CCompiler, at that time it was only a simple C compiler...

December 25th, 2003, 01:40 PM

Sam Hobbs

Quote:

Originally posted by Andreas Masur
That is not true...one of the most popular STL implementation uses an library and/or a dll - STLport. I do not know much about others - Dinkumware, which is part of the IDE, might be different, however, I never used anything different than STLport...

I am quite surprised that this is the first time I have heard of STLport and that it uses libraries. I have seen Dinkumware mentioned in the C++ language newsgroup but not STLport.

Quote:

Originally posted by Andreas Masur
Well...gcc stands for GNU Compiler Collection, which simply acts as a front end for the underlying compilers it contains. The former name was GNU CCompiler, at that time it was only a simple C compiler...

Yes but what is the default language for gcc? The meaning of gcc is not very relevant; the default language is.

December 25th, 2003, 02:44 PM

nsh123

Quote:

Originally posted by kasracer Do not pass void to functions in C++.

Sorry, I know that doesn't fix your problem but it is SO annoying when people do that =/

I'm wierd

Why so ?

Sincerely,

December 25th, 2003, 06:42 PM

kasracer

Quote:

Originally posted by nsh123
Why so ?

Sincerely,

Because the C++ standard says NOT to. A function requiring no arguements (you don't want to send any to main) does not need to be sent void. That is C, not C++ style.

January 10th, 2004, 12:09 AM

abc_coder

Quote:

Originally posted by kasracer
Because the C++ standard says NOT to. A function requiring no arguements (you don't want to send any to main) does not need to be sent void. That is C, not C++ style.

you are mistaken. void is a perfectly legal C++ function argument - I dare you to show me the exact paragraph in the standard that says different. however, if you DO look at the standard, you'll see something like the example in 8.3.5.1, saying that

int f();
means
int f(void); in C++
and
int f(unknown) in C

to come back to the original thread question, cout is not a part of STL - and to use C++ standard objects you need to link against the implementing library (libstdc++.* in this case). g++ automatically does this for you, while the generic frontend gcc does not and requires -lstdc++

finally, you DO want sometimes arguments to main() - ever heard of:

int main(int argc, char** argv)

January 10th, 2004, 07:20 AM

Paul McKenzie

Re: STL and Linux

Quote:

Originally posted by KnNeeded
I tried to use the iostream template class in the STL library

As abc_coder points out, iostreams is not part and has never been part of STL. The iostreams library has existed since the first C++ compilers existed, separate and apart from STL. Go to the SGI web site and look in the STL section -- you will find nothing there about iostreams. Get a book on just STL -- again you will find nothing on iostreams (except maybe an example using cout).

I know it sounds like a small thing to be concerned about, but I have seen programmers waste valuable time looking for information on iostreams, and where do they look? They look in the STL references, thereby not finding anything, wasting their time.

Regards,

Paul McKenzie

January 10th, 2004, 03:23 PM

kasracer

Quote:

Originally posted by abc_coder
you are mistaken. void is a perfectly legal C++ function argument

So is many other things in C but that doesn't mean you should do it.

Quote:

Originally posted by abc_coder
I dare you to show me the exact paragraph in the standard that says different. however, if you DO look at the standard, you'll see something like the example in 8.3.5.1, saying that

int f();
means
int f(void); in C++

You do realize that proves my point, right? It shows that f() means the samething as f(void).

Besides, when I did a search for 8.3.5.1 I didn't find ANYTHING, but in the standard in 8.3.5 it mentions