PDA

View Full Version : how to set up gcc or g++ on ubuntu



minnew
May 9th, 2006, 03:10 AM
Ok, I got Ubuntu loaded and running, and downloaded sciTE (as well as Anjuta). However, when I tried to compile a simple C or C++ program in either sciTE or Anjuta (or within Terminal using command lines) I got loads of "FILE NOT FOUND" errors, as if the path to the include files are not known. How do I set up gcc/g++ in Ubuntu, esp. regarding the include paths?

thanks

jazzmuzik
May 9th, 2006, 03:20 AM
Install the build-essential package.


sudo apt-get install build-essential

welkin
May 9th, 2006, 04:08 AM
Install the build-essential package.


sudo apt-get install build-essential

I tried that, and still I get errors when I try to compile my hello world program as follows:

$ gcc helloworld.cpp -o hello
/tmp/cc3nQWma.o: In function `main':
helloworld.cpp:(.text+0x25): undefined reference to `std::cout'
helloworld.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/cc3nQWma.o: In function `__tcf_0':
helloworld.cpp:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc3nQWma.o: In function `__static_initialization_and_destruction_0(int, int)':
helloworld.cpp:(.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc3nQWma.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

My helloworld.cpp is


//My almost first program in C++
#include <iostream>

//main begins program execution
int main()
{
std::cout << "Hello World!\n";

return 0; //indicate that prog ended successfully

} //end main fn


What am I doing wrong?

tonyr
May 9th, 2006, 04:23 AM
g++ uses a different front end than gcc. Try


g++ helloworld.cpp -o hello


instead.

welkin
May 9th, 2006, 04:30 AM
g++ uses a different front end than gcc. Try


g++ helloworld.cpp -o hello


instead.

Thank you!

paul_whan
May 9th, 2006, 06:28 AM
yes i have a similar problem, however i get those errors when using this command:

shell:
whanp@ubuntu:~/mytest/c++/world$ gcc hello.cc -o hello
/tmp/ccfQrcNT.o: In function `main':
hello.cc:(.text+0x25): undefined reference to `std::cout'
hello.cc:(.text+0x2a): undefined reference to `std::basic_ostream<char, std::cha r_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<c har, std::char_traits<char> >&, char const*)'
/tmp/ccfQrcNT.o: In function `__tcf_0':
hello.cc:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccfQrcNT.o: In function `__static_initialization_and_destruction_0(int, int )':
hello.cc:(.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccfQrcNT.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

my code for my hello.cc is this:

#include <iostream>

using namespace std;

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


I also have run the following command:

sudo apt-get install build-essential

lnostdal
May 9th, 2006, 08:15 AM
did you try what tonyr mentioned?

minnew
May 9th, 2006, 12:50 PM
thanks for the tip...g++ is up and running...however, what exactly does
sudo apt-get install build-essential
do?

thumper
May 9th, 2006, 02:05 PM
It is a meta-package that just depends on other packages.

Installing it also installs all the dependant packages.

minnew
May 9th, 2006, 02:36 PM
It is a meta-package that just depends on other packages.

Installing it also installs all the dependant packages.

As a newbie, how would I have known to install this?

thumper
May 9th, 2006, 03:06 PM
As a newbie, how would I have known to install this?
It has been mentioned here about 100 times?

I guess that the decision was made at some stage that gcc/g++ should not be installed by default. I don't really know the full background to this.

I have found the irc channels #ubuntu and #kubuntu quite helpful.

paul_whan
May 9th, 2006, 07:59 PM
yes I have tried what tonyr said, i have tried:

g++ hello.cpp

but that is not my problem right now. I am wondering about why:

gcc hello.cc

is not working. I am trying to compile .cc files, not .cpp. I was able to compile .cc on other linux: Mandrake, Red Hat, CentOs, and Fedora.

tonyr
May 9th, 2006, 10:25 PM
I have no answer at the moment, but this is definitely weird. man gcc
says that .cc extensions should be recognized as c++ files. It also says that
the -x option can be used to specify a front end language explicitly, so
that gcc -x c++ foo.cc should force the right front end processing.

Guess what?!?!? The -x c++ option doesn't work either.

I'd say this is a bug, but I don't know if it's a bug against gcc or the
man page.

A temporary fix might be to mess with the way gcc appears in /usr/bin.Currently it is a link to /usr/bin/gcc-4.0. Make no mistake,
this would be a hack until a better answer comes along:


sudo rm /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.0 /usr/bin/gcc


I can't swear that this will work, especially since .c files would now
be handled by g++. The Ubuntu powers or maybe the GCC
powers may have decreed that henceforth gcc and g++
should be segregated. Note that the Dapper version of gcc/g++ is 4.03.

tonyr
May 9th, 2006, 10:33 PM
A temporary fix might be to mess with the way gcc appears in /usr/bin.Currently it is a link to /usr/bingcc-4.0. Make no mistake,
this would be a hack until a better answer comes along:


sudo rm /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.0 /usr/bin/gcc


I can't swear that this will work, especially since .c files would now
be handled by g++. The Ubuntu powers or maybe the GCC
powers may have decreed that henceforth gcc and g++
should be segregated. Note that the Dapper version of gcc/g++ is 4.03.

I tried it and it works for .c files and .cc files. I'll try to find some better info
on why it is the way it is in Ubuntu.

tonyr
May 10th, 2006, 01:27 AM
I submitted this as Bug #43916 against gcc-4.0(Ubuntu). I'm not
convinced that it's a bug, but maybe someone will respond with
an explanation.

tonyr
May 10th, 2006, 01:56 AM
Well, changing the name from hello.cc to hello.cpp doesn't
change anything. That indicates to me that gcc isn't going to handle c++
files at all. The man page says it should. At this point I would have to guess
that Dapper/gcc-4.0 powers decided that gcc would not handle
c++ files. Goodbye backward compatibility.

Zdravko
May 10th, 2006, 07:02 AM
GCC compiles *.c source files.
G++ compiles *.cpp source files.
I don't think there is a bug. And yes - *.cc is also another reference of *.cpp source :)

toojays
May 10th, 2006, 12:31 PM
First, minnew's question, "As a newbie, how would I have known to install this?" I'm not aware of it being mentioned anywhere in the docs of Ubuntu 5.10, so you were right to come to this forum (although a simple search would have got your answer faster). In Ubuntu 6.06, advice about build-essential will be in the Ubuntu Desktop Guide, under Common Tasks->Programming.

Now, about the compilation. TonyR, it's not a bug. gcc does recognise the .cc extension as belonging to C++ files. It compiles them fine. The "undefined reference" errors which people are getting are link errors, not compile errors. Using g++ fixes the problem because the g++ frontend is set to link against the standard C++ library, while the gcc frontend is not setup to link against this library.

To go over this in a touch more detail. When you run "gcc helloworld.cpp -o hello", two things happen. First gcc compiles helloworld.cpp into an object file. Then it links the object file against some system libraries to create an executable. The compilation step is equivalent to "gcc -c -o helloworld.o helloworld.cpp". The link step is roughly equivalent to "gcc -o helloworld helloworld.o". It's this second step which fails. This is because gcc does not know to link against the standard C++ library. It's not meant to do this by default, because C is not C++, and we have g++ for that. But if you want to do it anyway, just to see, you can pass gcc the option "-lstdc++", which means "link against the standard C++ library".

Does this explanation help?

Oh, and by the way: if you have all your code in a single file (e.g. hello.cc) you can just compile it by running "make hello". :)

tonyr
May 10th, 2006, 05:38 PM
Now, about the compilation. TonyR, it's not a bug. gcc does recognise the .cc extension as belonging to C++ files. It compiles them fine. The "undefined reference" errors which people are getting are link errors, not compile errors. Using g++ fixes the problem because the g++ frontend is set to link against the standard C++ library, while the gcc frontend is not setup to link against this library.


Thanks for the detail. That's exactly the response I was looking for (and actually
dug out last night). It looks like the recognition/selection of which invocation
to use (gcc or g++) is in make instead of gcc. That's the part I didn't know.


Oh, and by the way: if you have all your code in a single file (e.g. hello.cc) you can just compile it by running "make hello".


paul-whan, the relevant part of the gcc man page is
Compiling C++ Programs.

But what about this:


I was able to compile .cc on other linux: Mandrake, Red Hat, CentOs, and Fedora.


Are those other distros just being nice during gcc/g++ installation/configuration
to establish link requirements in some 'non-standard' or 'local-installation-policy'
way, or is this a universal gcc/g++ policy change?

Maybe it's a RedHat vs. Debian thing. Mandrake and Fedora are in the RedHat
camp. I haven't looked at CentOs closely, but it looks to be a RedHat friend, too.

paul_whan
May 11th, 2006, 12:44 AM
that helps a bit more, but usually you wont use

gcc with a *.cpp file.

I did try getting g++ to work, which was fine. The way I checked this was by copying my hello.cc file and making it as a hello.cpp. After doing that i just went:

g++ hello.cpp

and i was able to create an a.out file. Which would runt he program fine. So now i want to know is there a way to set it so that you wont have to add that option everytime you compile with gcc?

tonyr
May 11th, 2006, 01:16 AM
So now i want to know is there a way to set it so that you wont have to add that option everytime you compile with gcc?

Well, I'm not sure what you are asking. I don't think there is any magic that will
make gcc automatically use libstdc++ for linking. Your choices are

1. make hello.cc

2. gcc -lstdc++ hello.cc

jazzmuzik
May 11th, 2006, 06:55 PM
It has been mentioned here about 100 times?

I guess that the decision was made at some stage that gcc/g++ should not be installed by default. I don't really know the full background to this.

I have found the irc channels #ubuntu and #kubuntu quite helpful.

Whatever the reason, not including the gcc compiler is very Microsoftish, i.e., dumbing down the OS for dumb consumers to make them still dumber. I'm obviously suspicious of the decision not to include it.

Zdravko
May 11th, 2006, 07:31 PM
jazzmuzik (http://www.ubuntuforums.org/member.php?u=82676), I share your opinion. I reckon, that gcc/g++ must be hard integrated in the very deep heart of Ubuntu. Programming in C++ is the main purpose of Linux for me.

tageiru
May 11th, 2006, 08:51 PM
Whatever the reason, not including the gcc compiler is very Microsoftish, i.e., dumbing down the OS for dumb consumers to make them still dumber. I'm obviously suspicious of the decision not to include it.
Yes, but the really disturbing thing is that Ubuntu does not include a coat hanger, talk about dumbed down!

Could we please get rid of the "dumbing down" rants? Smart people do not necessarily need a compiler in order to use the computer.

jazzmuzik
May 12th, 2006, 02:38 PM
Could we please get rid of the "dumbing down" rants? Smart people do not necessarily need a compiler in order to use the computer.

That's not my argument, which I didn't make clear. The compiler should be there, installed and ready to go whether any user needs it or not.