#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep 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
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    I'm using gcc 4.6.2.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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.
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo