0

I've been having some problems compiling this code for mergesort on an array of custom data types. The undefined reference is in response to

mergesort(hw1type*, int)

Here are the relevant code snips leaving out the includes, and what not, which I know all work:

Main (Where the error is propagating to):

hw1type * unsorted_array2 = new hw1type[SIZE];

unsorted_array2 = create_random_hw1type(SIZE);

mergesort(unsorted_array2, SIZE);

mergesort.h:

hw1type* mergesort(hw1type*, int);
hw1type* merge(hw1type*, int, hw1type*, int);

mergesort.cc:

hw1type mergesort(hw1type* unsorted_array, int n)

I can't see anything wrong with the way the functions are declared, or the way I'm passing the variables in main. But, it's been a while since I've used c++ so I could use a second or third pair of eyes!

Edit: Turns out it was the make file, I forgot to change. It's always something simple.

Ah, right, I was using the standard make file that came with the other classes.Anyway, here's that code:

I should probably throw mergesort.o into the list of OBJs, but anything else? The make file looks like this:

CC=gcc
CXX=c++
CFLAGS=-ggdb -O3 -I. 
LDFLAGS=-L. -lcs600

OBJ=timer.o random_generator.o sort.o

cs600:  libcs600.a main.o
    $(CXX) $(CFLAGS) main.o -o hw1 $(LDFLAGS)

libcs600.a: $(OBJ)
    ar ru libcs600.a $(OBJ)
    ranlib libcs600.a

.cc.o:
    $(CXX) -c $(CFLAGS) $<


clean:
    @rm -f *.o
    @rm -f libcs600.a
    @rm -f hw1

I need to add mergesort.o to the OBJ field, but anything other than that? Considering it's been a while for C++, it's really been a while since I've messed around with make files.

6
  • Not that it should cause any undefined reference, but you should use the same return type in the .h and .cc. Are you sure you're linking with mergesort.o included? Commented Jan 31, 2012 at 22:13
  • Could you turn this into a complete example (you don't need the body for the merge and mergesort functions), and include the complete error? Commented Jan 31, 2012 at 22:14
  • @JoachimIsaksson: actually, there are systems where the return type is mangled into the symbol! This is actually a sensible thing to do for multiple reasons: 1. if the function gets defined with a different return type than it is declared this error is caught at link time; 2. the error message for linker errors can contain the complete signature rather than leaving out the return type when showing a demangled symbol. 3. you could actually create a suitable header file declaring the functions defined in a libraryfrom the symbols. Commented Jan 31, 2012 at 22:21
  • The return type of mergesort is inconsistent. Commented Jan 31, 2012 at 22:22
  • 1
    Sun/Oracle's compiler does this for example. The main reason this isn't done is probably that name mangling was invented to cope with overloaded functions and it took people a while to come up with something like c++filt and then to realize that this could work with return types, too. By then, there was already code depending on the C++ ABI which didn't have the return type included. Commented Jan 31, 2012 at 22:27

2 Answers 2

1

"Undefined reference" is a link-time error. Make sure the object file that contains the missing function is part of your link command line. In your case, probably something like:

clang++ -o app main.o mergesort.o

Or simply:

clang++ -o app main.cc mergesort.cc

If you didn't compile each file separately first.

Sign up to request clarification or add additional context in comments.

Comments

0

You're not compiling mergesort.cc into object code, and/or you're not linking in that object code.

If you're using an IDE, make sure you've added mergesort.cc to your project configuration; if you're using Makefiles or compiling on the command line, make sure you include that in the list of source files you're compiling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.