compilation problems!!

S

seema

I have problem compiling this program using GCC(g++)

//bye.h --header file
#ifndef BYE_H
#define BYE_H
void display();
#endif

//bye.C -------- Source
#include "bye.h"
#include <iostream.h>

void display()
{
cout <<"Display the stuff ";

}

//main.cpp -----source
#include <iostream.h>
#include <string.h>
#include "bye.h"

class myclass
{
//mystruct x;
friend void display();
public:
myclass(){
display();
}
};

int main()
{
myclass me;

}

If I compile using g++ main.cpp I get the following error,

/tmp/ccG3OvUb.o: In function `myclass::myclass[in-charge]()':
/tmp/ccG3OvUb.o(.gnu.linkonce.t._ZN7myclassC1Ev+0x7): undefined
reference to `display()'
collect2: ld returned 1 exit status

Can some body explain how to do it
thanks in advance,
Seema Rao
 
P

Phlip

seema said:
I have problem compiling this program using GCC(g++)
//bye.C -------- Source

The .C is probably triggering g++ to compile this as a C module.
#include "bye.h"
#include <iostream.h>

An unrelated issue; you should switch to said:
void display()
{
cout <<"Display the stuff ";

I suspect g++ can then compile C++ things in a C module. But that is
platform-specific, so you should ask a g++ forum.
class myclass
{
//mystruct x;
friend void display();
public:
myclass(){
display();
}
};

Can you also just call display() directly from main(), without the friend?
/tmp/ccG3OvUb.o: In function `myclass::myclass[in-charge]()':
/tmp/ccG3OvUb.o(.gnu.linkonce.t._ZN7myclassC1Ev+0x7): undefined
reference to `display()'

Google for "name mangling". I suspect the .C extension has mangled the name
display() for C, not C++. Linkers generally only link mangled names that
match.
 
V

Victor Bazarov

seema said:
[..]
If I compile using g++ main.cpp I get the following error,

/tmp/ccG3OvUb.o: In function `myclass::myclass[in-charge]()':
/tmp/ccG3OvUb.o(.gnu.linkonce.t._ZN7myclassC1Ev+0x7): undefined
reference to `display()'
collect2: ld returned 1 exit status

Can some body explain how to do it

Somebody in 'gnu.g++.help' should be able to. You must be compiling
those translation units separately. You need to "link" them together
during linking. How to do that is compiler-specific and off-topic.

V
 
T

Thomas Tutone

seema said:
I have problem compiling this program using GCC(g++)

//bye.h --header file
#ifndef BYE_H
#define BYE_H
void display();
#endif

//bye.C -------- Source

change the name of the file to bye.cpp - it will make your life much
easier if you are consistent.
#include "bye.h"
#include <iostream.h>

Change the above line to:
#include said:
void display()
{

add the following line here:
using namespace std;
cout <<"Display the stuff ";

}

//main.cpp -----source
#include <iostream.h>

Change the above line to:
#include said:
#include <string.h>

Delete the above line - you don't need that header in this program.
#include "bye.h"

class myclass
{
//mystruct x;
friend void display();

Delete the above line - you don't need it in this program.
public:
myclass(){
display();
}
};

int main()
{
myclass me;

}

If I compile using g++ main.cpp I get the following error,

/tmp/ccG3OvUb.o: In function `myclass::myclass[in-charge]()':
/tmp/ccG3OvUb.o(.gnu.linkonce.t._ZN7myclassC1Ev+0x7): undefined
reference to `display()'
collect2: ld returned 1 exit status

Can some body explain how to do it

<implementation specific/>

If you are on Linux, compile it as follows:

g++ -Wall -Wextra -pedantic main.cpp bye.cpp -o seema

and execute it by typing:

seema

If you are on Windows, compile it as follows:

g++ -Wall -Wextra -pedantic main.cpp bye.cpp -o seema.exe

and execute it by typing:

seema

</implementation specific>

Hope that helps.

Best regards,

Tom
 
R

Rolf Magnus

Thomas said:
If you are on Linux, compile it as follows:

g++ -Wall -Wextra -pedantic main.cpp bye.cpp -o seema

-Wextra is only available on quite recent versions. Older ones want -W
instead. I would also add -ansi.
and execute it by typing:

seema

On a sane installation, this won't work. Better try:

./seema
 
T

Thomas Tutone

Rolf said:
-Wextra is only available on quite recent versions. Older ones want -W
instead. I would also add -ansi.


On a sane installation, this won't work. Better try:

./seema

Quite right. Thanks for the correction.

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top