LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2005, 07:17 AM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Question g++: inline functions can cause an undefined reference when linking?


I discovered a very weird compiler error this morning when working on my gam. I noticed that I have a few small functions in a source file and thought I might as well make them inline, but then I got compiler errors from g++. And it's not on every function, it's only on some functions. So I was very why g++ was accepting some functions as inline and some not. Here's a sample of the compiler errors (I was only getting this when linking all the object files):

Code:
g++ -o allacrost src/audio.o src/battle.o src/boot.o src/data.o src/engine.o src/global.o src/main.o src/map.o src/pause.o src/quit.o src/scene.o src/utils.o src/video/color.o src/video/coord_sys.o src/video/primative.o src/video/simpletext.o src/video/tgaread.o src/video/video.o  -lm -lSDL -lGL -lGLU -lIL -lILUT -lSDL_mixer -llua -llualib
src/main.o(.text+0x9ee): In function `main':
: undefined reference to `hoa_engine::GameSettings::SetTimer()'
src/main.o(.text+0xa0a): In function `main':
: undefined reference to `hoa_engine::GameModeManager::GetTop()'
src/main.o(.text+0xa37): In function `main':
: undefined reference to `hoa_engine::GameSettings::UpdateTime()'
src/main.o(.text+0xa44): In function `main':
: undefined reference to `hoa_engine::GameModeManager::GetTop()'
src/quit.o(.text+0x701): In function `hoa_quit::QuitMode::Update(unsigned)':
: undefined reference to `hoa_engine::GameModeManager::PopAll()'
collect2: ld returned 1 exit status
make: *** [allacrost] Error 1

After changing the above functions to be inline, everything compiled fine. What's interesting though is that I have two functions, hoa_engine::GameModeManager::Pop() and hoa_engine::GameModeManager::PopAll(). g++ is fine with Pop() being inline, but not with PopAll(). Here's the source of those two small functions:

Code:
// Free the top mode on the stack and pop it off
inline void GameModeManager::Pop() { 
	if (game_stack.size() > 0) {
		delete game_stack.back();
		game_stack.pop_back();
	}
}



// Pop off all game modes
void GameModeManager::PopAll() { 
	while (game_stack.size() > 0) {
		delete game_stack.back();
		game_stack.pop_back();
	}
}


So does anyone know why this is happening? This isn't a critical problem for me since I can just get around it by not inlining the "bad" functions, but I'd like to know why this is happening. If you have any wisdom to share, please do
 
Old 06-19-2005, 04:24 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I see nothing about checking one simple thing, so I'm asking. Are you sure the functions you have problems with are mentioned in correct .h files?
 
Old 06-20-2005, 10:25 AM   #3
jcspray
Member
 
Registered: Mar 2004
Location: York, UK
Distribution: Ubuntu
Posts: 132

Rep: Reputation: 15
You could get errors like this if make is confused and not recompiling the right stuff to take account of rearranged functions. Suggest removing all your object files after inlining a function and rebuilding.
 
Old 06-20-2005, 10:57 AM   #4
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Mara
I see nothing about checking one simple thing, so I'm asking. Are you sure the functions you have problems with are mentioned in correct .h files?

The problems are mentioned in the source files of code that include the "bad" inline functions, but they are 100% correct in the scope ( hoa_engine::GameModeManager::PopAll() ). The source I posted above comes from engine.cpp and those two functions are declared in the file engine.h like so:

Code:
class GameModeManager {
private:
	SINGLETON_DECLARE(GameModeManager)
	std::vector<GameMode*> game_stack;
public: 
	SINGLETON_METHODS(GameModeManager)
	
	void Pop();
        void PopAll();
	void Push(GameMode* gm);
	gmode GetGameType();
	GameMode* GetTop();
	void PrintStack();
};
The SINGLETON_*(class_name) are macros defined elsewhere.


Quote:
Originally posted by jcspray
You could get errors like this if make is confused and not recompiling the right stuff to take account of rearranged functions. Suggest removing all your object files after inlining a function and rebuilding.
This is the first thing I thought when I encountered the problem as well. Believe me, I made sure that everything was as clean as could be and even tried using a different Makefile. The problem wasn't with make getting confused unfortunately.
 
Old 06-21-2005, 01:19 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Have you tried declaring them inline within the class definition in both the source to be compiled and in main.cpp? gcc might be looking for something different if main.cpp thinks that the function is non-inline.
ta0kira
 
Old 06-21-2005, 03:13 AM   #6
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
I believe this is a bug in gcc. The wxWidgets library has similar problems when compiled with gcc 3.4, but works with gcc 3.3.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
undefined reference to functions from pcap.h suchitra Programming 6 12-28-2007 12:53 AM
gcc linking: undefined reference to just about everything shabbychef Programming 7 09-09-2005 08:42 PM
linking problems - 'undefined reference' errors back2morrie Programming 1 06-14-2005 08:02 AM
Linking own library: undefined reference to otosigo Programming 1 12-02-2004 08:05 AM
undefined reference to ... when using inline assembly in C Annie0716 Programming 3 08-01-2004 12:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration