LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-09-2009, 10:01 PM   #1
scratchmonkey
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
Problems linking object files with geany on ubuntu


I have created a simple test class - Mortgage, with the class declaration in the .h file and the class's methods defined in the .cpp file. (mortgage.h && mortgage.cpp, respectively) Straight up C++ 101 as far as I can tell. I instantiate the class in the main() function, which is defined in practice.cpp.
Using Geany on Ubuntu, both the practice.o and mortgage.o files are created, but then I get a linker error:

undefined reference to class::functionName

I get it for each function. If the mortgage.cpp file is foremost in the editor when I click "build", then I get the undefined reference to main() error, if practice.cpp is foremost (where the main() function is) then I get undefined reference to Mortgage::Mortgage(), and all the rest of the functions defined for that class.

How do I get the linker to know where my object files are so it will link them in? Or is the problem somewhere else entirely?

Thanks
 
Old 03-10-2009, 12:12 PM   #2
shakezilla
Member
 
Registered: Jun 2005
Location: U.S.
Distribution: xubuntu 8.10, linux 2.6.27-11-generic
Posts: 78

Rep: Reputation: 16
So you have 3 (or more) source files, correct?

Sounds like you may need an #IFNDEF somewhere. Try replacing this:

Code:
#include "mortgage.h"
with the following:

Code:
#IFNDEF MORTGAGE_H
#include "mortgage.h"
#ENDIF
Try that in each of your sources that includes the .h file.
 
Old 03-10-2009, 12:25 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Rather than put conditionals around the #include statements, the normal way is to put the conditionals inside the included files themselves. For example, mortgage.h might begin and end thus:
Code:
#if !defined(MORTGAGE_H)
#define MORTGAGE_H 1

// put your mortgage.h stuff here

#endif
Also, remember (contrary to shakezilla's advice) that things like#ifndef and #endif should be lower case.

Further, I have doubts that any of this is the source of scratchmonkey's problem. I'm guessing that it's a matter of how he's using geany as an IDE. I've never used geany.

Maybe scratchmonkey should browse through the geany manual here. Or maybe a geany expert can step forward.
 
Old 03-10-2009, 12:37 PM   #4
shakezilla
Member
 
Registered: Jun 2005
Location: U.S.
Distribution: xubuntu 8.10, linux 2.6.27-11-generic
Posts: 78

Rep: Reputation: 16
Hey thanks, I didn't know you could do that in the header file itself.

I've never really been clear on what the proper way to do preprocs is.
 
Old 03-10-2009, 12:53 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Well, you need to learn about preprocessor directives if you want to use C++. And I'm with wje_lq; I doubt your problem is a code problem. Sounds to me like a makefile problem, which presumably is handled in your development environment.
 
Old 03-10-2009, 02:07 PM   #6
scratchmonkey
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks guys,

I did use the preprocessor directives in the header file:

#ifndef MORTGAGE_H
#define MORTGAGE_H

// class declaration stuff

#endif

I also didn't think that was the issue, which is why I didn't mention it in the OP.

I was hoping there was a handy IDE out there that would allow me to continue to avoid makefiles, but looks like I'll have to bite the bullet.

Ah well, learning new things is what keeps this interesting.

Thanks again
 
Old 03-10-2009, 04:52 PM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Quote:
I was hoping there was a handy IDE out there that would allow me to continue to avoid makefiles, but looks like I'll have to bite the bullet.
Um, wait, there are plenty of IDE's out there. I don't use one, but I'm sure there are people here who do. And I'd be surprised if there wasn't a way to do what you want using geany, which bills itself as a lightweight IDE.

Have you looked through geany's documentation yet? That might be simpler than using makefiles, which are not the most user-friendly creatures on the planet.
 
Old 03-10-2009, 09:50 PM   #8
scratchmonkey
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wje_lq View Post
Have you looked through geany's documentation yet? That might be simpler than using makefiles, which are not the most user-friendly creatures on the planet.
Yes, I read through the documentation. The link you provided (thank you, by the way) points to the same information that geany provides through their help file. I actually read the docs, googled the issue for a couple days, and lurked in this forum, and linuxforums.org before posting.
Another respondent on linuxforums mentioned that he'd had similar issues with geany, and that it didn't handle multiple-file C++ programs very well. (which seems a bit weird, since everything beyond the very simplest C++ programs will have multiple files) He mentioned Sun Studio, the problem is that I'm running Ubuntu on a Dell mini-9 laptop. So the lightweight aspect of geany was a big draw.
I'll keep at it for a bit and if I find out what the issue is, I'll post it here. If anyone here knows of the solution, I'd be grateful.

Thanks again for the help.
 
Old 03-10-2009, 11:09 PM   #9
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Quote:
it didn't handle multiple-file C++ programs very well
You got me curious with this, and I actually skimmed the entire greany documentation file. The documentation is worded as though the greany author(s) don't even contemplate wanting to compile two modules and link the object files together into a single executable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Compilation problems of c++ files in ns-2.33 to generate the object code. shyan Linux - Newbie 4 12-14-2010 08:29 AM
C++ problem? Geany in UBUNTU 8.10 Len Tyree Programming 2 01-16-2009 03:46 PM
Linking precompile archive/object files with kernel module possible? Mechanic Linux - General 0 08-25-2006 03:08 PM
Problem linking f77 and f90 object files Make problem (I think) TheBrick Programming 1 05-22-2006 07:17 AM
Object linking order causes Segmentation fault??? The_Nerd Programming 1 11-04-2005 12:26 AM

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

All times are GMT -5. The time now is 07:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration