🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #712

Re: Problem linking with g++ class

#712
From: Ab Wilson
Date: Thu, 28 Jan 1993 11:36
137 lines
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 |
+--------------------------------------------------------------------+
Technical Details
Message-ID: <1993Jan28.113627.9938@planet.bt.co.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!UB.com!pacbell.com!sgiblab!spool.mu.edu!agate!doc.ic.ac.uk!warwick!uknet!axion!planet.bt.co.uk!naw
References: <1k3jc2INNjnk@iskut.ucs.ubc.ca> <honglin.18@apmaths.uwo.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!UB.com!pacbell.com!sgiblab!spool.mu.edu!agate!doc.ic.ac.uk!warwick!uknet!axion!planet.bt.co.uk!naw
From: naw@planet.bt.co.uk (Ab Wilson)
Newsgroups: gnu.g++.help
Subject: Re: Problem linking with g++ class
Message-ID: <1993Jan28.113627.9938@planet.bt.co.uk>
Date: 28 Jan 93 11:36:27 GMT
References: <1k3jc2INNjnk@iskut.ucs.ubc.ca> <honglin.18@apmaths.uwo.ca>
Sender: news@planet.bt.co.uk
Reply-To: naw@planet.bt.co.uk
Organization: BT Labs, Martlesham Heath, Ipswich, UK.
Lines: 137
In-Reply-To: honglin@apmaths.uwo.ca's message of 26 Jan 93 19:32:12 GMT