undefined reference to...

undefined reference to...

am 20.08.2004 16:47:43 von Philippe De Neve

Hi,

I'm new to programming and I'm having a problem I don't understand:

I have 3 very simple files:

1) Integerclass.h containing :

class Integer{
int i;
public:
Integer(int j){
i=j;
}
void change(void);
};

2) Integerclass.cpp containing :


#include "Integerclass.h"

void Integer::change(void){
i=5;
}

3) my_prog.cpp containing

#include "Integerclass.h"

int main(){
Integer P(2);
P.change();
return 0;
};

When I compile the Integerclass.cpp file no errors are returned.

But when I compile and link this :

c++ -o my_prog my_prog.cpp

the output is :

demovideo3:/Projects/little_proggie# c++ -o my_prog my_prog.cpp
/tmp/ccsGvVWq.o: In function `main':
/tmp/ccsGvVWq.o(.text+0x1f): undefined reference to `Integer::change(void)'
collect2: ld returned 1 exit status
demovideo3:/Projects/little_proggie#

I do not understand where I'm making a mistake. I did the same on a windows
machine using VC++ and no errors where returned? Can anyone explain me what
I'm doing wrong? Any help is appreciated!

regards, Philippe.










-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: undefined reference to...

am 20.08.2004 19:32:03 von Steven Smith

--M9NhX3UHpAaciwkO
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

> 1) Integerclass.h containing :
..
> 2) Integerclass.cpp containing :
..
> 3) my_prog.cpp containing
..
> But when I compile and link this :
>=20
> c++ -o my_prog my_prog.cpp
That command says to compile my_prog.cpp to an object and use only
that object, plus the standard libraries, to build my_prog i.e.
ignore Integerclass.cpp completely. There are two obvious solutions:

1) Compile everything at once:

c++ -o my_prog my_prog.cpp Integerclass.cpp

2) Compile the source files seperately, and then link them together

c++ -c my_prog.cpp
c++ -c Integerclass.cpp
c++ -o my_prog my_prog.o Integerclass.o

Hope that helps.

Steven Smith,
sos22@cantab.net.
--=20
One day, I'm going to get an Alice-bot to answer all my email for me,
and see how long it takes people to notice.

--M9NhX3UHpAaciwkO
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)

iD8DBQFBJjWTO4S8/gLNrjcRAhf1AKDTpfsB+SeGFcYEItjzwQuT9CoHzQCg t6qF
7HlKyPWeeoFuOBpc6ley9og=
=EGkm
-----END PGP SIGNATURE-----

--M9NhX3UHpAaciwkO--
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: undefined reference to...

am 20.08.2004 19:47:09 von Ray Olszewski

At 04:47 PM 8/20/2004 +0200, Philippe De Neve wrote:
>Hi,
>
>I'm new to programming and I'm having a problem I don't understand:
>
>I have 3 very simple files:
>
>1) Integerclass.h containing :
>
>class Integer{
> int i;
>public:
> Integer(int j){
> i=j;
> }
> void change(void);
>};
>
>2) Integerclass.cpp containing :
>
>
>#include "Integerclass.h"
>
>void Integer::change(void){
> i=5;
>}
>
>3) my_prog.cpp containing
>
>#include "Integerclass.h"
>
>int main(){
> Integer P(2);
> P.change();
> return 0;
>};
>
>When I compile the Integerclass.cpp file no errors are returned.
>
>But when I compile and link this :
>
>c++ -o my_prog my_prog.cpp
>
>the output is :
>
>demovideo3:/Projects/little_proggie# c++ -o my_prog my_prog.cpp
>/tmp/ccsGvVWq.o: In function `main':
>/tmp/ccsGvVWq.o(.text+0x1f): undefined reference to `Integer::change(void)'
>collect2: ld returned 1 exit status
>demovideo3:/Projects/little_proggie#
>
>I do not understand where I'm making a mistake. I did the same on a windows
>machine using VC++ and no errors where returned? Can anyone explain me what
>I'm doing wrong? Any help is appreciated!
>
>regards, Philippe.

Since VC++ is GUI based, I doubt you really did the "same" using it. I
mention this only because as a beginner, you need too learn the importance
of giving exact reports, not impressionistic ones, when asking for help or
advice.

Your problem is that the way you compile using c++, you do not give the
linker (ld, invoked by c++ to link after the compile is done) any
information about where to find the intermediate code for Integerclass.cpp.
If you run c++ this way instead --

c++ -o my_prog my_prog.cpp Integerclass.cpp

-- you should get a successful compile/link. (At least I do here, though
when run the program doesn't actually do anything visible, which looks to
be consistent with what you've actually coded.) But this is just a
quick-and-dirty solution to your immediate problem, not the real answer you
need.

More generally, what you are missing is the use of "make" to manage
multi-file compiles with gcc, c++, and the rest of the Gnu compiler set. I
haven't used VC++ in years, but I expect it has an equivalent to make
hidden somwehere in the programming environment it offers (I know the
Borland C++ compiler suite does; I've used that more than VC++).

An e-mail is no place for a tutorial on make, but there are plenty of
resources that introduce it and its use. Look around in the usual places.

BTW, how did you compile Integerclass.cpp by itself? Since it is not a
standalone program, you'd need to use some of the c++ switches to make the
compile work (the natural one is "c++ -c Integerclass.cpp"), and that
suggests you know more about using c++ than I'd been thinking here.



-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs