PDA

View Full Version : gcc / g++ question



oldmanstan
October 26th, 2005, 05:23 AM
I just started working through a c++ book and when I went to compile the quintessential "hello world" program I get crazy errors from gcc but it works fine when I run it directly through g++, is my gcc not configured correctly?

here is my code...
#include <iostream>

int main()
{
std::cout << "Hello world!\n";
}

and here is the error messages from gcc...
/tmp/ccFXEdrI.o: In function `main':
helloworld1.cpp:(.text+0x25): undefined reference to `std::cout'
helloworld1.cpp:(.text+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccFXEdrI.o: In function `__tcf_0':
helloworld1.cpp:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccFXEdrI.o: In function `__static_initialization_and_destruction_0(int, int)':
helloworld1.cpp:(.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccFXEdrI.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status

I tried running it with sudo too, same thing. I understand gcc is a kind of wrapper for various compilers and whatnot but I always thought it basically passed the job off to g++ anyway... in any event, I got it to work using just g++ but I'd still like to know why it doesn't work the other way...

LordHunter317
October 26th, 2005, 05:35 AM
When you invoke "gcc" to have it compile and link a program, like you have, it doesn't pass the standard C++ libraries to the linker. Meaning it doesn't try to include all the system code you need to run a C++ program.

Running "gcc" as "g++" causes it to include all the standard C++ libraries, rectifying this. There are a few other minor differences between the two invocations, but that's by far the most important one.

Ride Jib
October 26th, 2005, 05:53 PM
one of your errors is probably that you are not returning anything on a return type int.

LordHunter317
October 26th, 2005, 07:32 PM
one of your errors is probably that you are not returning anything on a return type int.In C++, if you don't return a value from main, it defaults to EXIT_SUCCESS. It's not an error, just a language quirk.

Kyral
October 26th, 2005, 07:34 PM
It would work if you made it void main()

I think

RastaMahata
October 26th, 2005, 08:03 PM
#include <iostream>

int main()
{
std::cout<<"Hello world!"<<endl;
return 1;
}
Compilation:
g++ FILE.gcc -o PROGRAMNAME

remin8
October 26th, 2005, 08:25 PM
I would just install Build-essiential & Anjuta and all its recomended bits. Using an IDE will save you time in the long run, I think. Also, you don't need std. Just use namespace std after your includes.

LordHunter317
October 26th, 2005, 08:28 PM
It would work if you made it void main()

I thinkNope, this is illegal in C++. Even if the code complies, the behavior is technically undefined.

RastaMahata,
Why are you returning an error state when there is no error?

darth_vector
October 27th, 2005, 02:45 AM
#include <iostream>

int main()
{
std::cout<<"Hello world!"<<endl;
return 1;
}
Compilation:
g++ FILE.gcc -o PROGRAMNAME

depending on how pedantic your compiler is this wont compile since endl is defined in the std namespace. you need:

std::cout<<"Hello world"<<std::endl;

its much easier to just put

using namespace std;

at the very top of your program, then you dont have to worry about namespaces.

oldmanstan
October 27th, 2005, 03:10 AM
Thanks for the help all, I think it's funny that there are so many different thoughts on how to code / compile such a simple program... very interesting.

p.s. I had anjuta installed already so I sat down and gave that a look-see, now using it.

kudu
October 27th, 2005, 04:09 AM
Thanks for the help all, I think it's funny that there are so many different thoughts on how to code / compile such a simple program... very interesting.

p.s. I had anjuta installed already so I sat down and gave that a look-see, now using it.

You were trying to use the C compiler (gcc) to compile C++ code (g++), that's all.

And it ain't a bad idea to get familiar with compiling/linking with the terminal rather than an IDE simply as an educational exercise. It's nice to know what's actually taking place under the hood. Good luck!

kudu

LordHunter317
October 27th, 2005, 05:21 AM
its much easier to just put

using namespace std;THis is terribly bad form, unless your program is very trivial, especially when you're declaring functions of the same name as the ones in std (common when writing algorithim template specializations)

That being said, for a program this trivial, it's ok. But it's better form to never have using at anything higher than function scope.

My8os
October 27th, 2005, 09:53 PM
Another way is:


using std::cout;

RastaMahata
October 28th, 2005, 02:05 AM
depending on how pedantic your compiler is this wont compile since endl is defined in the std namespace. you need:

std::cout<<"Hello world"<<std::endl;

its much easier to just put

using namespace std;

at the very top of your program, then you dont have to worry about namespaces.
](*,) ](*,) My bad, forgot to write using namespace std :(

And yeah, I wrote return 1 just for the heck of it.

cyberkoa
October 29th, 2005, 01:42 AM
I face the similar problem that a simpile C /C++ program won't compile.

My problem should be the one mentioned by LordHunter317


When you invoke "gcc" to have it compile and link a program, like you have, it doesn't pass the standard C++ libraries to the linker. Meaning it doesn't try to include all the system code you need to run a C++ program.

Running "gcc" as "g++" causes it to include all the standard C++ libraries, rectifying this. There are a few other minor differences between the two invocations, but that's by far the most important one.

Until now I still cannot set up an development environment for C++
The error list is too long , I extract the first few lines for reference, any help is welcomed. Thanks.



super@ubuntu:~/testprog$ g++-4.0 test.cpp
In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++config.h:35, from /usr/include/c++/4.0.2/iostream:43,
from test.cpp:1:
/usr/include/c++/4.0.2/i486-linux-gnu/bits/os_defines.h:39:22: error: features.h: No such file or directory
In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++locale.h:41, from /usr/include/c++/4.0.2/iosfwd:45,
from /usr/include/c++/4.0.2/ios:43,
from /usr/include/c++/4.0.2/ostream:44,
from /usr/include/c++/4.0.2/iostream:44,
from test.cpp:1:
/usr/include/c++/4.0.2/cstring:51:20: error: string.h: No such file or directoryIn file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++locale.h:42, from /usr/include/c++/4.0.2/iosfwd:45,
from /usr/include/c++/4.0.2/ios:43,
from /usr/include/c++/4.0.2/ostream:44,
from /usr/include/c++/4.0.2/iostream:44,
from test.cpp:1:
/usr/include/c++/4.0.2/cstdio:52:19: error: stdio.h: No such file or directory
In file included from /usr/include/c++/4.0.2/i486-linux-gnu/bits/c++locale.h:43

toojays
October 29th, 2005, 02:00 AM
Did you install the build-essential package?

LordHunter317
October 29th, 2005, 02:06 AM
Yeah, it looks like you somehow have the standard C++ headers installed, but not the C ones. I'm not exactly sure how that's possible (libc6-dev must be installed for libstdc++6-dev to function, and if that's not a dep, it's a bug) but try installing build-essential.

cyberkoa
October 29th, 2005, 08:28 PM
Did you install the build-essential package?

thx, I think this the reason.

I have installed and tested ,it works ! :)

Thanks to Lordhunter also :)

Another question , I would like to install the gtk2 development environment, which package should I install ?

I have searched the package list , could not find something like gtk2-dev

toojays
October 30th, 2005, 12:33 AM
Look for libgtk-2.0-dev.