public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* Bug in g++ 4.1.2 when using inline function definied in cpp file but declared in h file
@ 2007-03-22  5:39 Bartosz Wadolowski
  2007-03-23  0:46 ` Jim Wilson
  0 siblings, 1 reply; 2+ messages in thread
From: Bartosz Wadolowski @ 2007-03-22  5:39 UTC (permalink / raw)
  To: gcc-bugs

hi
i found bug in g++ 4.1.2 - version included with new kubuntu fiesty.
to reproduce this bug i've created 3 simple files a.h, a.cpp and main.cpp

//a.h
#ifndef A_H
#define A_H
class A{
public:
	void b(int c);
private:
int d;
};
#endif 

//a.cpp
#include "a.h"
inline void A::b(int c)
{
	d = c;
}

//main.cpp
#include "a.h"
int main()
{
	A *a = new A;
	a->b(1);
	return 0;
} 


then when i'm compiling them :
g++ -c -o a.o a.cpp - everything is ok
g++ -o A a.o main.o - i get:

main.o: In function `main':
main.cpp:(.text+0x2f): undefined reference to `A::b(int)'
collect2: ld returned 1 exit status

when i add those 2 lines to a.cpp:
#pragma implementation
#pragma interface

so it looks like this:

//a.cpp
#include "a.h"
#pragma implementation
#pragma interface
inline void A::b(int c)
{
	d = c;
}

everything is ok. i tried every possible combination of those pragmas and 
inline kayword, but only this one seams to work.

bartosz wadolowski


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Bug in g++ 4.1.2 when using inline function definied in cpp file  but declared in h file
  2007-03-22  5:39 Bug in g++ 4.1.2 when using inline function definied in cpp file but declared in h file Bartosz Wadolowski
@ 2007-03-23  0:46 ` Jim Wilson
  0 siblings, 0 replies; 2+ messages in thread
From: Jim Wilson @ 2007-03-23  0:46 UTC (permalink / raw)
  To: Bartosz Wadolowski; +Cc: gcc-bugs

We don't track bugs reported via email.  If you want to make sure you 
get an answer, use the bugzilla database instead.  This doesn't seem to 
be a gcc bug though.

In C++, the inline keyword is similar to what "static inline" means in 
GNU C, i.e. only emit the function if it is used.  Since there is no use 
of the function in a.cpp, gcc does not emit it.  You can see this if you 
compile a.cpp with -S and look at the assembler output.

You can fix this by deleting the use of the inline keyword.  Or you can 
fix it by putting the inline function definition into the a.h file 
instead of the a.cpp file.  This way it will be visible in main.cpp when 
we call it, and then gcc will emit it.

Or you can fix it by using pragma implementation and pragma interface as 
you discovered.

I'm not a C++ expert.  There may also be other ways to fix this.
-- 
Jim Wilson, GNU Tools Support, http://www.specifix.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-03-23  0:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-22  5:39 Bug in g++ 4.1.2 when using inline function definied in cpp file but declared in h file Bartosz Wadolowski
2007-03-23  0:46 ` Jim Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).