This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC C++ & inlining problem


On Mon, 16 Dec 2002, Brenner Joel wrote:

> There seems to be a number of problems with GCC inlining class member
> functions in C++ code, it is not clear if these are different
> manifestations of a single problem or are separate problems, however

Yes they are the manifastation of the same problem. You should take a look
in i.e. Stroustrup "The C++ Programming Language". In my german version,
4th edition your problem is described in Chapter 9 "Quelldateien und
Programme". Should be something about organizing source code files.

> they appear to be serious errors resulting in significant code growth
> and sometimes making it impossible to link the resulting object files.
>
> test.cpp
>
> 		class ClassA{
> 			int Value;
> 		public:
> 			ClassA(){}
> 			~ClassA(){}
> 			ClassA & operator=(const int op1){
> 				Value =  op1;
> 				return *this;
> 			}
> 			ClassA & operator*=(const int op2){
> 				Value *= op2;
> 				return *this;
> 			}
> 		};
>
>
> 		void test(void){
> 			ClassA x;
> 			x = 1;
> 		}
>
>
> this compiles OK and produces a small executable about 2K bytes.

OK, no problem here.

> Now if we move the class to ClassA.h and include it we get the same results.
>
> Next, split the class declaration into ClassA.h and it's definition also in
> ClassA.cpp as so
>
> ClassA.h
> 		class ClassA{
> 			int Value;
> 		public:
> 			ClassA();
> 			~ClassA();
> 			ClassA & operator=(const int op1);
> 			ClassA & operator*=(const int op2);
>
> 		};
>
> 		ClassA::ClassA(){}
> 		ClassA::~ClassA(){}
> 		ClassA & ClassA::operator=(const int op1){
> 			Value =  op1;
> 			return *this;
> 		}
> 		ClassA & ClassA::operator*=(const int op2){
> 			Value *= op2;
> 			return *this;
> 		}
>
> test.cpp
> 		#include "ClassA.h"
> 		void test(void){
> 			ClassA x;
> 			x = 1;
> 		}
>
> This still compiles Ok and produces a 2K object file.
Yes, everything is visible to the compile while translating test.cpp.

> now we move the class definition into ClassA.cpp
> ClassA.h
> 		class ClassA{
> 			int Value;
> 		public:
> 			ClassA();
> 			~ClassA();
> 			ClassA & operator=(const int op1);
> 			ClassA & operator*=(const int op2);
>
> 		};
>
> ClassA.cpp
> 		#include "ClassA.h"
> 		ClassA::ClassA(){}
> 		ClassA::~ClassA(){}
> 		ClassA & ClassA::operator=(const int op1){
> 			Value =  op1;
> 			return *this;
> 		}
> 		ClassA & ClassA::operator*=(const int op2){
> 			Value *= op2;
> 			return *this;
> 		}
>
> test.cpp
> 		#include "ClassA.h"
> 		void test(void){
> 			ClassA x;
> 			x = 1;
> 		}
>
> re-compile and the code size increases to about 25 K bytes.
>
That is OK too. The compiler does not know the meaning of the operators
while compiling test.cpp, becaus only ClassA.h is visible. So function
calls and some other things are introduced. This grows the binary.

> next, try making the functions inline by adding the inline keyword
> ClassA.cpp
> 		#include "ClassA.h"
> 		inline ClassA::ClassA(){}
> 		inline ClassA::~ClassA(){}
> 		inline ClassA & ClassA::operator=(const int op1){
> 			Value =  op1;
> 			return *this;
> 		}
> 		inline ClassA & ClassA::operator*=(const int op2){
> 			Value *= op2;
> 			return *this;
> 		}
>
>
> the program will no longer link, all of the operators and constructors of
> class A are now unresolved.

This behaviour is correct. There are no functions to be called from
test.cpp, because they are inlined in ClassA.cpp. So the linker is
unhappy.

mit freundliche Gruessen / kind regards
Andreas Conz

WWW    http://www.neuro.informatik.uni-kassel.de/~andreasc
e-mail andreasc@neuro.informatik.uni-kassel.de




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]