C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 3rd, 2012, 01:21 PM
jandeme jandeme is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 jandeme User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 7 sec
Reputation Power: 0
Newbie C++ question

Hi!

I've developed an application in perl, and now I'm (trying) writing it to C++. In perl, I had different group of functionalities in different .pm files like one for sql, and another one for templates and so on. I was trying to follow the same idea in c++ and have the same in different object files. The problem came, when I tried to use an sql function from the templates object:

Undefined reference to function xxx

Could you please give me a hint how to organize the program structure in c++ to make it work?

Many thanks!

Janos

Reply With Quote
  #2  
Old September 3rd, 2012, 01:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,381 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 5 m 43 sec
Reputation Power: 4080
In C++, when you use separate source files, you need to also have something called a Makefile (or a Project file, depending on compiler), which tells the C++ compiler which modules to include in the final project.

When you see an "Undefined reference to function XXX" type message, that indicates that the linker doesn't know where to pull function XXX from. This indicates that you haven't set up your Makefile or Project file correctly to tell it the various modules that make up your project.

If you tell us which compiler you are using, we could give you more specific instructions.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #3  
Old September 3rd, 2012, 02:36 PM
jandeme jandeme is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 jandeme User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 7 sec
Reputation Power: 0
I'm using gcc 4.6.2.

Reply With Quote
  #4  
Old September 4th, 2012, 12:49 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,381 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 5 m 43 sec
Reputation Power: 4080
Ok, what you need is a Makefile that tells the compiler the various modules that make up your project. Something like this ought to do it:
Code:
C = g++

# Change the following options as needed
C_OPTS = -O2 -g -Wall
PROGRAM = testprogram
OBJS = main.o \
       second.o \
       third.o

# Change the dirs and libs as needed
INCLUDEDIRS =
#INCLUDEDIRS = -I../include -I/foo/bar/include
LIBS =
#LIBS = -lm -lsocket
LIBDIRS =
#LIBDIRS = -L/foo/bar/socketlib -L /bar/foo

# Don't need to fiddle with anything beyond this.

PROGRAM: $(OBJS)
	$(CC) -o $(PROGRAM) $(C_OPTS) $(INCLUDEDIRS) $(LIBDIRS) $(OBJS) $(LIBS)

%.o:%.cpp
	$(CC) $(C_OPTS) -c -o $@ $(INCLUDEDIRS) $<

clean:
	/bin/rm *.o $(PROGRAM)

Name this file as "Makefile" and put it in the directory with your .cpp files.

You'll need to modify some sections of Makefile for your specific project. At the very minimum, you want to change PROGRAM = to the name of your final program and also change the OBJS= to the names of the various modules of your project (put a .o entry for every .cpp file that you want from your project in here). I put comments in where you need to modify the Makefile to your needs.

Also note that where the lines are indented 8 characters from the left, those are tab characters, not spaces. While GNUmake is tolerant of using either, some make programs insist on tabs, so use tabs where needed.

To compile this program, simply run make from the command line and it should compile everything into a final program.

Hope this helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Newbie C++ question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap