The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Newbie C++ question
Discuss Newbie C++ question in the C Programming forum on Dev Shed. Newbie C++ question C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 3rd, 2012, 01:21 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
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
|

September 3rd, 2012, 01:46 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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
|

September 3rd, 2012, 02:36 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
Time spent in forums: 21 m 7 sec
Reputation Power: 0
|
|
|
I'm using gcc 4.6.2.
|

September 4th, 2012, 12:49 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|