|
Home > Archive > Unix Programming > January 2004 > is gcc a c++ compiler
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
is gcc a c++ compiler
|
|
|
| Hi call,
"man gcc" stated that gcc is a c++ compiler. However, I have the following
*VERY SIMPLE* code in c++ that doesn't compile in gcc. It compiles fine in
VC++ 7.0. I have no clue why. TIA
Error message:
gcc test.cc
/tmp/ccMJrkgw.o(.text+0x18): In function `main':
: undefined reference to `Point::Point[in-charge]()'
/tmp/ccMJrkgw.o(.text+0x3f): In function `main':
: undefined reference to `Point::Point[in-charge](double, double)'
/tmp/ccMJrkgw.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status
test.cc:
#include "Point.h"
#include <stdio.h>
int main()
{
Point a;
Point b(3, 4);
//printf("Distance is %f", a.getDistance(b));
return 0;
}
Point.h:
#ifndef Point_H
#define Point_H
class Point
{
private:
double m_x, m_y;
public:
Point();
Point(double x, double y);
double getDistance(Point p);
void setPoint(double x, double y);
double getX();
double getY();
};
#endif
Point.cc:
#include "Point.h"
#include <math.h>
Point::Point()
{
setPoint(0, 0);
}
Point::Point(double x, double y)
{
setPoint(x, y);
}
void Point::setPoint(double x, double y)
{
m_x = x;
m_y = y;
}
double Point::getX()
{
return m_x;
}
double Point::getY()
{
return m_y;
}
double Point::getDistance(Point p)
{
return pow(pow(p.getX() - m_x, 2) + pow(p.getY() - m_y, 2), 0.5);
}
| |
| joe@invalid.address 2004-01-23, 5:01 pm |
| Tommy <tommy@nospam.com> writes:
quote:
> "man gcc" stated that gcc is a c++ compiler. However, I have the
> following *VERY SIMPLE* code in c++ that doesn't compile in gcc. It
> compiles fine in VC++ 7.0. I have no clue why. TIA
gcc and g++ are front ends for the GNU compilation system, which will
compile a number of different languages. If you want to compile C++
code, then invoke it as g++ rather than gcc.
Joe
| |
|
| > gcc and g++ are front ends for the GNU compilation system, which willquote:
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
Using g++ did get rid of the 3rd error. The first 2 errors remain.
joe@invalid.address wrote:
quote:
> Tommy <tommy@nospam.com> writes:
>
>
> gcc and g++ are front ends for the GNU compilation system, which will
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
>
> Joe
| |
| joe@invalid.address 2004-01-23, 5:01 pm |
| Tommy <tommy@nospam.com> writes:
quote:
> "man gcc" stated that gcc is a c++ compiler. However, I have the
> following *VERY SIMPLE* code in c++ that doesn't compile in gcc. It
> compiles fine in VC++ 7.0. I have no clue why. TIA
gcc and g++ are front ends for the GNU compilation system, which will
compile a number of different languages. If you want to compile C++
code, then invoke it as g++ rather than gcc.
Joe
| |
|
| > gcc and g++ are front ends for the GNU compilation system, which willquote:
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
Using g++ did get rid of the 3rd error. The first 2 errors remain.
joe@invalid.address wrote:
quote:
> Tommy <tommy@nospam.com> writes:
>
>
> gcc and g++ are front ends for the GNU compilation system, which will
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
>
> Joe
| |
| those who know me have no need of my name 2004-01-23, 5:01 pm |
| in comp.unix.programmer i read:
quote:
>
>Using g++ did get rid of the 3rd error. The first 2 errors remain.
naturally. where do you think gcc is going to obtain the implementation of
your Point object -- thin air? you must tell it ...
g++ test.cc point.cc
or
g++ -c point.cc && g++ test.cc point.o
or
g++ -c point.cc && g++ -c test.cc && g++ test.o point.o
--
a signature
| |
| those who know me have no need of my name 2004-01-23, 5:01 pm |
| in comp.unix.programmer i read:
quote:
>
>Using g++ did get rid of the 3rd error. The first 2 errors remain.
naturally. where do you think gcc is going to obtain the implementation of
your Point object -- thin air? you must tell it ...
g++ test.cc point.cc
or
g++ -c point.cc && g++ test.cc point.o
or
g++ -c point.cc && g++ -c test.cc && g++ test.o point.o
--
a signature
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:01 pm |
| joe@invalid.address writes:
quote:
>
> gcc and g++ are front ends for the GNU compilation system, which will
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
The only difference is that g++ links with libstdc++ by default,
whereas gcc doesn't. Both will happily compile C++ source into object
files.
--
Måns Rullgård
mru@kth.se
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:01 pm |
| joe@invalid.address writes:
quote:
>
> gcc and g++ are front ends for the GNU compilation system, which will
> compile a number of different languages. If you want to compile C++
> code, then invoke it as g++ rather than gcc.
The only difference is that g++ links with libstdc++ by default,
whereas gcc doesn't. Both will happily compile C++ source into object
files.
--
Måns Rullgård
mru@kth.se
| |
| Paul Pluzhnikov 2004-01-23, 5:01 pm |
| mru@kth.se (Måns Rullgård) writes:
quote:
> The only difference is that g++ links with libstdc++ by default,
> whereas gcc doesn't.
That is not the *only* difference.
quote:
> Both will happily compile C++ source into object files.
True. However, given a 'C' source, the object files produced by
gcc and g++ will be different (and the language accepted will be
different too).
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Paul Pluzhnikov 2004-01-23, 5:01 pm |
| mru@kth.se (Måns Rullgård) writes:
quote:
> The only difference is that g++ links with libstdc++ by default,
> whereas gcc doesn't.
That is not the *only* difference.
quote:
> Both will happily compile C++ source into object files.
True. However, given a 'C' source, the object files produced by
gcc and g++ will be different (and the language accepted will be
different too).
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:01 pm |
| Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:
quote:
>
> True. However, given a 'C' source, the object files produced by
> gcc and g++ will be different (and the language accepted will be
> different too).
The 'gcc' program will look at the suffix of the filename to select
compiler. If it is .c, a C compiler will be used. Suffixes of .cc,
..C, .cxx or .cpp, and possibly some more, will cause the C++ compiler
to be selected. I'm not sure how g++ behaves with these things.
--
Måns Rullgård
mru@kth.se
| |
| =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= 2004-01-23, 5:01 pm |
| Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:
quote:
>
> True. However, given a 'C' source, the object files produced by
> gcc and g++ will be different (and the language accepted will be
> different too).
The 'gcc' program will look at the suffix of the filename to select
compiler. If it is .c, a C compiler will be used. Suffixes of .cc,
..C, .cxx or .cpp, and possibly some more, will cause the C++ compiler
to be selected. I'm not sure how g++ behaves with these things.
--
Måns Rullgård
mru@kth.se
| |
| Valentin Nechayev 2004-01-23, 5:01 pm |
| >>> Tommy wrote:
[QUOTE][color=darkred]
T> Using g++ did get rid of the 3rd error. The first 2 errors remain.
Your message is meaningless unless exact gcc version and command string
is specified.
-netch-
| |
| Valentin Nechayev 2004-01-23, 5:01 pm |
| >>> Tommy wrote:
[QUOTE][color=darkred]
T> Using g++ did get rid of the 3rd error. The first 2 errors remain.
Your message is meaningless unless exact gcc version and command string
is specified.
-netch-
| |
| Peter Ammon 2004-01-23, 5:02 pm |
| Tommy wrote:
quote:
> Hi call,
>
> "man gcc" stated that gcc is a c++ compiler. However, I have the following
> *VERY SIMPLE* code in c++ that doesn't compile in gcc. It compiles fine in
> VC++ 7.0. I have no clue why. TIA
>
> Error message:
> gcc test.cc
> /tmp/ccMJrkgw.o(.text+0x18): In function `main':
> : undefined reference to `Point::Point[in-charge]()'
> /tmp/ccMJrkgw.o(.text+0x3f): In function `main':
> : undefined reference to `Point::Point[in-charge](double, double)'
> /tmp/ccMJrkgw.o(.eh_frame+0x11): undefined reference to
> `__gxx_personality_v0'
> collect2: ld returned 1 exit status
>
>
You want
g++ Point.cc test.cc
It's generating a linking error because you didn't compile the file Point.cc
[...]
| |
| Peter Ammon 2004-01-23, 5:02 pm |
| Tommy wrote:
quote:
> Hi call,
>
> "man gcc" stated that gcc is a c++ compiler. However, I have the following
> *VERY SIMPLE* code in c++ that doesn't compile in gcc. It compiles fine in
> VC++ 7.0. I have no clue why. TIA
>
> Error message:
> gcc test.cc
> /tmp/ccMJrkgw.o(.text+0x18): In function `main':
> : undefined reference to `Point::Point[in-charge]()'
> /tmp/ccMJrkgw.o(.text+0x3f): In function `main':
> : undefined reference to `Point::Point[in-charge](double, double)'
> /tmp/ccMJrkgw.o(.eh_frame+0x11): undefined reference to
> `__gxx_personality_v0'
> collect2: ld returned 1 exit status
>
>
You want
g++ Point.cc test.cc
It's generating a linking error because you didn't compile the file Point.cc
[...]
| |
| joe durusau 2004-01-23, 5:02 pm |
|
Peter Ammon wrote:
quote:
> Tommy wrote:
>
>
> You want
> g++ Point.cc test.cc
>
> It's generating a linking error because you didn't compile the file Point.cc
>
> [...]
True enough, but the OP should also be warned that _compiling_
and _linking_ are two different things. gcc and g++ can both
compile without difficulty, but if you use gcc to drive the link
stage, it will not send the correct commands to the linker.
It is a good practice to use g++ to both compile and link,
but it is not really necessary.
For example, the sequence:
gcc -c point.cc
gcc -c test.cc
g++ -o point point.o test.o
would work,
as would the command Peter suggested, but
gcc point.cc test.cc
would cause trouble.
See the man pages or detailed documentation for more info.
Speaking only for myself,
Joe Durusau
| |
| joe durusau 2004-01-23, 5:02 pm |
|
Peter Ammon wrote:
quote:
> Tommy wrote:
>
>
> You want
> g++ Point.cc test.cc
>
> It's generating a linking error because you didn't compile the file Point.cc
>
> [...]
True enough, but the OP should also be warned that _compiling_
and _linking_ are two different things. gcc and g++ can both
compile without difficulty, but if you use gcc to drive the link
stage, it will not send the correct commands to the linker.
It is a good practice to use g++ to both compile and link,
but it is not really necessary.
For example, the sequence:
gcc -c point.cc
gcc -c test.cc
g++ -o point point.o test.o
would work,
as would the command Peter suggested, but
gcc point.cc test.cc
would cause trouble.
See the man pages or detailed documentation for more info.
Speaking only for myself,
Joe Durusau
|
|
|
|
|