Thread View: gnu.g++.help
4 messages
4 total messages
Started by keith
Tue, 26 Jan 1993 14:54
Problem linking with g++ class
Author: keith
Date: Tue, 26 Jan 1993 14:54
Date: Tue, 26 Jan 1993 14:54
133 lines
2725 bytes
2725 bytes
I am having difficulties linking to a very simple class with g++.
The program compiles, links and runs properly with BC++ 3.1.
I am running gcc 2.3.3
with libg++ 2.3 on a SUN OS 4.1.1. Gcc is installed in a directory
other than /usr/local.
I have tried to make the class as simple as possible and based its
design on Complex.h in libg++. I have tried a similiar test program
with the Complex.h class and it compiled, linked and ran properly with
g++. The 2 test files and the class I am trying to link to are
included at the end of this file.
When I type the command
g++ test1.c test2.c -lm
I get the error message
ld: Undefined symbol
___7mpfloatRC7mpfloat
___7mpfloatd
___ml__FRC7mpfloatT0
__$_7mpfloat
collect: /usr/bin/ld returned 2 exit status
I am obviously missing something simple. Could someone please point
it out to me.
Thanks in advance.
Keith S Cover
Physics, UBC
keith@msmri.med.ubc.ca
test1.c
=======
#include <iostream.h>
#include <math.h>
#include "mpf14.h"
mpfloat square(mpfloat a);
main() {
mpfloat a = 4;
mpfloat b = square(a);
return 0;
}
test2.c
=======
#include "mpf14.h"
mpfloat square(mpfloat a) {
return( a*a );
}
mpf14.h
=======
#ifndef _mpfloat_h
#ifdef __GNUG__
#pragma interface
#endif
#define _mpfloat_h 1
// INCLUDE FILES
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
class mpfloat {
protected:
double num;
public:
// constructors and destructors
mpfloat();
mpfloat(const mpfloat& x);
mpfloat(const double x);
~mpfloat();
// get value
double getnum() const;
};
// INLINE FUNCTION PROTOTYPES
// Arithmetic operators
mpfloat operator * ( const mpfloat& x, const mpfloat& y);
// INLINE MEMBERS
// constructors
inline mpfloat::mpfloat() {
}
inline mpfloat::mpfloat(const mpfloat& x) :num(x.getnum()) {
}
inline mpfloat::mpfloat(const double x) {
num = x;
}
inline mpfloat::~mpfloat() {
}
// Get number
inline double mpfloat::getnum() const {
return(num);
}
// Inline Functions
inline mpfloat operator * ( const mpfloat& x, const mpfloat& y) {
return mpfloat( x.getnum()*y.getnum() );
}
#endif // _mpfloat_h
Re: Problem linking with g++ class
Author: Leif Lonnblad
Date: Tue, 26 Jan 1993 18:43
Date: Tue, 26 Jan 1993 18:43
20 lines
1380 bytes
1380 bytes
I think your problem is the '#pragma interface' directive in the mpf14.h file. Among other things this instructs gcc not to produce code for inline functions defined in this file, unless there is a corresponding '#pragma implementation' directive in the '.c' file currently beeing compiled. (do 'man gcc' for details) This should not matter if all the inline functions actually were compiled inline, but as you haven't declared them inline in the class declaration, they will not even be considered for inlining by gcc (I guess it would be appropriate if gcc warned about this). Leif --------------------------------------------------------------- Leif Lonnblad, DESY (-T-), Notkestr. 85, 2000 Hamburg 52, FRG phone: +49-40-8998-2048, fax: +49-40-8998-2777 e-mail: <lonnblad@ips102.desy.de>, <leif@thep.lu.se>
Re: Problem linking with g++ class
Author: HONGLIN YE
Date: Tue, 26 Jan 1993 19:32
Date: Tue, 26 Jan 1993 19:32
150 lines
3304 bytes
3304 bytes
Hi,
I don't think you can get an answer from this news group for that
problen. There have been many help requests on that matter in the group,
but people simply ignored or maybe they don't know the answer yet.
In article <1k3jc2INNjnk@iskut.ucs.ubc.ca> keith@msmri.med.ubc.ca writes:
>I am having difficulties linking to a very simple class with g++.
>The program compiles, links and runs properly with BC++ 3.1.
>I am running gcc 2.3.3
>with libg++ 2.3 on a SUN OS 4.1.1. Gcc is installed in a directory
>other than /usr/local.
>I have tried to make the class as simple as possible and based its
>design on Complex.h in libg++. I have tried a similiar test program
>with the Complex.h class and it compiled, linked and ran properly with
>g++. The 2 test files and the class I am trying to link to are
>included at the end of this file.
>When I type the command
> g++ test1.c test2.c -lm
>I get the error message
> ld: Undefined symbol
> ___7mpfloatRC7mpfloat
> ___7mpfloatd
> ___ml__FRC7mpfloatT0
> __$_7mpfloat
> collect: /usr/bin/ld returned 2 exit status
>I am obviously missing something simple. Could someone please point
>it out to me.
>Thanks in advance.
>Keith S Cover
>Physics, UBC
>keith@msmri.med.ubc.ca
>test1.c
>=======
>#include <iostream.h>
>#include <math.h>
>#include "mpf14.h"
>mpfloat square(mpfloat a);
>main() {
> mpfloat a = 4;
> mpfloat b = square(a);
> return 0;
>}
>test2.c
>=======
>#include "mpf14.h"
>mpfloat square(mpfloat a) {
> return( a*a );
>}
>mpf14.h
>=======
>#ifndef _mpfloat_h
>#ifdef __GNUG__
>#pragma interface
>#endif
>#define _mpfloat_h 1
>// INCLUDE FILES
>#include <iostream.h>
>#include <math.h>
>#include <stdlib.h>
>class mpfloat {
>protected:
> double num;
>public:
> // constructors and destructors
> mpfloat();
> mpfloat(const mpfloat& x);
> mpfloat(const double x);
> ~mpfloat();
> // get value
> double getnum() const;
>};
>// INLINE FUNCTION PROTOTYPES
>// Arithmetic operators
>mpfloat operator * ( const mpfloat& x, const mpfloat& y);
>// INLINE MEMBERS
>// constructors
>inline mpfloat::mpfloat() {
>}
>inline mpfloat::mpfloat(const mpfloat& x) :num(x.getnum()) {
>}
>inline mpfloat::mpfloat(const double x) {
> num = x;
>}
>inline mpfloat::~mpfloat() {
>}
>// Get number
>inline double mpfloat::getnum() const {
> return(num);
>}
>// Inline Functions
>inline mpfloat operator * ( const mpfloat& x, const mpfloat& y) {
> return mpfloat( x.getnum()*y.getnum() );
>}
>#endif // _mpfloat_h
Re: Problem linking with g++ class
Author: Ab Wilson
Date: Thu, 28 Jan 1993 11:36
Date: Thu, 28 Jan 1993 11:36
137 lines
3750 bytes
3750 bytes
I usually don't post answers to querries in this news group because
mostly the questions are stupid. This is no exception but I was
prompted to reply by the message:
Hi,
I don't think you can get an answer from this news group for that
problen. There have been many help requests on that matter in the group,
but people simply ignored or maybe they don't know the answer yet.
Posted by honglin@apmaths.uwo.ca (HONGLIN YE).
Ok. This is what you should have:
>>>test1.c:
#include <iostream.h>
#include <math.h>
#include "mpf14.h"
mpfloat square(mpfloat a);
main() {
mpfloat a = 4;
mpfloat b = square(a);
return 0;
}
>>>test2.c
#include "mpf14.h"
mpfloat square(mpfloat a) {
return( a*a );
}
>>>mpf14.c
#include "mpf14.h"
>>>mpf14.h
#ifndef _mpfloat_h
#ifdef __GNUG__
#pragma interface
#endif
#define _mpfloat_h 1
// INCLUDE FILES
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
class mpfloat {
protected:
double num;
public:
// constructors and destructors
mpfloat();
mpfloat(const mpfloat& x);
mpfloat(const double x);
~mpfloat();
// get value
double getnum() const;
};
// INLINE FUNCTION PROTOTYPES
// Arithmetic operators
mpfloat operator * ( const mpfloat& x, const mpfloat& y);
// INLINE MEMBERS
// constructors
inline mpfloat::mpfloat() {
}
inline mpfloat::mpfloat(const mpfloat& x) :num(x.getnum()) {
}
inline mpfloat::mpfloat(const double x) {
num = x;
}
inline mpfloat::~mpfloat() {
}
// Get number
inline double mpfloat::getnum() const {
return(num);
}
// Inline Functions
inline mpfloat operator * ( const mpfloat& x, const mpfloat& y) {
return mpfloat( x.getnum()*y.getnum() );
}
#endif // _mpfloat_h
The result:
242 :-) g++ test1.c test2.c mpf14.c -lm
243 :-) ls
a.out mpf14.c mpf14.h test1.c test2.c
244 :-) a.out
245 :-)
Now for future reference, in G++, whenever you have a header file
containing either inline function definitions, or classes with virtual
functions (or derived from classes with virtual functions), you must
ALWAYS have a corresponding .c file.
Why? Because symbols will be generated from this file (either vtables,
or bodies for the inline functions). Fine you say, but why don't the
symbols end up in one of the src file the .h is included in? Ans:
because you said #pragma interface. Why did you say #pragma interface?
Because if you didn't, then G++ would generate symbols in ALL the
src files that include the .h. Whats wrong with that you ask? Ans: you
get multiply defined symbols. How do I prevent this you ask? Ans: you
use #pragma interface. But then G++ won't generate the symbols so what
do I do? Ans: RTFM, #pragma interface in file foo.h says "don't
generate symbols for this file unless it is included in foo.c".
Problem solved?
Ab.
--
+--------------------------------------------------------------------+
| Whose opinions did you say these were? | naw@planet.bt.co.uk |
+--------------------------------------------------------------------+
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads